Skip to content

Commit 51c7f0b

Browse files
author
mizantrop2397
authored
Merge pull request #285 from art-community/feature/stream-closing-fix
Feature/stream closing fix
2 parents 63a53d2 + 4d2a68c commit 51c7f0b

File tree

34 files changed

+187
-249
lines changed

34 files changed

+187
-249
lines changed

application-config-extensions/src/main/java/ru/art/config/extensions/sql/SqlAgileConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ private PoolProperties extractTomcatPoolConfig(Config config, DbConnectionProper
143143
tomcatPoolConfig.setDbProperties(ifExceptionOrEmpty(() -> config.getProperties(DRIVER_PROPERTIES), new Properties()));
144144
return tomcatPoolConfig;
145145
}
146-
}
146+
}

application-core/src/main/java/ru/art/core/context/Context.java

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
import java.util.function.*;
4343

4444
public class Context {
45-
private static final ReentrantLock LOCK = new ReentrantLock();
46-
private static volatile Context INSTANCE;
45+
private static final ReentrantLock lock = new ReentrantLock();
46+
private static volatile Context instance;
4747
private volatile ContextInitialConfiguration initialConfiguration = new ContextInitialDefaultConfiguration();
4848
private volatile ContextState state = READY;
4949
private volatile Long lastActionTimestamp = currentTimeMillis();
@@ -54,7 +54,7 @@ public class Context {
5454
}
5555

5656
private Context() {
57-
if (nonNull(INSTANCE)) {
57+
if (nonNull(instance)) {
5858
out.println(format(CONTEXT_CHANGED, initialConfiguration.getClass().getName()));
5959
}
6060
if (initialConfiguration.isUnloadModulesOnShutdown()) {
@@ -63,7 +63,7 @@ private Context() {
6363
}
6464

6565
private Context(ContextInitialConfiguration initialConfiguration) {
66-
if (nonNull(INSTANCE)) {
66+
if (nonNull(instance)) {
6767
out.println(format(CONTEXT_CHANGED, initialConfiguration.getClass().getName()));
6868
}
6969
this.initialConfiguration = initialConfiguration;
@@ -73,7 +73,7 @@ private Context(ContextInitialConfiguration initialConfiguration) {
7373
}
7474

7575
private Context(ContextInitialConfiguration contextInitialConfiguration, Map<String, ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState>> modules) {
76-
if (nonNull(INSTANCE)) {
76+
if (nonNull(instance)) {
7777
out.println(format(CONTEXT_CHANGED, contextInitialConfiguration.getClass().getName()));
7878
}
7979
this.initialConfiguration = contextInitialConfiguration;
@@ -86,40 +86,40 @@ private Context(ContextInitialConfiguration contextInitialConfiguration, Map<Str
8686
public static Context initContext(ContextInitialConfiguration contextInitialConfiguration) {
8787
if (isNull(contextInitialConfiguration))
8888
throw new ContextInitializationException(CONTEXT_INITIAL_CONFIGURATION_IS_NULL);
89-
ReentrantLock lock = Context.LOCK;
89+
ReentrantLock lock = Context.lock;
9090
lock.lock();
91-
INSTANCE = new Context(contextInitialConfiguration);
91+
instance = new Context(contextInitialConfiguration);
9292
lock.unlock();
93-
return INSTANCE;
93+
return instance;
9494
}
9595

9696
public static void recreateContext(ContextInitialConfiguration contextInitialConfiguration) {
9797
if (isNull(contextInitialConfiguration)) {
9898
throw new ContextInitializationException(CONTEXT_INITIAL_CONFIGURATION_IS_NULL);
9999
}
100-
ReentrantLock lock = Context.LOCK;
100+
ReentrantLock lock = Context.lock;
101101
lock.lock();
102-
long oldContextLastActionTimestamp = INSTANCE.lastActionTimestamp;
103-
final Map<String, ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState>> modules = INSTANCE.modules;
104-
INSTANCE = new Context(contextInitialConfiguration, modules);
105-
INSTANCE.refreshAndReloadModules();
102+
long oldContextLastActionTimestamp = instance.lastActionTimestamp;
103+
final Map<String, ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState>> modules = instance.modules;
104+
instance = new Context(contextInitialConfiguration, modules);
105+
instance.refreshAndReloadModules();
106106
out.println(format(CONTEXT_RELOADED_MESSAGE, currentTimeMillis() - oldContextLastActionTimestamp));
107-
INSTANCE.lastActionTimestamp = currentTimeMillis();
107+
instance.lastActionTimestamp = currentTimeMillis();
108108
lock.unlock();
109109
}
110110

111111
public static Context context() {
112-
Context localInstance = INSTANCE;
112+
Context localInstance = instance;
113113
if (isNull(localInstance)) {
114-
ReentrantLock lock = Context.LOCK;
114+
ReentrantLock lock = Context.lock;
115115
lock.lock();
116-
localInstance = INSTANCE;
116+
localInstance = instance;
117117
if (isNull(localInstance)) {
118-
INSTANCE = new Context();
118+
instance = new Context();
119119
}
120120
lock.unlock();
121121
}
122-
return INSTANCE;
122+
return instance;
123123
}
124124

125125
public static ContextInitialConfiguration contextConfiguration() {
@@ -128,10 +128,10 @@ public static ContextInitialConfiguration contextConfiguration() {
128128

129129
public <C extends ModuleConfiguration, S extends ModuleState> C getModule(String moduleId, Module<C, S> toLoadIfNotExists) {
130130
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
131-
if (isNull(INSTANCE) || state != READY) {
131+
if (isNull(instance) || state != READY) {
132132
return toLoadIfNotExists.getDefaultConfiguration();
133133
}
134-
ReentrantLock lock = Context.LOCK;
134+
ReentrantLock lock = Context.lock;
135135
lock.lock();
136136
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
137137
PreconfiguredModuleProvider preconfiguredModulesProvider;
@@ -151,9 +151,9 @@ public <C extends ModuleConfiguration, S extends ModuleState> C getModule(String
151151

152152
public <C extends ModuleConfiguration, S extends ModuleState> S getModuleState(String moduleId, Module<C, S> toLoadIfNotExists) {
153153
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
154-
ReentrantLock lock = Context.LOCK;
154+
ReentrantLock lock = Context.lock;
155155
lock.lock();
156-
if (isNull(INSTANCE) || state == LOADING_MODULES) {
156+
if (isNull(instance) || state == LOADING_MODULES) {
157157
lock.unlock();
158158
return toLoadIfNotExists.getState();
159159
}
@@ -175,9 +175,9 @@ public <C extends ModuleConfiguration, S extends ModuleState> S getModuleState(S
175175

176176
public <C extends ModuleConfiguration, S extends ModuleState> C getModule(String moduleId, Supplier<Module<C, S>> toLoadIfNotExists) {
177177
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
178-
ReentrantLock lock = Context.LOCK;
178+
ReentrantLock lock = Context.lock;
179179
lock.lock();
180-
if (isNull(INSTANCE) || state != READY) {
180+
if (isNull(instance) || state != READY) {
181181
lock.unlock();
182182
return toLoadIfNotExists.get().getDefaultConfiguration();
183183
}
@@ -200,9 +200,9 @@ public <C extends ModuleConfiguration, S extends ModuleState> C getModule(String
200200

201201
public <C extends ModuleConfiguration, S extends ModuleState> S getModuleState(String moduleId, Supplier<Module<C, S>> toLoadIfNotExists) {
202202
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
203-
ReentrantLock lock = Context.LOCK;
203+
ReentrantLock lock = Context.lock;
204204
lock.lock();
205-
if (isNull(INSTANCE) || state == LOADING_MODULES) {
205+
if (isNull(instance) || state == LOADING_MODULES) {
206206
lock.unlock();
207207
return toLoadIfNotExists.get().getState();
208208
}
@@ -225,7 +225,7 @@ public <C extends ModuleConfiguration, S extends ModuleState> S getModuleState(S
225225

226226
public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule(Module<C, S> module) {
227227
if (isNull(module)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
228-
ReentrantLock lock = Context.LOCK;
228+
ReentrantLock lock = Context.lock;
229229
lock.lock();
230230
ContextState currentState = state;
231231
state = LOADING_MODULES;
@@ -241,7 +241,7 @@ public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule
241241

242242
public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule(Module<C, S> module, ModuleConfigurator<C, S> moduleConfigurator) {
243243
if (isNull(module)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
244-
ReentrantLock lock = Context.LOCK;
244+
ReentrantLock lock = Context.lock;
245245
lock.lock();
246246
ContextState currentState = state;
247247
state = LOADING_MODULES;
@@ -257,7 +257,7 @@ public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule
257257

258258
public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule(Module<C, S> module, C customModuleConfiguration) {
259259
if (isNull(module)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
260-
ReentrantLock lock = Context.LOCK;
260+
ReentrantLock lock = Context.lock;
261261
lock.lock();
262262
ContextState currentState = state;
263263
state = LOADING_MODULES;
@@ -272,7 +272,7 @@ public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule
272272
}
273273

274274
private <C extends ModuleConfiguration, S extends ModuleState> C loadModule(Module<C, S> module, PreconfiguredModuleProvider preconfiguredModulesProvider) {
275-
ReentrantLock lock = Context.LOCK;
275+
ReentrantLock lock = Context.lock;
276276
lock.lock();
277277
ContextState currentState = state;
278278
state = LOADING_MODULES;
@@ -289,7 +289,7 @@ public <C extends ModuleConfiguration> Context overrideModule(String moduleId, C
289289
if (isNull(customModuleConfiguration)) {
290290
throw new ContextInitializationException(CUSTOM_MODULE_CONFIGURATION_IS_NULL);
291291
}
292-
ReentrantLock lock = Context.LOCK;
292+
ReentrantLock lock = Context.lock;
293293
lock.lock();
294294
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
295295
if (isNull(moduleContainer)) {
@@ -309,7 +309,7 @@ public <C extends ModuleConfiguration> Context overrideModule(String moduleId, C
309309
@SuppressWarnings("Duplicates")
310310
public Context reloadModule(String moduleId) {
311311
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
312-
ReentrantLock lock = Context.LOCK;
312+
ReentrantLock lock = Context.lock;
313313
lock.lock();
314314
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
315315
if (isNull(moduleContainer)) {
@@ -329,7 +329,7 @@ public Context reloadModule(String moduleId) {
329329
@SuppressWarnings("Duplicates")
330330
public Context refreshModule(String moduleId) {
331331
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
332-
ReentrantLock lock = Context.LOCK;
332+
ReentrantLock lock = Context.lock;
333333
lock.lock();
334334
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
335335
if (isNull(moduleContainer)) {
@@ -348,7 +348,7 @@ public Context refreshModule(String moduleId) {
348348

349349
public Context refreshAndReloadModule(String moduleId) {
350350
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
351-
ReentrantLock lock = Context.LOCK;
351+
ReentrantLock lock = Context.lock;
352352
lock.lock();
353353
ContextState currentState = state;
354354
state = REFRESHING_AND_RELOADING_MODULES;
@@ -394,9 +394,9 @@ public boolean hasModule(String moduleId) {
394394
}
395395

396396
public static boolean contextIsNotReady() {
397-
ReentrantLock lock = Context.LOCK;
397+
ReentrantLock lock = Context.lock;
398398
lock.lock();
399-
Context instance = INSTANCE;
399+
Context instance = Context.instance;
400400
lock.unlock();
401401
return isNull(instance) || instance.state != READY;
402402
}
@@ -405,4 +405,4 @@ private void unloadModules() {
405405
modules.values().forEach(module -> module.getModule().onUnload());
406406
modules.clear();
407407
}
408-
}
408+
}

application-core/src/main/java/ru/art/core/extension/InputOutputStreamExtensions.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@
2020

2121
import lombok.experimental.*;
2222
import static ru.art.core.constants.BufferConstants.*;
23-
import static ru.art.core.constants.StreamConstants.*;
23+
import static ru.art.core.extension.InputStreamExtensions.*;
2424
import java.io.*;
2525

2626
@UtilityClass
2727
public class InputOutputStreamExtensions {
2828
public static void transferBytes(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException {
29-
byte[] bytes = new byte[bufferSize];
30-
int readChars;
31-
while ((readChars = inputStream.read(bytes)) != EOF) {
32-
outputStream.write(bytes, 0, readChars);
33-
}
29+
outputStream.write(toByteArray(inputStream, bufferSize));
3430
}
3531

3632
public static void transferBytes(InputStream inputStream, OutputStream outputStream) throws IOException {

application-core/src/main/java/ru/art/core/extension/InputStreamExtensions.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@
3535

3636
@UtilityClass
3737
public class InputStreamExtensions {
38-
public static byte[] toByteArray(InputStream is) {
39-
return toByteArray(is, DEFAULT_BUFFER_SIZE);
38+
public static byte[] toByteArray(InputStream inputStream) {
39+
return toByteArray(inputStream, DEFAULT_BUFFER_SIZE);
4040
}
4141

42-
public static byte[] toByteArray(InputStream is, int bufferSize) {
42+
public static byte[] toByteArray(InputStream inputStream, int bufferSize) {
4343
if (bufferSize <= 0) {
4444
return EMPTY_BYTES;
4545
}
4646
ByteBuffer buffer = allocateDirect(bufferSize);
4747
byte[] result = EMPTY_BYTES;
4848
try {
49-
ReadableByteChannel channel = newChannel(is);
49+
ReadableByteChannel channel = newChannel(inputStream);
5050
while (channel.read(buffer) != EOF) {
5151
buffer.flip();
5252
byte[] bufferBytes = new byte[buffer.limit()];
@@ -63,25 +63,32 @@ public static byte[] toByteArray(InputStream is, int bufferSize) {
6363
return result;
6464
}
6565

66-
public static byte[] toByteArraySafety(InputStream is) {
66+
public static byte[] toByteArraySafety(InputStream inputStream) {
67+
return toByteArraySafety(inputStream, DEFAULT_BUFFER_SIZE);
68+
}
69+
70+
public static byte[] toByteArraySafety(InputStream inputStream, int bufferSize) {
6771
try {
68-
return toByteArray(is);
72+
return toByteArray(inputStream, bufferSize);
6973
} catch (Throwable throwable) {
7074
throwable.printStackTrace();
7175
return EMPTY_BYTES;
7276
}
7377
}
7478

75-
public static String toString(InputStream is, Charset charset) {
76-
return new String(toByteArray(is), charset);
79+
public static String toString(InputStream inputStream) {
80+
return toString(inputStream, DEFAULT_BUFFER_SIZE);
7781
}
7882

79-
public static String toString(InputStream is) {
80-
try {
81-
return toString(is, contextConfiguration().getCharset());
82-
} catch (Throwable throwable) {
83-
throwable.printStackTrace();
84-
return EMPTY_STRING;
85-
}
83+
public static String toString(InputStream inputStream, int bufferSize) {
84+
return toString(inputStream, bufferSize, contextConfiguration().getCharset());
85+
}
86+
87+
public static String toString(InputStream inputStream, Charset charset) {
88+
return toString(inputStream, DEFAULT_BUFFER_SIZE, charset);
89+
}
90+
91+
public static String toString(InputStream inputStream, int bufferSize, Charset charset) {
92+
return new String(toByteArray(inputStream, bufferSize), charset);
8693
}
8794
}

application-core/src/main/java/ru/art/core/jar/JarExtensions.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import ru.art.core.exception.*;
2222
import static java.nio.file.Files.*;
2323
import static java.nio.file.StandardCopyOption.*;
24-
import static java.util.Objects.*;
2524
import static java.util.Optional.*;
2625
import static java.util.regex.Pattern.*;
2726
import static java.util.stream.Collectors.*;
@@ -73,10 +72,8 @@ public static void extractJar(String jarPath) {
7372
}
7473

7574
public static void extractJar(String jarPath, String directory) {
76-
ZipFile jarArchive = null;
77-
try {
75+
try (ZipFile jarArchive = new ZipFile(jarPath)) {
7876
createDirectories(Paths.get(directory));
79-
jarArchive = new ZipFile(jarPath);
8077
for (ZipEntry entry : jarArchive.stream().collect(toList())) {
8178
Path entryDestination = Paths.get(directory).resolve(entry.getName());
8279
if (entry.isDirectory() && !exists(entryDestination)) {
@@ -87,14 +84,6 @@ public static void extractJar(String jarPath, String directory) {
8784
}
8885
} catch (IOException ioException) {
8986
throw new InternalRuntimeException(ioException);
90-
} finally {
91-
if (nonNull(jarArchive)) {
92-
try {
93-
jarArchive.close();
94-
} catch (IOException ignored) {
95-
// Ignore cause unnecessary to handle this exception
96-
}
97-
}
9887
}
9988
}
10089

@@ -125,10 +114,8 @@ public static void extractJarEntry(String jarPath, String entryRegex) {
125114
}
126115

127116
public static void extractJarEntry(String jarPath, String entryRegex, String directory) {
128-
ZipFile jarArchive = null;
129-
try {
117+
try (ZipFile jarArchive = new ZipFile(jarPath)) {
130118
createDirectories(Paths.get(directory));
131-
jarArchive = new ZipFile(jarPath);
132119
List<? extends ZipEntry> entries = jarArchive
133120
.stream()
134121
.filter(entry -> compile(entryRegex).matcher(entry.getName()).matches())
@@ -144,14 +131,6 @@ public static void extractJarEntry(String jarPath, String entryRegex, String dir
144131
}
145132
} catch (IOException ioException) {
146133
throw new InternalRuntimeException(ioException);
147-
} finally {
148-
if (nonNull(jarArchive)) {
149-
try {
150-
jarArchive.close();
151-
} catch (IOException ioException) {
152-
ioException.printStackTrace();
153-
}
154-
}
155134
}
156135
}
157136
}

application-core/src/main/java/ru/art/core/lazy/LazyLoadingValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class LazyLoadingValue<T> {
1212
private final AtomicReference<T> value = new AtomicReference<>();
1313
private final Supplier<T> loader;
1414

15-
public T value() {
15+
public T fastValue() {
1616
T value;
1717
if (nonNull(value = this.value.get())) {
1818
return value;

0 commit comments

Comments
 (0)