From 2a56104cf405a7298688a3d84861727202945ca3 Mon Sep 17 00:00:00 2001 From: Marc Magon Date: Sun, 31 Dec 2023 01:48:25 +0200 Subject: [PATCH] Guiced Persistence, Test Modules configuration, Add dynamic register modules to Guice Context --- pom.xml | 8 +- .../guicedinjection/GuiceContext.java | 41 ++-- .../guicedinjection/SysStreamsLogger.java | 188 ------------------ .../CustomClassScannerI.java | 2 +- .../FileSearchTest.java | 3 +- .../GlobalPropertiesTest.java | 3 +- .../GuiceContextTest.java | 3 +- .../IGuiceConfigTest.java | 6 +- .../IGuiceContextTestConfigurator.java | 2 +- .../OptionalPairTest.java | 3 +- .../{guicedinjection => tests}/PairTest.java | 3 +- .../ParallelPostStartupTest1.java | 2 +- .../ParallelPostStartupTest2.java | 2 +- .../{guicedinjection => tests}/PsvmTest.java | 4 +- src/test/java/module-info.java | 9 + ...dinjection.interfaces.IFileContentsScanner | 2 +- ...cedinjection.interfaces.IGuiceConfigurator | 4 +- ...icedinjection.interfaces.IGuicePostStartup | 4 +- 18 files changed, 61 insertions(+), 228 deletions(-) delete mode 100644 src/main/java/com/guicedee/guicedinjection/SysStreamsLogger.java rename src/test/java/com/guicedee/{guicedinjection => tests}/CustomClassScannerI.java (93%) rename src/test/java/com/guicedee/{guicedinjection => tests}/FileSearchTest.java (96%) rename src/test/java/com/guicedee/{guicedinjection => tests}/GlobalPropertiesTest.java (92%) rename src/test/java/com/guicedee/{guicedinjection => tests}/GuiceContextTest.java (89%) rename src/test/java/com/guicedee/{guicedinjection => tests}/IGuiceConfigTest.java (92%) rename src/test/java/com/guicedee/{guicedinjection => tests}/IGuiceContextTestConfigurator.java (89%) rename src/test/java/com/guicedee/{guicedinjection => tests}/OptionalPairTest.java (89%) rename src/test/java/com/guicedee/{guicedinjection => tests}/PairTest.java (85%) rename src/test/java/com/guicedee/{guicedinjection => tests}/ParallelPostStartupTest1.java (88%) rename src/test/java/com/guicedee/{guicedinjection => tests}/ParallelPostStartupTest2.java (88%) rename src/test/java/com/guicedee/{guicedinjection => tests}/PsvmTest.java (71%) create mode 100644 src/test/java/module-info.java diff --git a/pom.xml b/pom.xml index f705276..7ac77dc 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,6 @@ commons-lang3 - com.fasterxml.jackson.core jackson-core @@ -149,6 +148,12 @@ jcl-over-slf4j + + org.slf4j + slf4j-simple + test + + org.junit.jupiter junit-jupiter-api @@ -177,7 +182,6 @@ org.apache.maven.plugins maven-compiler-plugin - ${maven.compiler.version} true diff --git a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java index ff623ed..2ffe661 100644 --- a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java +++ b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java @@ -25,7 +25,6 @@ import jakarta.validation.constraints.*; import lombok.extern.java.Log; -import java.lang.Module; import java.lang.annotation.*; import java.util.*; import java.util.concurrent.*; @@ -60,14 +59,6 @@ public class GuiceContext> * The building injector */ public static boolean buildingInjector = false; - /** - * A standard default waiting time for threads - */ - public static long defaultWaitTime = 2; - /** - * The default wait unit for the thread time - */ - public static TimeUnit defaultWaitUnit = TimeUnit.SECONDS; /** * The configuration object */ @@ -101,7 +92,8 @@ private GuiceContext() //No config required } - private Set registerModules = new LinkedHashSet<>(); + private static Set registerModuleForScanning = new LinkedHashSet<>(); + private static List modules = new ArrayList<>(); /** * Reference the Injector Directly @@ -118,7 +110,6 @@ public static synchronized Injector inject() } if (GuiceContext.instance().injector == null) { - SysStreamsLogger.bindSystemStreams(); try { GuiceContext.buildingInjector = true; @@ -138,8 +129,11 @@ public static synchronized Injector inject() GuiceContext.instance() .loadPreStartups(); - List cModules = new ArrayList<>(); - cModules.add(new GuiceInjectorModule()); + List cModules = new ArrayList<>(modules); + Set iGuiceModules = GuiceContext.instance().loadIGuiceModules(); + cModules.addAll(iGuiceModules); + + //cModules.add(new GuiceInjectorModule()); GuiceContext.instance().injector = Guice.createInjector(cModules); GuiceContext.buildingInjector = false; GuiceContext.instance() @@ -765,10 +759,15 @@ private String[] getModulesExclusionList() * @return This instance */ @SuppressWarnings("unchecked") - public J registerModule(String javaModuleName) + public static void registerModule(String javaModuleName) { - this.registerModules.add(javaModuleName); - return (J) this; + instance().registerModuleForScanning.add(javaModuleName); + instance().getConfig().setIncludeModuleAndJars(true); + } + + public static void registerModule(com.google.inject.Module module) + { + instance().modules.add(module); } /** @@ -780,7 +779,7 @@ public J registerModule(String javaModuleName) private String[] getModulesInclusionsList() { Set strings = new TreeSet<>(); - strings.addAll(registerModules); + strings.addAll(registerModuleForScanning); Set exclusions = getLoader(IGuiceScanModuleInclusions.class, true, ServiceLoader.load(IGuiceScanModuleInclusions.class)); if (exclusions.iterator() .hasNext()) @@ -1240,7 +1239,10 @@ public Set loaderToSetNoInjection(ServiceLoader loader) output.add(newInstance); completed.add((Class) newInstance.getClass()); } - } catch (Throwable T) + } catch (java.util.ServiceConfigurationError T) + { + log.log(Level.WARNING, "Cannot load services - ", T); + }catch (Throwable T) { log.log(Level.SEVERE, "Cannot load services - ", T); } @@ -1255,8 +1257,7 @@ public Set loaderToSetNoInjection(ServiceLoader loader) output.add((T) newInstance.getDeclaredConstructor()); } catch (NoSuchMethodException e) { - log - .log(Level.SEVERE, "Cannot load a service through default constructor", e); + log.log(Level.SEVERE, "Cannot load a service through default constructor", e); } } return output; diff --git a/src/main/java/com/guicedee/guicedinjection/SysStreamsLogger.java b/src/main/java/com/guicedee/guicedinjection/SysStreamsLogger.java deleted file mode 100644 index 174601a..0000000 --- a/src/main/java/com/guicedee/guicedinjection/SysStreamsLogger.java +++ /dev/null @@ -1,188 +0,0 @@ -package com.guicedee.guicedinjection; -import java.io.IOException; -import java.io.PrintStream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.guicedee.guicedinjection.properties.GlobalProperties.*; - -public class SysStreamsLogger { - private static Logger sysOutLogger = LoggerFactory.getLogger("SYSOUT"); - private static Logger sysErrLogger = LoggerFactory.getLogger("SYSERR"); - - public static final PrintStream sysout = System.out; - public static final PrintStream syserr = System.err; - - protected static final String LINE_SEPERATOR = getSystemPropertyOrEnvironment("line.separator","\n"); - - public static void bindSystemStreams() { - // Enable autoflush - System.setOut(new PrintStream(new LoggingOutputStream(sysOutLogger, false), true)); - System.setErr(new PrintStream(new LoggingOutputStream(sysErrLogger, true), true)); - } - - public static void unbindSystemStreams() { - System.setOut(sysout); - System.setErr(syserr); - } - - private static class LoggingOutputStream extends java.io.OutputStream { - - protected Logger log; - protected boolean isError; - - /** - * Used to maintain the contract of {@link #close()}. - */ - protected boolean hasBeenClosed = false; - - /** - * The internal buffer where data is stored. - */ - protected byte[] buf; - - /** - * The number of valid bytes in the buffer. This value is always in the - * range 0 through buf.length; elements - * buf[0] through buf[count-1] contain valid byte - * data. - */ - protected int count; - - /** - * Remembers the size of the buffer for speed. - */ - private int bufLength; - - /** - * The default number of bytes in the buffer. =2048 - */ - public static final int DEFAULT_BUFFER_LENGTH = 2048; - - private LoggingOutputStream() { - // illegal - } - - /** - * Creates the LoggingOutputStream to flush to the given Category. - * - * @param log - * the Logger to write to - * - * @param isError - * the if true write to error, else info - * - * @exception IllegalArgumentException - * if cat == null or priority == null - */ - public LoggingOutputStream(Logger log, boolean isError) throws IllegalArgumentException { - if (log == null) { - throw new IllegalArgumentException("log == null"); - } - - this.isError = isError; - this.log = log; - bufLength = DEFAULT_BUFFER_LENGTH; - buf = new byte[DEFAULT_BUFFER_LENGTH]; - count = 0; - } - - /** - * Closes this output stream and releases any system resources - * associated with this stream. The general contract of - * close is that it closes the output stream. A closed - * stream cannot perform output operations and cannot be reopened. - */ - @Override - public void close() { - flush(); - hasBeenClosed = true; - } - - /** - * Writes the specified byte to this output stream. The general contract - * for write is that one byte is written to the output - * stream. The byte to be written is the eight low-order bits of the - * argument b. The 24 high-order bits of b are - * ignored. - * - * @param b - * the byte to write - */ - @Override - public void write(final int b) throws IOException { - if (hasBeenClosed) { - throw new IOException("The stream has been closed."); - } - - // don't log nulls - if (b == 0) { - return; - } - - // would this be writing past the buffer? - if (count == bufLength) { - // grow the buffer - final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH; - final byte[] newBuf = new byte[newBufLength]; - - System.arraycopy(buf, 0, newBuf, 0, bufLength); - - buf = newBuf; - bufLength = newBufLength; - } - - buf[count] = (byte) b; - count++; - } - - /** - * Flushes this output stream and forces any buffered output bytes to be - * written out. The general contract of flush is that - * calling it is an indication that, if any bytes previously written - * have been buffered by the implementation of the output stream, such - * bytes should immediately be written to their intended destination. - */ - @Override - public void flush() { - - if (count == 0) { - return; - } - - // don't print out blank lines; flushing from PrintStream puts out - // these - if (count == LINE_SEPERATOR.length()) { - if (((char) buf[0]) == LINE_SEPERATOR.charAt(0) && ((count == 1) || // <- - // Unix - // & - // Mac, - // -> - // Windows - ((count == 2) && ((char) buf[1]) == LINE_SEPERATOR.charAt(1)))) { - reset(); - return; - } - } - - final byte[] theBytes = new byte[count]; - - System.arraycopy(buf, 0, theBytes, 0, count); - - if (isError) { - log.error(new String(theBytes)); - } else { - log.info(new String(theBytes)); - } - - reset(); - } - - private void reset() { - // not resetting the buffer -- assuming that if it grew that it - // will likely grow similarly again - count = 0; - } - } -} \ No newline at end of file diff --git a/src/test/java/com/guicedee/guicedinjection/CustomClassScannerI.java b/src/test/java/com/guicedee/tests/CustomClassScannerI.java similarity index 93% rename from src/test/java/com/guicedee/guicedinjection/CustomClassScannerI.java rename to src/test/java/com/guicedee/tests/CustomClassScannerI.java index 8580981..df1af29 100644 --- a/src/test/java/com/guicedee/guicedinjection/CustomClassScannerI.java +++ b/src/test/java/com/guicedee/tests/CustomClassScannerI.java @@ -1,4 +1,4 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; import com.guicedee.guicedinjection.interfaces.IFileContentsScanner; import io.github.classgraph.ResourceList; diff --git a/src/test/java/com/guicedee/guicedinjection/FileSearchTest.java b/src/test/java/com/guicedee/tests/FileSearchTest.java similarity index 96% rename from src/test/java/com/guicedee/guicedinjection/FileSearchTest.java rename to src/test/java/com/guicedee/tests/FileSearchTest.java index 9d5997e..9b4de66 100644 --- a/src/test/java/com/guicedee/guicedinjection/FileSearchTest.java +++ b/src/test/java/com/guicedee/tests/FileSearchTest.java @@ -1,5 +1,6 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; +import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.interfaces.IPathContentsRejectListScanner; import io.github.classgraph.ResourceList; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/guicedee/guicedinjection/GlobalPropertiesTest.java b/src/test/java/com/guicedee/tests/GlobalPropertiesTest.java similarity index 92% rename from src/test/java/com/guicedee/guicedinjection/GlobalPropertiesTest.java rename to src/test/java/com/guicedee/tests/GlobalPropertiesTest.java index 0e67216..94b22c1 100644 --- a/src/test/java/com/guicedee/guicedinjection/GlobalPropertiesTest.java +++ b/src/test/java/com/guicedee/tests/GlobalPropertiesTest.java @@ -1,5 +1,6 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; +import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.properties.GlobalProperties; import org.junit.jupiter.api.Assertions; diff --git a/src/test/java/com/guicedee/guicedinjection/GuiceContextTest.java b/src/test/java/com/guicedee/tests/GuiceContextTest.java similarity index 89% rename from src/test/java/com/guicedee/guicedinjection/GuiceContextTest.java rename to src/test/java/com/guicedee/tests/GuiceContextTest.java index da90a24..89d7957 100644 --- a/src/test/java/com/guicedee/guicedinjection/GuiceContextTest.java +++ b/src/test/java/com/guicedee/tests/GuiceContextTest.java @@ -3,8 +3,9 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package com.guicedee.guicedinjection; +package com.guicedee.tests; +import com.guicedee.guicedinjection.GuiceContext; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/guicedee/guicedinjection/IGuiceConfigTest.java b/src/test/java/com/guicedee/tests/IGuiceConfigTest.java similarity index 92% rename from src/test/java/com/guicedee/guicedinjection/IGuiceConfigTest.java rename to src/test/java/com/guicedee/tests/IGuiceConfigTest.java index aa03d6a..35cec54 100644 --- a/src/test/java/com/guicedee/guicedinjection/IGuiceConfigTest.java +++ b/src/test/java/com/guicedee/tests/IGuiceConfigTest.java @@ -1,12 +1,12 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; +import com.guicedee.guicedinjection.GuiceConfig; +import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.interfaces.IGuiceConfig; import com.guicedee.guicedinjection.interfaces.IGuiceConfigurator; import lombok.extern.java.Log; import org.junit.jupiter.api.Test; -import java.util.logging.Level; - import static org.junit.jupiter.api.Assertions.*; @Log diff --git a/src/test/java/com/guicedee/guicedinjection/IGuiceContextTestConfigurator.java b/src/test/java/com/guicedee/tests/IGuiceContextTestConfigurator.java similarity index 89% rename from src/test/java/com/guicedee/guicedinjection/IGuiceContextTestConfigurator.java rename to src/test/java/com/guicedee/tests/IGuiceContextTestConfigurator.java index ce33b99..7048fd2 100644 --- a/src/test/java/com/guicedee/guicedinjection/IGuiceContextTestConfigurator.java +++ b/src/test/java/com/guicedee/tests/IGuiceContextTestConfigurator.java @@ -1,4 +1,4 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; import com.guicedee.guicedinjection.interfaces.IGuiceConfig; import com.guicedee.guicedinjection.interfaces.IGuiceConfigurator; diff --git a/src/test/java/com/guicedee/guicedinjection/OptionalPairTest.java b/src/test/java/com/guicedee/tests/OptionalPairTest.java similarity index 89% rename from src/test/java/com/guicedee/guicedinjection/OptionalPairTest.java rename to src/test/java/com/guicedee/tests/OptionalPairTest.java index a5f70e3..038d7f7 100644 --- a/src/test/java/com/guicedee/guicedinjection/OptionalPairTest.java +++ b/src/test/java/com/guicedee/tests/OptionalPairTest.java @@ -1,5 +1,6 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; +import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.pairing.OptionalPair; import com.guicedee.guicedinjection.properties.GlobalProperties; import org.junit.jupiter.api.Assertions; diff --git a/src/test/java/com/guicedee/guicedinjection/PairTest.java b/src/test/java/com/guicedee/tests/PairTest.java similarity index 85% rename from src/test/java/com/guicedee/guicedinjection/PairTest.java rename to src/test/java/com/guicedee/tests/PairTest.java index 81007de..1718315 100644 --- a/src/test/java/com/guicedee/guicedinjection/PairTest.java +++ b/src/test/java/com/guicedee/tests/PairTest.java @@ -1,5 +1,6 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; +import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.pairing.Pair; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/guicedee/guicedinjection/ParallelPostStartupTest1.java b/src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java similarity index 88% rename from src/test/java/com/guicedee/guicedinjection/ParallelPostStartupTest1.java rename to src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java index 4fc1271..1db134c 100644 --- a/src/test/java/com/guicedee/guicedinjection/ParallelPostStartupTest1.java +++ b/src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java @@ -1,4 +1,4 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; import com.guicedee.guicedinjection.interfaces.IGuicePostStartup; diff --git a/src/test/java/com/guicedee/guicedinjection/ParallelPostStartupTest2.java b/src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java similarity index 88% rename from src/test/java/com/guicedee/guicedinjection/ParallelPostStartupTest2.java rename to src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java index a900b44..028767c 100644 --- a/src/test/java/com/guicedee/guicedinjection/ParallelPostStartupTest2.java +++ b/src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java @@ -1,4 +1,4 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; import com.guicedee.guicedinjection.interfaces.IGuicePostStartup; diff --git a/src/test/java/com/guicedee/guicedinjection/PsvmTest.java b/src/test/java/com/guicedee/tests/PsvmTest.java similarity index 71% rename from src/test/java/com/guicedee/guicedinjection/PsvmTest.java rename to src/test/java/com/guicedee/tests/PsvmTest.java index 753fb5f..61f0c45 100644 --- a/src/test/java/com/guicedee/guicedinjection/PsvmTest.java +++ b/src/test/java/com/guicedee/tests/PsvmTest.java @@ -1,4 +1,6 @@ -package com.guicedee.guicedinjection; +package com.guicedee.tests; + +import com.guicedee.guicedinjection.GuiceContext; public class PsvmTest { diff --git a/src/test/java/module-info.java b/src/test/java/module-info.java new file mode 100644 index 0000000..b58f263 --- /dev/null +++ b/src/test/java/module-info.java @@ -0,0 +1,9 @@ +module guice.injection.tests { + requires com.guicedee.guicedinjection; + + requires static lombok; + + requires org.junit.jupiter.api; + requires org.slf4j; + requires org.slf4j.simple; +} \ No newline at end of file diff --git a/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IFileContentsScanner b/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IFileContentsScanner index fd6f9d8..29d96e2 100644 --- a/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IFileContentsScanner +++ b/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IFileContentsScanner @@ -1 +1 @@ -com.guicedee.guicedinjection.CustomClassScannerI +com.guicedee.tests.CustomClassScannerI diff --git a/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceConfigurator b/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceConfigurator index 87bcf4a..ed7c8dc 100644 --- a/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceConfigurator +++ b/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceConfigurator @@ -1,2 +1,2 @@ -com.guicedee.guicedinjection.IGuiceConfigTest -com.guicedee.guicedinjection.IGuiceContextTestConfigurator \ No newline at end of file +com.guicedee.tests.IGuiceConfigTest +com.guicedee.tests.IGuiceContextTestConfigurator \ No newline at end of file diff --git a/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuicePostStartup b/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuicePostStartup index f480432..525220c 100644 --- a/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuicePostStartup +++ b/src/test/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuicePostStartup @@ -1,2 +1,2 @@ -com.guicedee.guicedinjection.ParallelPostStartupTest1 -com.guicedee.guicedinjection.ParallelPostStartupTest2 \ No newline at end of file +com.guicedee.tests.ParallelPostStartupTest1 +com.guicedee.tests.ParallelPostStartupTest2 \ No newline at end of file