From c2d44ce19716f6933beb2afc0ac3dfacaf4969fe Mon Sep 17 00:00:00 2001 From: GedMarc Date: Thu, 29 Feb 2024 01:13:39 +0200 Subject: [PATCH] Client update for more decentralized functionality --- .../guicedinjection/GuiceContext.java | 524 ++++++++---------- .../guicedee/guicedinjection/JobService.java | 87 +-- .../GuiceContextProvision.java | 15 + .../implementations/JobServiceProvision.java | 14 + .../injections/ContextBinderGuice.java | 6 +- .../guicedinjection/pairing/OptionalPair.java | 75 --- .../guicedinjection/pairing/Pair.java | 145 ----- .../properties/GlobalProperties.java | 218 -------- .../representations/ICopyable.java | 3 +- src/main/java/module-info.java | 11 +- ....guicedinjection.interfaces.IGuiceProvider | 1 + ...edinjection.interfaces.IJobServiceProvider | 1 + .../com/guicedee/tests/FileSearchTest.java | 6 +- .../com/guicedee/tests/GuiceContextTest.java | 4 +- .../com/guicedee/tests/IGuiceConfigTest.java | 5 +- .../com/guicedee/tests/OptionalPairTest.java | 3 +- .../java/com/guicedee/tests/PsvmTest.java | 4 +- 17 files changed, 340 insertions(+), 782 deletions(-) create mode 100644 src/main/java/com/guicedee/guicedinjection/implementations/GuiceContextProvision.java create mode 100644 src/main/java/com/guicedee/guicedinjection/implementations/JobServiceProvision.java delete mode 100644 src/main/java/com/guicedee/guicedinjection/pairing/OptionalPair.java delete mode 100644 src/main/java/com/guicedee/guicedinjection/pairing/Pair.java delete mode 100644 src/main/java/com/guicedee/guicedinjection/properties/GlobalProperties.java create mode 100644 src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceProvider create mode 100644 src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IJobServiceProvider diff --git a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java index f8c2a66..bb23574 100644 --- a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java +++ b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java @@ -18,6 +18,7 @@ import com.google.common.base.*; import com.google.inject.*; +import com.guicedee.client.*; import com.guicedee.guicedinjection.abstractions.*; import com.guicedee.guicedinjection.interfaces.*; import com.guicedee.guicedinjection.interfaces.annotations.*; @@ -44,7 +45,7 @@ */ @Log @SuppressWarnings("MissingClassJavaDoc") -public class GuiceContext> +public class GuiceContext> implements IGuiceContext { /** * This particular instance of the class @@ -54,7 +55,7 @@ public class GuiceContext> /** * A list of all the loaded singleton sets */ - private static final Map allLoadedServices = new LinkedHashMap<>(); + //private static final Map allLoadedServices = new LinkedHashMap<>(); /** * The building injector */ @@ -92,8 +93,6 @@ private GuiceContext() //No config required } - private static Set registerModuleForScanning = new LinkedHashSet<>(); - private static List modules = new ArrayList<>(); /** * Reference the Injector Directly @@ -101,7 +100,7 @@ private GuiceContext() * @return The global Guice Injector Object, Never Null, Instantiates the Injector if not configured */ @NotNull - public static synchronized Injector inject() + public synchronized Injector inject() { if (GuiceContext.buildingInjector) { @@ -114,36 +113,53 @@ public static synchronized Injector inject() { GuiceContext.buildingInjector = true; GuiceContext.log.info("Starting up Guice Context"); - GuiceContext.instance() - .loadConfiguration(); - if (GuiceContext.instance() - .getConfig() - .isPathScanning() || - GuiceContext.instance() - .getConfig() - .isClasspathScanning()) + GuiceContext + .instance() + .loadConfiguration(); + if (GuiceContext + .instance() + .getConfig() + .isPathScanning() || GuiceContext + .instance() + .getConfig() + .isClasspathScanning()) { - GuiceContext.instance() - .loadScanner(); + GuiceContext + .instance() + .loadScanner(); } - GuiceContext.instance() - .loadPreStartups(); + GuiceContext + .instance() + .loadPreStartups(); List cModules = new ArrayList<>(modules); - Set iGuiceModules = GuiceContext.instance().loadIGuiceModules(); + Set iGuiceModules = GuiceContext + .instance() + .loadIGuiceModules(); cModules.addAll(iGuiceModules); //cModules.add(new GuiceInjectorModule()); GuiceContext.instance().injector = Guice.createInjector(cModules); GuiceContext.buildingInjector = false; - GuiceContext.instance() - .loadPostStartups(); + GuiceContext + .instance() + .loadPostStartups(); - Runtime.getRuntime() - .addShutdownHook(new Thread(GuiceContext::destroy)); + Runtime + .getRuntime() + .addShutdownHook(new Thread() + { + public void run() + { + IGuiceContext + .getContext() + .destroy(); + } + }); GuiceContext.log.config("Injection System Ready"); - } catch (Throwable e) + } + catch (Throwable e) { GuiceContext.log.log(Level.SEVERE, "Exception creating Injector : " + e.getMessage(), e); throw new RuntimeException("Unable to boot Guice Injector", e); @@ -153,155 +169,15 @@ public static synchronized Injector inject() return GuiceContext.instance().injector; } - /** - * Gets a new injected instance of a class - * - * @param The type to retrieve - * @param type The physical class object - * @return The scoped object - */ - @NotNull - public static T get(@NotNull Class type) - { - return get(type, null); - } - - /** - * Gets a new injected instance of a class - * - * @param The type to retrieve - * @param type The physical class object - * @return The scoped object - * @deprecated For get() - */ - @NotNull - @Deprecated() - public static T getInstance(@NotNull Class type) - { - return get(type, null); - } - - /** - * Gets a new injected instance of a class - * - * @param The type to retrieve - * @param type The physical class object - * @return The scoped object - * @deprecated For get() - */ - @NotNull - @Deprecated() - public static T getInstance(@NotNull Key type) - { - return get(type); - } - - /** - * Gets a new injected instance of a class - * - * @param The type to retrieve - * @param type The physical class object - * @return The scoped object - * @deprecated For get() - */ - @NotNull - @Deprecated() - public static T getInstance(@NotNull Class type, Class annotation) - { - return get(type, annotation); - } - - private static boolean isEntityType(Class clazz) - { - try - { - for (Annotation annotation : clazz.getAnnotations()) - { - if (annotation.annotationType() - .getCanonicalName() - .equalsIgnoreCase("jakarta.persistence.Entity")) - { - return true; - } - } - } catch (NullPointerException npe) - { - return false; - } - return false; - } - - private static boolean isNotEnhanceable(Class clazz) - { - return clazz.isAnnotationPresent(INotEnhanceable.class); - } - - private static boolean isNotInjectable(Class clazz) - { - return clazz.isAnnotationPresent(INotInjectable.class); - } - - /** - * Gets a new injected instance of a class - * - * @param The type to retrieve - * @param type The physical class object - * @param annotation The annotation to fetch - * @return The scoped object - */ - public static T get(@NotNull Class type, Class annotation) - { - if (annotation == null) - { - return get(Key.get(type)); - } - return get(Key.get(type, annotation)); - } - - /** - * Gets a new specified instance from a give key - * - * @param The type to retrieve - * @param type The physical class object - * @return The scoped object - */ - @SuppressWarnings("unchecked") - @NotNull - public static T get(@NotNull Key type) - { - Class clazz = (Class) type.getTypeLiteral() - .getRawType(); - T instance; - boolean isEntityType = isEntityType(clazz); - if (isNotEnhanceable(clazz) || isEntityType) - { - try - { - instance = clazz.getDeclaredConstructor() - .newInstance(); - if (!isNotInjectable(clazz)) - { - inject().injectMembers(instance); - } - } catch (Exception e) - { - log.log(Level.SEVERE, "Unable to construct [" + clazz.getCanonicalName() + "]. Not Enhanceable or an Entity.", e); - throw new RuntimeException(e); - } - } else - { - instance = inject().getInstance(type); - } - return instance; - } + private static Set destroyers = GuiceContext + .instance() + .getLoader(IGuicePreDestroy.class, false, ServiceLoader.load(IGuicePreDestroy.class)); - private static Set destroyers = GuiceContext.instance() - .getLoader(IGuicePreDestroy.class, false, ServiceLoader.load(IGuicePreDestroy.class)); /** * Execute on Destroy */ @SuppressWarnings("unused") - public static void destroy() + public void destroy() { try { @@ -310,12 +186,17 @@ public static void destroy() try { destroyer.onDestroy(); - } catch (Throwable T) + } + catch (Throwable T) { - log.log(Level.SEVERE, "Could not run destroyer [" + destroyer.getClass().getCanonicalName() + "]"); + log.log(Level.SEVERE, + "Could not run destroyer [" + destroyer + .getClass() + .getCanonicalName() + "]"); } } - } catch (Throwable T) + } + catch (Throwable T) { log.log(Level.SEVERE, "Could not run destroyers", T); } @@ -336,6 +217,7 @@ public static void destroy() * Returns the Java version as an int value. * * @return the Java version as an int value (8, 9, etc.) + * * @since 12130 */ private static int getJavaVersion() @@ -402,12 +284,12 @@ private void loadConfiguration() Set guiceConfigurators = loadIGuiceConfigs(); for (IGuiceConfigurator guiceConfigurator : guiceConfigurators) { - GuiceContext.log.config("Loading IGuiceConfigurator - " + - guiceConfigurator.getClass() - .getCanonicalName()); + GuiceContext.log.config("Loading IGuiceConfigurator - " + guiceConfigurator + .getClass() + .getCanonicalName()); guiceConfigurator.configure(GuiceContext.config); } - if(!GuiceContext.config.isIncludeModuleAndJars()) + if (!GuiceContext.config.isIncludeModuleAndJars()) { log.warning("Scanning is not restricted to modules and may incur a performance impact. Consider registering your module with GuiceContext.registerModule() to auto enable, or SPI IGuiceConfiguration"); } @@ -426,8 +308,9 @@ private String[] getJarsExclusionList() { Set strings = new TreeSet<>(); Set exclusions = loadJarRejectScanners(); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IGuiceScanJarExclusions exclusion : exclusions) { @@ -449,8 +332,9 @@ private String[] getJarsInclusionList() { Set strings = new TreeSet<>(); Set exclusions = loadJarInclusionScanners(); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IGuiceScanJarInclusions exclusion : exclusions) { @@ -479,23 +363,25 @@ private synchronized void loadScanner() { if (async) { - scanResult = scanner.scan(Runtime.getRuntime() - .availableProcessors()); - } else + scanResult = scanner.scan(Runtime + .getRuntime() + .availableProcessors()); + } + else { scanResult = scanner.scan(); } stopwatch.stop(); Map fileScans = quickScanFiles(); - fileScans.forEach((key, value) -> - scanResult.getResourcesWithLeafName(key) - .forEachByteArrayIgnoringIOException(value)); - quickScanFilesPattern().forEach( - (key, value) -> - scanResult.getResourcesMatchingPattern(key) - .forEachByteArrayIgnoringIOException(value)); + fileScans.forEach((key, value) -> scanResult + .getResourcesWithLeafName(key) + .forEachByteArrayIgnoringIOException(value)); + quickScanFilesPattern().forEach((key, value) -> scanResult + .getResourcesMatchingPattern(key) + .forEachByteArrayIgnoringIOException(value)); - } catch (Exception mpe) + } + catch (Exception mpe) { GuiceContext.log.log(Level.SEVERE, "Unable to run scanner", mpe); } @@ -536,13 +422,15 @@ private ClassGraph configureScanner(ClassGraph graph) { graph = graph.rejectJars(jarRejections); } - } else + } + else { String[] modulesRejection = getModulesExclusionList(); if (modulesRejection.length != 0) { graph = graph.rejectModules(modulesRejection); - } else + } + else { graph = graph.ignoreParentModuleLayers(); } @@ -559,14 +447,16 @@ private ClassGraph configureScanner(ClassGraph graph) { graph = graph.acceptJars(jarRejections); } - } else + } + else { String[] modulesRejection = getModulesInclusionsList(); log.config("Accepted Modules for Scanning : " + Arrays.toString(modulesRejection)); if (modulesRejection.length != 0) { graph = graph.acceptModules(modulesRejection); - } else + } + else { graph = graph.ignoreParentModuleLayers(); } @@ -642,14 +532,16 @@ private String[] getPackagesList() { Set strings = new LinkedHashSet<>(); Set exclusions = getLoader(IPackageContentsScanner.class, true, ServiceLoader.load(IPackageContentsScanner.class)); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IPackageContentsScanner exclusion : exclusions) { - GuiceContext.log.log(Level.CONFIG, "Loading IPackageContentsScanner - " + - exclusion.getClass() - .getCanonicalName()); + GuiceContext.log.log(Level.CONFIG, + "Loading IPackageContentsScanner - " + exclusion + .getClass() + .getCanonicalName()); Set searches = exclusion.searchFor(); strings.addAll(searches); } @@ -667,14 +559,16 @@ private String[] getBlacklistPackages() { Set strings = new LinkedHashSet<>(); Set exclusions = getLoader(IPackageRejectListScanner.class, true, ServiceLoader.load(IPackageRejectListScanner.class)); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IPackageRejectListScanner exclusion : exclusions) { - GuiceContext.log.log(Level.CONFIG, "Loading IPackageContentsScanner - " + - exclusion.getClass() - .getCanonicalName()); + GuiceContext.log.log(Level.CONFIG, + "Loading IPackageContentsScanner - " + exclusion + .getClass() + .getCanonicalName()); Set searches = exclusion.exclude(); strings.addAll(searches); } @@ -692,14 +586,16 @@ private String[] getPathsList() { Set strings = new TreeSet<>(); Set exclusions = getLoader(IPathContentsScanner.class, true, ServiceLoader.load(IPathContentsScanner.class)); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IPathContentsScanner exclusion : exclusions) { - GuiceContext.log.log(Level.CONFIG, "Loading IPathScanningContentsScanner - " + - exclusion.getClass() - .getCanonicalName()); + GuiceContext.log.log(Level.CONFIG, + "Loading IPathScanningContentsScanner - " + exclusion + .getClass() + .getCanonicalName()); Set searches = exclusion.searchFor(); strings.addAll(searches); } @@ -717,14 +613,16 @@ private String[] getPathsBlacklistList() { Set strings = new TreeSet<>(); Set exclusions = loadPathRejectScanners(); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IPathContentsRejectListScanner exclusion : exclusions) { - GuiceContext.log.log(Level.CONFIG, "Loading IPathContentsRejectListScanner - " + - exclusion.getClass() - .getCanonicalName()); + GuiceContext.log.log(Level.CONFIG, + "Loading IPathContentsRejectListScanner - " + exclusion + .getClass() + .getCanonicalName()); Set searches = exclusion.searchFor(); strings.addAll(searches); } @@ -743,8 +641,9 @@ private String[] getModulesExclusionList() { Set strings = new TreeSet<>(); Set exclusions = getLoader(IGuiceScanModuleExclusions.class, true, ServiceLoader.load(IGuiceScanModuleExclusions.class)); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IGuiceScanModuleExclusions exclusion : exclusions) { @@ -755,24 +654,27 @@ private String[] getModulesExclusionList() } return strings.toArray(new String[0]); } - - /** + /* + *//** * Registers a module for scanning when filtering is enabled * * @param javaModuleName The name in the module-info.java file + * * @return This instance - */ + *//* @SuppressWarnings("unchecked") public static void registerModule(String javaModuleName) { instance().registerModuleForScanning.add(javaModuleName); - instance().getConfig().setIncludeModuleAndJars(true); + instance() + .getConfig() + .setIncludeModuleAndJars(true); } public static void registerModule(com.google.inject.Module module) { instance().modules.add(module); - } + }*/ /** * Returns a complete list of generic exclusions @@ -785,8 +687,9 @@ private String[] getModulesInclusionsList() Set strings = new TreeSet<>(); strings.addAll(registerModuleForScanning); Set exclusions = getLoader(IGuiceScanModuleInclusions.class, true, ServiceLoader.load(IGuiceScanModuleInclusions.class)); - if (exclusions.iterator() - .hasNext()) + if (exclusions + .iterator() + .hasNext()) { for (IGuiceScanModuleInclusions exclusion : exclusions) { @@ -807,9 +710,10 @@ private Map quickScanFiles() Set fileScanners = getLoader(IFileContentsScanner.class, true, ServiceLoader.load(IFileContentsScanner.class)); for (IFileContentsScanner fileScanner : fileScanners) { - GuiceContext.log.log(Level.CONFIG, "Loading IFileContentsScanner - " + - fileScanner.getClass() - .getCanonicalName()); + GuiceContext.log.log(Level.CONFIG, + "Loading IFileContentsScanner - " + fileScanner + .getClass() + .getCanonicalName()); fileScans.putAll(fileScanner.onMatch()); } return fileScans; @@ -824,9 +728,10 @@ private Map quickScanFilesPattern() Set fileScanners = getLoader(IFileContentsPatternScanner.class, true, ServiceLoader.load(IFileContentsPatternScanner.class)); for (IFileContentsPatternScanner fileScanner : fileScanners) { - GuiceContext.log.log(Level.CONFIG, "Loading IFileContentsPatternScanner - " + - fileScanner.getClass() - .getCanonicalName()); + GuiceContext.log.log(Level.CONFIG, + "Loading IFileContentsPatternScanner - " + fileScanner + .getClass() + .getCanonicalName()); fileScans.putAll(fileScanner.onMatch()); } return fileScans; @@ -838,20 +743,25 @@ private Map quickScanFilesPattern() * @param loaderType The service type * @param The type * @param dontInject Don't inject + * * @return A set of them */ @SuppressWarnings("unchecked") @NotNull - public Set getLoader(Class loaderType, - @SuppressWarnings("unused") - boolean dontInject, ServiceLoader serviceLoader) + public Set getLoader(Class loaderType, @SuppressWarnings("unused") boolean dontInject, ServiceLoader serviceLoader) { - if (!getAllLoadedServices().containsKey(loaderType)) + if (!IGuiceContext + .getAllLoadedServices() + .containsKey(loaderType)) { - Set loader = loaderToSetNoInjection(serviceLoader); - getAllLoadedServices().put(loaderType, loader); + Set loader = IGuiceContext.loaderToSetNoInjection(serviceLoader); + IGuiceContext + .getAllLoadedServices() + .put(loaderType, loader); } - return getAllLoadedServices().get(loaderType); + return IGuiceContext + .getAllLoadedServices() + .get(loaderType); } /** @@ -887,8 +797,9 @@ private void loadPostStartups() for (IGuicePostStartup postStartup : startupSet) { Integer sortOrder = postStartup.sortOrder(); - postStartupGroups.computeIfAbsent(sortOrder, k -> new TreeSet<>()) - .add(postStartup); + postStartupGroups + .computeIfAbsent(sortOrder, k -> new TreeSet<>()) + .add(postStartup); } for (Map.Entry> entry : postStartupGroups.entrySet()) @@ -903,23 +814,29 @@ private void loadPostStartups() try { iGuicePostStartup.postLoad(); - } catch (Throwable T) + } + catch (Throwable T) { - log.log(Level.SEVERE, "Cannot execute post startup - " + iGuicePostStartup.getClass() - .getCanonicalName(), T); + log.log(Level.SEVERE, + "Cannot execute post startup - " + iGuicePostStartup + .getClass() + .getCanonicalName(), + T); } } - } else + } + else { log.info("Starting Post Startup Group [" + key + "] in Parallel"); ExecutorService postStartup = null; for (IGuicePostStartup iGuicePostStartup : value) { - postStartup = JobService.getInstance().addJob("PostStartup", () -> { + postStartup = JobService.INSTANCE.addJob("PostStartup", () -> { try { iGuicePostStartup.postLoad(); - } catch (Throwable T) + } + catch (Throwable T) { log.log(Level.SEVERE, "Cannot execute post startup - ", T); } @@ -930,9 +847,10 @@ private void loadPostStartups() if (postStartup != null) { log.config("Waiting for post startup group to finish...."); - JobService.getInstance().removeJob("PostStartup"); + JobService.INSTANCE.removeJob("PostStartup"); } - } catch (Throwable e) + } + catch (Throwable e) { log.log(Level.SEVERE, "Cannot execute post startup - ", e); } @@ -964,8 +882,7 @@ public GuiceConfig getConfig() * * @return The list of guice post startups */ - public @NotNull - Set loadPostStartupServices() + public @NotNull Set loadPostStartupServices() { return getLoader(IGuicePostStartup.class, ServiceLoader.load(IGuicePostStartup.class)); } @@ -975,8 +892,7 @@ Set loadPostStartupServices() * * @return The list of guice post startups */ - public @NotNull - Set loadPathRejectScanners() + public @NotNull Set loadPathRejectScanners() { return getLoader(IPathContentsRejectListScanner.class, true, ServiceLoader.load(IPathContentsRejectListScanner.class)); } @@ -987,8 +903,7 @@ Set loadPathRejectScanners() * * @return The list of guice post startups */ - public @NotNull - Set loadJarRejectScanners() + public @NotNull Set loadJarRejectScanners() { return getLoader(IGuiceScanJarExclusions.class, true, ServiceLoader.load(IGuiceScanJarExclusions.class)); } @@ -999,8 +914,7 @@ Set loadJarRejectScanners() * * @return The list of guice post startups */ - public @NotNull - Set loadJarInclusionScanners() + public @NotNull Set loadJarInclusionScanners() { return getLoader(IGuiceScanJarInclusions.class, true, ServiceLoader.load(IGuiceScanJarInclusions.class)); } @@ -1011,8 +925,7 @@ Set loadJarInclusionScanners() * * @return The list of guice post startups */ - public @NotNull - Set loadPreStartupServices() + public @NotNull Set loadPreStartupServices() { return getLoader(IGuicePreStartup.class, true, ServiceLoader.load(IGuicePreStartup.class)); } @@ -1022,8 +935,7 @@ Set loadPreStartupServices() * * @return The list of guice post startups */ - public @NotNull - Set loadIGuiceModules() + public @NotNull Set loadIGuiceModules() { return getLoader(IGuiceModule.class, true, ServiceLoader.load(IGuiceModule.class)); } @@ -1033,8 +945,7 @@ Set loadIGuiceModules() * * @return The list of guice configs */ - public @NotNull - Set loadIGuiceConfigs() + public @NotNull Set loadIGuiceConfigs() { return getLoader(IGuiceConfigurator.class, true, ServiceLoader.load(IGuiceConfigurator.class)); } @@ -1049,9 +960,9 @@ private void loadPreStartups() startups.sort(Comparator.comparing(IGuicePreStartup::sortOrder)); for (IGuicePreStartup startup : startups) { - GuiceContext.log.config("Loading IGuicePreStartup - " + - startup.getClass() - .getCanonicalName()); + GuiceContext.log.config("Loading IGuicePreStartup - " + startup + .getClass() + .getCanonicalName()); startup.onStartup(); } } @@ -1062,25 +973,33 @@ private void loadPreStartups() * * @param loaderType The service type * @param The type + * * @return A set of them */ @SuppressWarnings("unchecked") @NotNull public > Set getLoader(Class loaderType, ServiceLoader serviceLoader) { - if (!getAllLoadedServices().containsKey(loaderType)) + if (!IGuiceContext + .getAllLoadedServices() + .containsKey(loaderType)) { Set loader; if (GuiceContext.buildingInjector || injector == null) { - loader = loaderToSetNoInjection(serviceLoader); - } else + loader = IGuiceContext.loaderToSetNoInjection(serviceLoader); + } + else { - loader = loaderToSet(serviceLoader); + loader = IGuiceContext.loaderToSet(serviceLoader); } - getAllLoadedServices().put(loaderType, loader); + IGuiceContext + .getAllLoadedServices() + .put(loaderType, loader); } - return getAllLoadedServices().get(loaderType); + return IGuiceContext + .getAllLoadedServices() + .get(loaderType); } /** @@ -1091,18 +1010,17 @@ public > Set getLoader(Class loaderType, ServiceLo * @return the allLoadedServices (type Map Class, Set ) of this GuiceContext object. */ @SuppressWarnings("WeakerAccess") - @NotNull + /*@NotNull public static Map getAllLoadedServices() { return allLoadedServices; - } + }*/ /** * If this scanner is registered to run asynchronously * * @return - */ - public boolean isAsync() + */ public boolean isAsync() { return async; } @@ -1117,16 +1035,17 @@ public void setAsync(boolean async) this.async = async; } - Map> loaderClasses = new ConcurrentHashMap<>(); + //Map> loaderClasses = new ConcurrentHashMap<>(); /** * Method loaderToSet, converts a ServiceLoader into a TreeSet * * @param loader of type ServiceLoader + * * @return Set */ - @SuppressWarnings("unchecked") - @NotNull + //@SuppressWarnings("unchecked") + /*@NotNull public > Set loaderToSet(ServiceLoader loader) { @SuppressWarnings("rawtypes") @@ -1138,12 +1057,14 @@ public > Set loaderToSet(ServiceLoader loader) if (!loaderClasses.containsKey(type)) { - GuiceConfig config = GuiceContext.instance().getConfig(); + GuiceConfig config = GuiceContext + .instance() + .getConfig(); if (config.isServiceLoadWithClassPath()) { for (ClassInfo classInfo : instance() - .getScanResult() - .getClassesImplementing(type)) + .getScanResult() + .getClassesImplementing(type)) { Class load = (Class) classInfo.loadClass(); loadeds.add(load); @@ -1155,7 +1076,8 @@ public > Set loaderToSet(ServiceLoader loader) { loadeds.add(newInstance.getClass()); } - } catch (Throwable T) + } + catch (Throwable T) { log.log(Level.SEVERE, "Unable to provide instance of " + type + " to TreeSet", T); } @@ -1165,12 +1087,12 @@ public > Set loaderToSet(ServiceLoader loader) Set outcomes = new TreeSet<>(); for (Class aClass : loaderClasses.get(type)) { - outcomes.add((T) GuiceContext.get(aClass)); + outcomes.add((T) IGuiceContext.get(aClass)); } return outcomes; - } + }*/ - public > Set> loadClassSet(ServiceLoader loader) + /*public > Set> loadClassSet(ServiceLoader loader) { String type = loader.toString(); type = type.replace("java.util.ServiceLoader[", ""); @@ -1179,12 +1101,14 @@ public > Set> loadClassSet(ServiceLoader loa if (!loaderClasses.containsKey(type)) { Set loadeds = new HashSet<>(); - GuiceConfig config = GuiceContext.instance().getConfig(); + GuiceConfig config = GuiceContext + .instance() + .getConfig(); if (config.isServiceLoadWithClassPath()) { for (ClassInfo classInfo : instance() - .getScanResult() - .getClassesImplementing(type)) + .getScanResult() + .getClassesImplementing(type)) { @SuppressWarnings("unchecked") Class load = (Class) classInfo.loadClass(); @@ -1198,7 +1122,8 @@ public > Set> loadClassSet(ServiceLoader loa //noinspection unchecked loadeds.add((Class) newInstance.getClass()); } - } catch (Throwable T) + } + catch (Throwable T) { log.log(Level.SEVERE, "Unable to provide instance of " + type + " to TreeSet", T); } @@ -1206,29 +1131,31 @@ public > Set> loadClassSet(ServiceLoader loa } //noinspection unchecked return (Set) loaderClasses.get(type); - } + }*/ /** * Method loaderToSet, converts a ServiceLoader into a TreeSet * * @param loader of type ServiceLoader + * * @return Set */ - @SuppressWarnings("unchecked") + /*@SuppressWarnings("unchecked") @NotNull public Set loaderToSetNoInjection(ServiceLoader loader) { Set> loadeds = new HashSet<>(); - GuiceConfig config = GuiceContext.instance().getConfig(); + GuiceConfig config = GuiceContext + .instance() + .getConfig(); String type = loader.toString(); type = type.replace("java.util.ServiceLoader[", ""); type = type.substring(0, type.length() - 1); - if (config.isServiceLoadWithClassPath() && !buildingInjector && instance() - .getScanResult() != null) + if (config.isServiceLoadWithClassPath() && !buildingInjector && instance().getScanResult() != null) { for (ClassInfo classInfo : instance() - .getScanResult() - .getClassesImplementing(type)) + .getScanResult() + .getClassesImplementing(type)) { Class load = (Class) classInfo.loadClass(); loadeds.add(load); @@ -1243,10 +1170,12 @@ public Set loaderToSetNoInjection(ServiceLoader loader) output.add(newInstance); completed.add((Class) newInstance.getClass()); } - } catch (java.util.ServiceConfigurationError T) + } + catch (java.util.ServiceConfigurationError T) { log.log(Level.WARNING, "Cannot load services - ", T); - }catch (Throwable T) + } + catch (Throwable T) { log.log(Level.SEVERE, "Cannot load services - ", T); } @@ -1259,11 +1188,12 @@ public Set loaderToSetNoInjection(ServiceLoader loader) try { output.add((T) newInstance.getDeclaredConstructor()); - } catch (NoSuchMethodException e) + } + catch (NoSuchMethodException e) { log.log(Level.SEVERE, "Cannot load a service through default constructor", e); } } return output; - } + }*/ } diff --git a/src/main/java/com/guicedee/guicedinjection/JobService.java b/src/main/java/com/guicedee/guicedinjection/JobService.java index 8c09c52..5cc584a 100644 --- a/src/main/java/com/guicedee/guicedinjection/JobService.java +++ b/src/main/java/com/guicedee/guicedinjection/JobService.java @@ -1,7 +1,7 @@ package com.guicedee.guicedinjection; import com.google.inject.Singleton; -import com.guicedee.guicedinjection.interfaces.IGuicePreDestroy; +import com.guicedee.guicedinjection.interfaces.*; import lombok.Getter; import lombok.Setter; import lombok.extern.java.Log; @@ -18,8 +18,7 @@ @Singleton @Log -public class JobService - implements IGuicePreDestroy +public class JobService implements IGuicePreDestroy, IJobService { private final Map serviceMap = new ConcurrentHashMap<>(); private final Map pollingMap = new ConcurrentHashMap<>(); @@ -34,17 +33,9 @@ public class JobService @Setter private static TimeUnit defaultWaitUnit = TimeUnit.SECONDS; - private static final JobService INSTANCE = new JobService(); + public static final JobService INSTANCE = new JobService(); private static ExecutorService jobCleanup = null; - public static JobService getInstance(){ - if (jobCleanup == null) - { - jobCleanup = INSTANCE.jobCleanup(); - } - return INSTANCE; - } - public JobService() { //No config required @@ -55,6 +46,7 @@ public JobService() * * @return */ + @Override public Set getJobPools() { return serviceMap.keySet(); @@ -65,6 +57,7 @@ public Set getJobPools() * * @return */ + @Override public Set getPollingPools() { return pollingMap.keySet(); @@ -75,6 +68,7 @@ public Set getPollingPools() * * @param pool The pool to remove */ + @Override public ExecutorService removeJob(String pool) { ExecutorService es = serviceMap.get(pool); @@ -93,6 +87,7 @@ public ExecutorService removeJob(String pool) * * @param pool The pool name to remove */ + @Override public ScheduledExecutorService removePollingJob(String pool) { ScheduledExecutorService es = pollingMap.get(pool); @@ -112,6 +107,7 @@ public ScheduledExecutorService removePollingJob(String pool) * @param name * @param executorService */ + @Override public ExecutorService registerJobPool(String name, ExecutorService executorService) { if (serviceMap.containsKey(name)) @@ -126,7 +122,8 @@ public ExecutorService registerJobPool(String name, ExecutorService executorServ if (executorService instanceof ForkJoinPool) { ForkJoinPool pool = (ForkJoinPool) executorService; - } else if (executorService instanceof ThreadPoolExecutor) + } + else if (executorService instanceof ThreadPoolExecutor) { ThreadPoolExecutor executor = (ThreadPoolExecutor) executorService; executor.setMaximumPoolSize(maxQueueCount.get(name)); @@ -143,6 +140,7 @@ public ExecutorService registerJobPool(String name, ExecutorService executorServ * @param name The name of the pool * @param executorService The service executor */ + @Override public ScheduledExecutorService registerJobPollingPool(String name, ScheduledExecutorService executorService) { if (pollingMap.containsKey(name)) @@ -159,9 +157,14 @@ public ScheduledExecutorService registerJobPollingPool(String name, ScheduledExe * @param jobPoolName * @param thread */ + @Override public ExecutorService addJob(String jobPoolName, Runnable thread) { - if (!serviceMap.containsKey(jobPoolName) || serviceMap.get(jobPoolName).isTerminated() || serviceMap.get(jobPoolName).isShutdown()) + if (!serviceMap.containsKey(jobPoolName) || serviceMap + .get(jobPoolName) + .isTerminated() || serviceMap + .get(jobPoolName) + .isShutdown()) { registerJobPool(jobPoolName, executorServiceSupplier.get()); } @@ -171,7 +174,7 @@ public ExecutorService addJob(String jobPoolName, Runnable thread) { log.log(Level.FINER, maxQueueCount + " Hit - Finishing before next run"); removeJob(jobPoolName); - service = registerJobPool(jobPoolName,executorServiceSupplier.get()); + service = registerJobPool(jobPoolName, executorServiceSupplier.get()); } service.execute(thread); return service; @@ -183,9 +186,14 @@ public ExecutorService addJob(String jobPoolName, Runnable thread) * @param jobPoolName * @param thread */ + @Override public Future addTask(String jobPoolName, Callable thread) { - if (!serviceMap.containsKey(jobPoolName) || serviceMap.get(jobPoolName).isTerminated() || serviceMap.get(jobPoolName).isShutdown()) + if (!serviceMap.containsKey(jobPoolName) || serviceMap + .get(jobPoolName) + .isTerminated() || serviceMap + .get(jobPoolName) + .isShutdown()) { registerJobPool(jobPoolName, executorServiceSupplier.get()); } @@ -200,11 +208,13 @@ public Future addTask(String jobPoolName, Callable thread) return service.submit(thread); } + @Override public void waitForJob(String jobName) { waitForJob(jobName, defaultWaitTime, defaultWaitUnit); } + @Override public void waitForJob(String jobName, long timeout, TimeUnit unit) { if (!serviceMap.containsKey(jobName)) @@ -216,7 +226,8 @@ public void waitForJob(String jobName, long timeout, TimeUnit unit) try { service.awaitTermination(timeout, unit); - } catch (InterruptedException e) + } + catch (InterruptedException e) { log.log(Level.WARNING, "Thread didn't close cleanly, make sure running times are acceptable", e); service.shutdownNow(); @@ -231,15 +242,15 @@ public void waitForJob(String jobName, long timeout, TimeUnit unit) private ExecutorService jobCleanup() { ScheduledExecutorService jobsShutdownNotClosed = addPollingJob("JobsShutdownNotClosed", () -> { - for (String jobPool : getInstance().getJobPools()) + for (String jobPool : getJobPools()) { ExecutorService executorService = serviceMap.get(jobPool); - if(executorService.isShutdown() && !executorService.isTerminated()) + if (executorService.isShutdown() && !executorService.isTerminated()) { log.fine("Closing unfinished job - " + jobPool); removeJob(jobPool); } - if(executorService.isShutdown() && executorService.isTerminated()) + if (executorService.isShutdown() && executorService.isTerminated()) { log.fine("Cleaning terminated job - " + jobPool); executorService.close(); @@ -257,12 +268,19 @@ private ExecutorService jobCleanup() * @param jobPoolName * @param thread */ + @Override public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable thread, long delay, TimeUnit unit) { - if (!pollingMap.containsKey(jobPoolName) || pollingMap.get(jobPoolName).isTerminated() || pollingMap.get(jobPoolName).isShutdown()) + if (!pollingMap.containsKey(jobPoolName) || pollingMap + .get(jobPoolName) + .isTerminated() || pollingMap + .get(jobPoolName) + .isShutdown()) { - registerJobPollingPool(jobPoolName, Executors.newScheduledThreadPool(Runtime.getRuntime() - .availableProcessors())); + registerJobPollingPool(jobPoolName, + Executors.newScheduledThreadPool(Runtime + .getRuntime() + .availableProcessors())); } ScheduledExecutorService service = pollingMap.get(jobPoolName); service.scheduleAtFixedRate(thread, 1L, delay, unit); @@ -275,12 +293,19 @@ public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable threa * @param jobPoolName * @param thread */ + @Override public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable thread, long initialDelay, long delay, TimeUnit unit) { - if (!pollingMap.containsKey(jobPoolName) || pollingMap.get(jobPoolName).isTerminated() || pollingMap.get(jobPoolName).isShutdown()) + if (!pollingMap.containsKey(jobPoolName) || pollingMap + .get(jobPoolName) + .isTerminated() || pollingMap + .get(jobPoolName) + .isShutdown()) { - registerJobPollingPool(jobPoolName, Executors.newScheduledThreadPool(Runtime.getRuntime() - .availableProcessors())); + registerJobPollingPool(jobPoolName, + Executors.newScheduledThreadPool(Runtime + .getRuntime() + .availableProcessors())); } ScheduledExecutorService service = pollingMap.get(jobPoolName); service.scheduleAtFixedRate(thread, initialDelay, delay, unit); @@ -290,16 +315,15 @@ public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable threa /** * Shutdowns */ + @Override public void destroy() { log.config("Destroying all running jobs..."); - serviceMap.forEach((key, value) -> - { + serviceMap.forEach((key, value) -> { log.config("Shutting Down [" + key + "]"); removeJob(key); }); - pollingMap.forEach((key, value) -> - { + pollingMap.forEach((key, value) -> { log.config("Shutting Down Poll Job [" + key + "]"); removePollingJob(key); }); @@ -312,7 +336,8 @@ private int getCurrentTaskCount(ExecutorService service) { ForkJoinPool pool = (ForkJoinPool) service; return (int) pool.getQueuedTaskCount(); - } else if (service instanceof ThreadPoolExecutor) + } + else if (service instanceof ThreadPoolExecutor) { ThreadPoolExecutor executor = (ThreadPoolExecutor) service; return (int) executor.getTaskCount(); diff --git a/src/main/java/com/guicedee/guicedinjection/implementations/GuiceContextProvision.java b/src/main/java/com/guicedee/guicedinjection/implementations/GuiceContextProvision.java new file mode 100644 index 0000000..55f37f4 --- /dev/null +++ b/src/main/java/com/guicedee/guicedinjection/implementations/GuiceContextProvision.java @@ -0,0 +1,15 @@ +package com.guicedee.guicedinjection.implementations; + +import com.guicedee.client.*; +import com.guicedee.guicedinjection.*; +import com.guicedee.guicedinjection.interfaces.*; + +public class GuiceContextProvision implements IGuiceProvider +{ + @Override + public IGuiceContext get() + { + return GuiceContext.instance(); + } + +} diff --git a/src/main/java/com/guicedee/guicedinjection/implementations/JobServiceProvision.java b/src/main/java/com/guicedee/guicedinjection/implementations/JobServiceProvision.java new file mode 100644 index 0000000..0631fda --- /dev/null +++ b/src/main/java/com/guicedee/guicedinjection/implementations/JobServiceProvision.java @@ -0,0 +1,14 @@ +package com.guicedee.guicedinjection.implementations; + +import com.guicedee.guicedinjection.*; +import com.guicedee.guicedinjection.interfaces.*; + +public class JobServiceProvision implements IJobServiceProvider +{ + @Override + public IJobService get() + { + return JobService.INSTANCE; + } + +} diff --git a/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java b/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java index 13ef1d9..c628429 100644 --- a/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java +++ b/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java @@ -6,7 +6,7 @@ import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.JobService; -import com.guicedee.guicedinjection.interfaces.IGuiceModule; +import com.guicedee.guicedinjection.interfaces.*; import com.guicedee.guicedinjection.properties.GlobalProperties; import io.github.classgraph.ScanResult; import lombok.extern.java.Log; @@ -43,7 +43,9 @@ public void configure() { .in(Singleton.class); ContextBinderGuice.log.fine("Bound JobService.class"); + bind(IJobService.class) + .toInstance(JobService.INSTANCE); bind(JobService.class) - .toInstance(JobService.getInstance()); + .toInstance(JobService.INSTANCE); } } diff --git a/src/main/java/com/guicedee/guicedinjection/pairing/OptionalPair.java b/src/main/java/com/guicedee/guicedinjection/pairing/OptionalPair.java deleted file mode 100644 index 0750857..0000000 --- a/src/main/java/com/guicedee/guicedinjection/pairing/OptionalPair.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.guicedee.guicedinjection.pairing; - -import jakarta.validation.constraints.NotNull; -import java.util.Optional; - -/** - * Specifies a generic pair - * - * @param Key - * @param Value - */ -public class OptionalPair - extends Pair { - - /** - * Constructs a new blank pair - */ - public OptionalPair() { - //No config required - } - - /** - * Constructs a new key value pair - * - * @param key The key to use - * @param value the value to use - */ - public OptionalPair(K key, V value) { - super(key, value); - } - - @Override - public String toString() { - return "Key[" + getKey() + "];Value[" + getValue() + "]"; - } - - /** - * Sets the value for the given pair - * - * @param value The value to set - * @return Optional nullable of the value - */ - @Override - public OptionalPair setValue(V value) { - super.setValue(value); - return this; - } - - /** - * Sets the key for the given pair - * - * @param key The key to return - * @return The optional pair - */ - @Override - public OptionalPair setKey(@NotNull K key) { - super.setKey(key); - return this; - } - - public Optional getKeyOptional() { - return Optional.ofNullable(getKey()); - } - - /** - * Returns the optional object of the value - * - * @return Optional nullable of the value - */ - public Optional getValueOptional() { - return Optional.ofNullable(getValue()); - } - - -} diff --git a/src/main/java/com/guicedee/guicedinjection/pairing/Pair.java b/src/main/java/com/guicedee/guicedinjection/pairing/Pair.java deleted file mode 100644 index 765f7f0..0000000 --- a/src/main/java/com/guicedee/guicedinjection/pairing/Pair.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.guicedee.guicedinjection.pairing; - -import jakarta.validation.constraints.NotNull; -import java.util.Objects; - -/** - * Specifies a generic pair - * - * @param Key - * @param Value - */ -public class Pair - implements Comparable> { - private static final Pair emptyPair = new Pair<>(null, null); - - /** - * The specified key - */ - private K key; - /** - * The specified value - */ - private V value; - - /** - * Constructs a new blank pair - */ - public Pair() { - //Nothing Needed - } - - /** - * Constructs a new key value pair - * - * @param key The key for the pair - * @param value The value for the pair - */ - public Pair(@NotNull K key, V value) { - this.key = key; - this.value = value; - } - - @Override - public String toString() { - return "Key[" + getKey() + "]-[" + getValue() + "}"; - } - - /** - * Gets the key for the given pair - * - * @return The key given - */ - public K getKey() { - return key; - } - - /** - * Returns the value for the given pair - * - * @return Sets this Pairs value - */ - public V getValue() { - return value; - } - - /** - * Sets the value for the given pair - * - * @param value Sets this pairs values - * @return this Pair - */ - public Pair setValue(V value) { - this.value = value; - return this; - } - - /** - * Sets the key for the given pair - * - * @param key Sets this pairs key - * @return The pair - */ - public Pair setKey(@NotNull K key) { - this.key = key; - return this; - } - - @Override - public int compareTo(@NotNull Pair o) { - return getKey().toString() - .compareTo(o.getKey() - .toString()); - } - - /** - * Returns an empty pair - * - * @return An empty pair - */ - @SuppressWarnings("unchecked") - public static Pair empty() { - return (Pair) emptyPair; - } - - /** - * If the pair is empty - * - * @return if the key is null - */ - public boolean isEmpty() { - return key == null; - } - - /** - * Returns a new instance of a pair - * - * @param key The key - * @param value The value - * @param The key type - * @param The value type - * @return The new instance of Pair - */ - public static Pair of(K key, V value) { - return new Pair<>(key, value); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Pair pair = (Pair) o; - return Objects.equals(getKey(), pair.getKey()); - } - - @Override - public int hashCode() { - return Objects.hash(getKey()); - } - - -} diff --git a/src/main/java/com/guicedee/guicedinjection/properties/GlobalProperties.java b/src/main/java/com/guicedee/guicedinjection/properties/GlobalProperties.java deleted file mode 100644 index 34799d3..0000000 --- a/src/main/java/com/guicedee/guicedinjection/properties/GlobalProperties.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (C) 2017 GedMarc - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.guicedee.guicedinjection.properties; - -import com.fasterxml.jackson.annotation.*; -import com.fasterxml.jackson.core.*; -import com.fasterxml.jackson.databind.*; -import com.google.inject.*; -import com.guicedee.guicedinjection.*; -import lombok.extern.java.Log; - -import java.util.*; -import java.util.logging.*; - -import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*; -import static com.fasterxml.jackson.annotation.JsonInclude.Include.*; - -/** - * A pretty class for containing EAR or Container level global properties. - *

- * Key to Map ID to Property - * - * @author GedMarc - * @since 08 Jul 2017 - */ -@SuppressWarnings("MissingClassJavaDoc") -@Singleton -@JsonAutoDetect(fieldVisibility = ANY, - getterVisibility = NONE, - setterVisibility = NONE) -@JsonInclude(NON_NULL) -@Log -public class GlobalProperties -{ - private final Map> globalProperties; - - /** - * Constructs a new GlobalProperties - */ - public GlobalProperties() - { - globalProperties = new HashMap<>(); - } - - /** - * Adds a key to the global application library - * - * @param key Adds a key into the global library - * @param properties Puts the property map into the global settings - */ - public void addKey(String key, Map properties) - { - globalProperties.put(key, properties); - } - - /** - * Adds a normal string string property to the library - * - * @param key Takes the key for the property - * @param property The property to apply - * @param value The value to apply - */ - public void addProperty(String key, String property, String value) - { - if (!globalProperties.containsKey(key)) - { - globalProperties.put(key, new HashMap<>()); - } - globalProperties.get(key) - .put(property, value); - } - - /** - * Adds a normal string string property to the library - * - * @param key The key and property to add - * @param property The property to add - * @param value The value to add - */ - public void addProperty(String key, String property, Object value) - { - if (!globalProperties.containsKey(key)) - { - globalProperties.put(key, new HashMap<>()); - } - globalProperties.get(key) - .put(property, value); - } - - /** - * Gets the key with the given map return type - * - * @param The key type - * @param The value map type - * @param key The key - * @return A map to return - */ - @SuppressWarnings({"unchecked", "UnusedReturnValue"}) - public Map getKey(String key) - { - return (Map) globalProperties.get(key); - } - - /** - * Gets a default string key and property mapping - * - * @param The value type - * @param key The key - * @param property And properties map to retrieve from - * @return The value of the mapped key and map ID - */ - @SuppressWarnings("unchecked") - public V getProperty(String key, String property) - { - return (V) globalProperties.get(key) - .get(property); - } - - /** - * Removes a property from any list - * - * @param key The key to remove - * @param property The property to remove from the assigned map - */ - public void removeProperty(String key, String property) - { - if (globalProperties.containsKey(key)) - { - globalProperties.get(key) - .remove(property); - } - } - - /** - * Sets the property - * - * @param key The key to remove - * @param property The property to return - */ - public void emptyProperty(String key, String property) - { - if (globalProperties.containsKey(key)) - { - globalProperties.get(key) - .put(property, ""); - } - } - - /** - * Returns a JSON implementation of the toString() - * - * @return A JSON Representation - */ - @Override - public String toString() - { - try - { - return GuiceContext.get(ObjectMapper.class) - .writeValueAsString(this); - } - catch (JsonProcessingException e) - { - log.log(Level.SEVERE, "Non-Mappable character in GlobalProperties Map, Can't toString()", e); - return super.toString(); - } - } - - public static String getSystemPropertyOrEnvironment(String name, String defaultValue) - { - if (System.getProperty(name) != null) - { - return System.getProperty(name); - } - if (System.getenv(name) != null) - { - try - { - System.setProperty(name, System.getenv(name)); - return System.getProperty(name); - }catch (Throwable T) - { - log.log(Level.WARNING,"Couldn't set system property value [" + name + "] - [" + defaultValue + "]"); - return System.getenv(name); - } - } - else { - if (defaultValue == null) - { - return null; - } - log.log(Level.WARNING,"Return default value for property [" + name + "] - [" + defaultValue + "]"); - try - { - System.setProperty(name, defaultValue); - return System.getProperty(name); - }catch (Throwable T) - { - log.log(Level.WARNING,"Couldn't set system property value [" + name + "] - [" + defaultValue + "]"); - return defaultValue; - } - } - } -} diff --git a/src/main/java/com/guicedee/guicedinjection/representations/ICopyable.java b/src/main/java/com/guicedee/guicedinjection/representations/ICopyable.java index bce8854..4e65e85 100644 --- a/src/main/java/com/guicedee/guicedinjection/representations/ICopyable.java +++ b/src/main/java/com/guicedee/guicedinjection/representations/ICopyable.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.*; import com.google.inject.Key; import com.google.inject.name.Names; +import com.guicedee.client.*; import com.guicedee.guicedinjection.GuiceContext; import lombok.extern.java.Log; @@ -64,7 +65,7 @@ default Map asMap(Field[] fields) */ default J updateFrom(Object source) { - ObjectMapper om = GuiceContext.get(Key.get(ObjectMapper.class, Names.named("Default"))); + ObjectMapper om = IGuiceContext.get(Key.get(ObjectMapper.class, Names.named("Default"))); try { String jsonFromSource = om.writeValueAsString(source); diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 6b88e06..d5d892c 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,5 +1,6 @@ import com.guicedee.guicedinjection.JobService; -import com.guicedee.guicedinjection.interfaces.IGuicePreDestroy; +import com.guicedee.guicedinjection.implementations.*; +import com.guicedee.guicedinjection.interfaces.*; module com.guicedee.guicedinjection { requires transitive com.guicedee.client; @@ -17,9 +18,8 @@ exports com.guicedee.guicedinjection; //exports com.guicedee.guicedinjection.exceptions; exports com.guicedee.guicedinjection.abstractions; - exports com.guicedee.guicedinjection.pairing; + //exports com.guicedee.guicedinjection.pairing; //exports com.guicedee.services.jsonrepresentation.json; - exports com.guicedee.guicedinjection.properties; exports com.guicedee.guicedinjection.representations; uses com.guicedee.guicedinjection.interfaces.IPackageContentsScanner; @@ -46,13 +46,12 @@ provides com.guicedee.guicedinjection.interfaces.IGuiceModule with com.guicedee.guicedinjection.injections.ContextBinderGuice; //provides com.guicedee.guicedinjection.interfaces.IGuiceModule with com.guicedee.guicedinjection.abstractions.GuiceInjectorModule; + provides IGuiceProvider with GuiceContextProvision; + provides IJobServiceProvider with JobServiceProvision; provides IGuicePreDestroy with JobService; provides java.net.spi.URLStreamHandlerProvider with com.guicedee.guicedinjection.urls.JrtUrlHandler; opens com.guicedee.guicedinjection to com.fasterxml.jackson.databind; - opens com.guicedee.guicedinjection.properties to com.fasterxml.jackson.databind; - - opens com.guicedee.guicedinjection.pairing; } diff --git a/src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceProvider b/src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceProvider new file mode 100644 index 0000000..cf6b8cd --- /dev/null +++ b/src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IGuiceProvider @@ -0,0 +1 @@ +com.guicedee.guicedinjection.implementations.GuiceContextProvision \ No newline at end of file diff --git a/src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IJobServiceProvider b/src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IJobServiceProvider new file mode 100644 index 0000000..1ff1c61 --- /dev/null +++ b/src/main/resources/META-INF/services/com.guicedee.guicedinjection.interfaces.IJobServiceProvider @@ -0,0 +1 @@ +com.guicedee.guicedinjection.implementations.JobServiceProvision \ No newline at end of file diff --git a/src/test/java/com/guicedee/tests/FileSearchTest.java b/src/test/java/com/guicedee/tests/FileSearchTest.java index 9b4de66..75c2100 100644 --- a/src/test/java/com/guicedee/tests/FileSearchTest.java +++ b/src/test/java/com/guicedee/tests/FileSearchTest.java @@ -1,5 +1,6 @@ package com.guicedee.tests; +import com.guicedee.client.*; import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.interfaces.IPathContentsRejectListScanner; import io.github.classgraph.ResourceList; @@ -28,8 +29,9 @@ public void findJSFiles() return output; } ); - - GuiceContext.inject(); + + IGuiceContext + .getContext().inject(); ResourceList resourceswithPattern = GuiceContext.instance().getScanResult() .getResourcesMatchingPattern(Pattern.compile("(.*)\\/resources\\/testResourceFind\\.js")); System.out.println("Resource List found : " + resourceswithPattern); diff --git a/src/test/java/com/guicedee/tests/GuiceContextTest.java b/src/test/java/com/guicedee/tests/GuiceContextTest.java index 89d7957..ef42082 100644 --- a/src/test/java/com/guicedee/tests/GuiceContextTest.java +++ b/src/test/java/com/guicedee/tests/GuiceContextTest.java @@ -5,6 +5,7 @@ */ package com.guicedee.tests; +import com.guicedee.client.*; import com.guicedee.guicedinjection.GuiceContext; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -30,7 +31,8 @@ public static void pre() { @Test public void testInjection() { - GuiceContext.inject(); + IGuiceContext + .getContext().inject(); } } diff --git a/src/test/java/com/guicedee/tests/IGuiceConfigTest.java b/src/test/java/com/guicedee/tests/IGuiceConfigTest.java index 35cec54..9b13002 100644 --- a/src/test/java/com/guicedee/tests/IGuiceConfigTest.java +++ b/src/test/java/com/guicedee/tests/IGuiceConfigTest.java @@ -1,5 +1,6 @@ package com.guicedee.tests; +import com.guicedee.client.*; import com.guicedee.guicedinjection.GuiceConfig; import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.interfaces.IGuiceConfig; @@ -20,8 +21,8 @@ public void testConfig() GuiceContext.instance() .loadIGuiceConfigs() .add(new IGuiceConfigTest()); - GuiceContext.inject(); - GuiceConfig config = GuiceContext.get(GuiceConfig.class); + IGuiceContext.getContext().inject(); + GuiceConfig config = IGuiceContext.get(GuiceConfig.class); config = GuiceContext.instance().getConfig(); assertTrue(config.isServiceLoadWithClassPath()); diff --git a/src/test/java/com/guicedee/tests/OptionalPairTest.java b/src/test/java/com/guicedee/tests/OptionalPairTest.java index 038d7f7..9c8aa03 100644 --- a/src/test/java/com/guicedee/tests/OptionalPairTest.java +++ b/src/test/java/com/guicedee/tests/OptionalPairTest.java @@ -1,5 +1,6 @@ package com.guicedee.tests; +import com.guicedee.client.*; import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.pairing.OptionalPair; import com.guicedee.guicedinjection.properties.GlobalProperties; @@ -9,7 +10,7 @@ public class OptionalPairTest { @Test public void toStringTest() { - GlobalProperties global = GuiceContext.get(GlobalProperties.class); + GlobalProperties global = IGuiceContext.get(GlobalProperties.class); } @Test diff --git a/src/test/java/com/guicedee/tests/PsvmTest.java b/src/test/java/com/guicedee/tests/PsvmTest.java index 61f0c45..ba2a121 100644 --- a/src/test/java/com/guicedee/tests/PsvmTest.java +++ b/src/test/java/com/guicedee/tests/PsvmTest.java @@ -1,5 +1,6 @@ package com.guicedee.tests; +import com.guicedee.client.*; import com.guicedee.guicedinjection.GuiceContext; public class PsvmTest @@ -10,6 +11,7 @@ public static void main(String[] args) GuiceContext.instance() .getConfig() .setServiceLoadWithClassPath(true); - GuiceContext.inject(); + IGuiceContext + .getContext().inject(); } }