Skip to content

Commit

Permalink
Merge pull request #285 from art-community/feature/stream-closing-fix
Browse files Browse the repository at this point in the history
Feature/stream closing fix
  • Loading branch information
mizantrop2397 authored Jun 21, 2020
2 parents 63a53d2 + 4d2a68c commit 51c7f0b
Show file tree
Hide file tree
Showing 34 changed files with 187 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ private PoolProperties extractTomcatPoolConfig(Config config, DbConnectionProper
tomcatPoolConfig.setDbProperties(ifExceptionOrEmpty(() -> config.getProperties(DRIVER_PROPERTIES), new Properties()));
return tomcatPoolConfig;
}
}
}
76 changes: 38 additions & 38 deletions application-core/src/main/java/ru/art/core/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import java.util.function.*;

public class Context {
private static final ReentrantLock LOCK = new ReentrantLock();
private static volatile Context INSTANCE;
private static final ReentrantLock lock = new ReentrantLock();
private static volatile Context instance;
private volatile ContextInitialConfiguration initialConfiguration = new ContextInitialDefaultConfiguration();
private volatile ContextState state = READY;
private volatile Long lastActionTimestamp = currentTimeMillis();
Expand All @@ -54,7 +54,7 @@ public class Context {
}

private Context() {
if (nonNull(INSTANCE)) {
if (nonNull(instance)) {
out.println(format(CONTEXT_CHANGED, initialConfiguration.getClass().getName()));
}
if (initialConfiguration.isUnloadModulesOnShutdown()) {
Expand All @@ -63,7 +63,7 @@ private Context() {
}

private Context(ContextInitialConfiguration initialConfiguration) {
if (nonNull(INSTANCE)) {
if (nonNull(instance)) {
out.println(format(CONTEXT_CHANGED, initialConfiguration.getClass().getName()));
}
this.initialConfiguration = initialConfiguration;
Expand All @@ -73,7 +73,7 @@ private Context(ContextInitialConfiguration initialConfiguration) {
}

private Context(ContextInitialConfiguration contextInitialConfiguration, Map<String, ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState>> modules) {
if (nonNull(INSTANCE)) {
if (nonNull(instance)) {
out.println(format(CONTEXT_CHANGED, contextInitialConfiguration.getClass().getName()));
}
this.initialConfiguration = contextInitialConfiguration;
Expand All @@ -86,40 +86,40 @@ private Context(ContextInitialConfiguration contextInitialConfiguration, Map<Str
public static Context initContext(ContextInitialConfiguration contextInitialConfiguration) {
if (isNull(contextInitialConfiguration))
throw new ContextInitializationException(CONTEXT_INITIAL_CONFIGURATION_IS_NULL);
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
INSTANCE = new Context(contextInitialConfiguration);
instance = new Context(contextInitialConfiguration);
lock.unlock();
return INSTANCE;
return instance;
}

public static void recreateContext(ContextInitialConfiguration contextInitialConfiguration) {
if (isNull(contextInitialConfiguration)) {
throw new ContextInitializationException(CONTEXT_INITIAL_CONFIGURATION_IS_NULL);
}
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
long oldContextLastActionTimestamp = INSTANCE.lastActionTimestamp;
final Map<String, ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState>> modules = INSTANCE.modules;
INSTANCE = new Context(contextInitialConfiguration, modules);
INSTANCE.refreshAndReloadModules();
long oldContextLastActionTimestamp = instance.lastActionTimestamp;
final Map<String, ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState>> modules = instance.modules;
instance = new Context(contextInitialConfiguration, modules);
instance.refreshAndReloadModules();
out.println(format(CONTEXT_RELOADED_MESSAGE, currentTimeMillis() - oldContextLastActionTimestamp));
INSTANCE.lastActionTimestamp = currentTimeMillis();
instance.lastActionTimestamp = currentTimeMillis();
lock.unlock();
}

public static Context context() {
Context localInstance = INSTANCE;
Context localInstance = instance;
if (isNull(localInstance)) {
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
localInstance = INSTANCE;
localInstance = instance;
if (isNull(localInstance)) {
INSTANCE = new Context();
instance = new Context();
}
lock.unlock();
}
return INSTANCE;
return instance;
}

public static ContextInitialConfiguration contextConfiguration() {
Expand All @@ -128,10 +128,10 @@ public static ContextInitialConfiguration contextConfiguration() {

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

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

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

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

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

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

public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule(Module<C, S> module, C customModuleConfiguration) {
if (isNull(module)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
ContextState currentState = state;
state = LOADING_MODULES;
Expand All @@ -272,7 +272,7 @@ public <C extends ModuleConfiguration, S extends ModuleState> Context loadModule
}

private <C extends ModuleConfiguration, S extends ModuleState> C loadModule(Module<C, S> module, PreconfiguredModuleProvider preconfiguredModulesProvider) {
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
ContextState currentState = state;
state = LOADING_MODULES;
Expand All @@ -289,7 +289,7 @@ public <C extends ModuleConfiguration> Context overrideModule(String moduleId, C
if (isNull(customModuleConfiguration)) {
throw new ContextInitializationException(CUSTOM_MODULE_CONFIGURATION_IS_NULL);
}
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
if (isNull(moduleContainer)) {
Expand All @@ -309,7 +309,7 @@ public <C extends ModuleConfiguration> Context overrideModule(String moduleId, C
@SuppressWarnings("Duplicates")
public Context reloadModule(String moduleId) {
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
if (isNull(moduleContainer)) {
Expand All @@ -329,7 +329,7 @@ public Context reloadModule(String moduleId) {
@SuppressWarnings("Duplicates")
public Context refreshModule(String moduleId) {
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
ModuleContainer<? extends ModuleConfiguration, ? extends ModuleState> moduleContainer = modules.get(moduleId);
if (isNull(moduleContainer)) {
Expand All @@ -348,7 +348,7 @@ public Context refreshModule(String moduleId) {

public Context refreshAndReloadModule(String moduleId) {
if (isNull(moduleId)) throw new ContextInitializationException(MODULE_ID_IS_NULL);
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
ContextState currentState = state;
state = REFRESHING_AND_RELOADING_MODULES;
Expand Down Expand Up @@ -394,9 +394,9 @@ public boolean hasModule(String moduleId) {
}

public static boolean contextIsNotReady() {
ReentrantLock lock = Context.LOCK;
ReentrantLock lock = Context.lock;
lock.lock();
Context instance = INSTANCE;
Context instance = Context.instance;
lock.unlock();
return isNull(instance) || instance.state != READY;
}
Expand All @@ -405,4 +405,4 @@ private void unloadModules() {
modules.values().forEach(module -> module.getModule().onUnload());
modules.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@

import lombok.experimental.*;
import static ru.art.core.constants.BufferConstants.*;
import static ru.art.core.constants.StreamConstants.*;
import static ru.art.core.extension.InputStreamExtensions.*;
import java.io.*;

@UtilityClass
public class InputOutputStreamExtensions {
public static void transferBytes(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException {
byte[] bytes = new byte[bufferSize];
int readChars;
while ((readChars = inputStream.read(bytes)) != EOF) {
outputStream.write(bytes, 0, readChars);
}
outputStream.write(toByteArray(inputStream, bufferSize));
}

public static void transferBytes(InputStream inputStream, OutputStream outputStream) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@

@UtilityClass
public class InputStreamExtensions {
public static byte[] toByteArray(InputStream is) {
return toByteArray(is, DEFAULT_BUFFER_SIZE);
public static byte[] toByteArray(InputStream inputStream) {
return toByteArray(inputStream, DEFAULT_BUFFER_SIZE);
}

public static byte[] toByteArray(InputStream is, int bufferSize) {
public static byte[] toByteArray(InputStream inputStream, int bufferSize) {
if (bufferSize <= 0) {
return EMPTY_BYTES;
}
ByteBuffer buffer = allocateDirect(bufferSize);
byte[] result = EMPTY_BYTES;
try {
ReadableByteChannel channel = newChannel(is);
ReadableByteChannel channel = newChannel(inputStream);
while (channel.read(buffer) != EOF) {
buffer.flip();
byte[] bufferBytes = new byte[buffer.limit()];
Expand All @@ -63,25 +63,32 @@ public static byte[] toByteArray(InputStream is, int bufferSize) {
return result;
}

public static byte[] toByteArraySafety(InputStream is) {
public static byte[] toByteArraySafety(InputStream inputStream) {
return toByteArraySafety(inputStream, DEFAULT_BUFFER_SIZE);
}

public static byte[] toByteArraySafety(InputStream inputStream, int bufferSize) {
try {
return toByteArray(is);
return toByteArray(inputStream, bufferSize);
} catch (Throwable throwable) {
throwable.printStackTrace();
return EMPTY_BYTES;
}
}

public static String toString(InputStream is, Charset charset) {
return new String(toByteArray(is), charset);
public static String toString(InputStream inputStream) {
return toString(inputStream, DEFAULT_BUFFER_SIZE);
}

public static String toString(InputStream is) {
try {
return toString(is, contextConfiguration().getCharset());
} catch (Throwable throwable) {
throwable.printStackTrace();
return EMPTY_STRING;
}
public static String toString(InputStream inputStream, int bufferSize) {
return toString(inputStream, bufferSize, contextConfiguration().getCharset());
}

public static String toString(InputStream inputStream, Charset charset) {
return toString(inputStream, DEFAULT_BUFFER_SIZE, charset);
}

public static String toString(InputStream inputStream, int bufferSize, Charset charset) {
return new String(toByteArray(inputStream, bufferSize), charset);
}
}
25 changes: 2 additions & 23 deletions application-core/src/main/java/ru/art/core/jar/JarExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import ru.art.core.exception.*;
import static java.nio.file.Files.*;
import static java.nio.file.StandardCopyOption.*;
import static java.util.Objects.*;
import static java.util.Optional.*;
import static java.util.regex.Pattern.*;
import static java.util.stream.Collectors.*;
Expand Down Expand Up @@ -73,10 +72,8 @@ public static void extractJar(String jarPath) {
}

public static void extractJar(String jarPath, String directory) {
ZipFile jarArchive = null;
try {
try (ZipFile jarArchive = new ZipFile(jarPath)) {
createDirectories(Paths.get(directory));
jarArchive = new ZipFile(jarPath);
for (ZipEntry entry : jarArchive.stream().collect(toList())) {
Path entryDestination = Paths.get(directory).resolve(entry.getName());
if (entry.isDirectory() && !exists(entryDestination)) {
Expand All @@ -87,14 +84,6 @@ public static void extractJar(String jarPath, String directory) {
}
} catch (IOException ioException) {
throw new InternalRuntimeException(ioException);
} finally {
if (nonNull(jarArchive)) {
try {
jarArchive.close();
} catch (IOException ignored) {
// Ignore cause unnecessary to handle this exception
}
}
}
}

Expand Down Expand Up @@ -125,10 +114,8 @@ public static void extractJarEntry(String jarPath, String entryRegex) {
}

public static void extractJarEntry(String jarPath, String entryRegex, String directory) {
ZipFile jarArchive = null;
try {
try (ZipFile jarArchive = new ZipFile(jarPath)) {
createDirectories(Paths.get(directory));
jarArchive = new ZipFile(jarPath);
List<? extends ZipEntry> entries = jarArchive
.stream()
.filter(entry -> compile(entryRegex).matcher(entry.getName()).matches())
Expand All @@ -144,14 +131,6 @@ public static void extractJarEntry(String jarPath, String entryRegex, String dir
}
} catch (IOException ioException) {
throw new InternalRuntimeException(ioException);
} finally {
if (nonNull(jarArchive)) {
try {
jarArchive.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LazyLoadingValue<T> {
private final AtomicReference<T> value = new AtomicReference<>();
private final Supplier<T> loader;

public T value() {
public T fastValue() {
T value;
if (nonNull(value = this.value.get())) {
return value;
Expand Down
Loading

0 comments on commit 51c7f0b

Please sign in to comment.