Skip to content

Commit

Permalink
Stability updates for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
GedMarc committed May 31, 2024
1 parent d1ad4c4 commit daef326
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
99 changes: 42 additions & 57 deletions src/main/java/com/guicedee/guicedinjection/GuiceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
import io.github.classgraph.ScanResult;
import lombok.extern.java.Log;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.regex.Pattern;

import static com.guicedee.guicedinjection.properties.GlobalProperties.getSystemPropertyOrEnvironment;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;

/**
* Provides an interface for reflection and injection in one.
Expand All @@ -53,11 +54,6 @@ public class GuiceContext<J extends GuiceContext<J>> implements IGuiceContext
* This particular instance of the class
*/
private static final GuiceContext<?> instance = new GuiceContext<>();

/**
* A list of all the loaded singleton sets
*/
//private static final Map<Class, Set> allLoadedServices = new LinkedHashMap<>();
/**
* The building injector
*/
Expand Down Expand Up @@ -96,13 +92,14 @@ private GuiceContext()
}



/**
* Reference the Injector Directly
*
* @return The global Guice Injector Object, Never Null, Instantiates the Injector if not configured
*/
public synchronized Injector inject()

public Injector inject()
{
if (GuiceContext.buildingInjector)
{
Expand All @@ -114,6 +111,7 @@ public synchronized Injector inject()
try
{
GuiceContext.buildingInjector = true;
LocalDateTime start = LocalDateTime.now();
GuiceContext.log.info("Starting up Guice Context");
GuiceContext
.instance()
Expand Down Expand Up @@ -159,8 +157,8 @@ public void run()
.destroy();
}
});

GuiceContext.log.config("Injection System Ready");
LocalDateTime end = LocalDateTime.now();
log.info("System started in " + ChronoUnit.MILLIS.between(start, end) + "ms");
}
catch (Throwable e)
{
Expand Down Expand Up @@ -245,7 +243,7 @@ private static int getJavaVersion()
*
* @return The physical Scan Result from the complete class scanner
*/

public ScanResult getScanResult()
{
if (scanResult == null)
Expand Down Expand Up @@ -352,7 +350,7 @@ private String[] getJarsInclusionList()
/**
* Starts up Guice and the scanner
*/
private synchronized void loadScanner()
private void loadScanner()
{
if (scanner == null)
{
Expand Down Expand Up @@ -727,7 +725,7 @@ private Map<Pattern, ResourceList.ByteArrayConsumer> quickScanFilesPattern()
* @return A set of them
*/
@SuppressWarnings("unchecked")

public <T> Set<T> getLoader(Class<T> loaderType, @SuppressWarnings("unused") boolean dontInject, ServiceLoader<T> serviceLoader)
{
if (!IGuiceContext
Expand Down Expand Up @@ -778,58 +776,45 @@ public static void setScanner(ClassGraph scanner)
private void loadPostStartups()
{
Set<IGuicePostStartup> startupSet = loadPostStartupServices();
Map<Integer, Set<IGuicePostStartup>> groupedPostStartup = startupSet.stream()
.collect(groupingBy(IGuicePostStartup::sortOrder, toSet()));
/* Map<Integer, Set<IGuicePostStartup<?>>> postStartupGroups = new TreeMap<>();
Map<Integer, Set<IGuicePostStartup<?>>> postStartupGroups = new TreeMap<>();
for (IGuicePostStartup<?> postStartup : startupSet)
{
Integer sortOrder = postStartup.sortOrder();
postStartupGroups
.computeIfAbsent(sortOrder, k -> new TreeSet<>())
.add(postStartup);
}
*/
for (Map.Entry<Integer, Set<IGuicePostStartup>> entry : groupedPostStartup.entrySet())
for (Map.Entry<Integer, Set<IGuicePostStartup<?>>> entry : postStartupGroups.entrySet())
{
Integer key = entry.getKey();
Set<IGuicePostStartup> value = entry.getValue();
if (value.size() == 1)
Set<IGuicePostStartup<?>> value = entry.getValue();
List<CompletableFuture<Boolean>> futures = new ArrayList<>();
// log.info("Starting Post Startup Group [" + key + "]");
ExecutorService ex = null;
for (IGuicePostStartup<?> iGuicePostStartup : value)
{
log.info("Starting Post Load [" + iGuicePostStartup.getClass()
.getSimpleName() + "] - Start Order [" + key + "]");
ex= iGuicePostStartup.getExecutorService();
futures.addAll(iGuicePostStartup.postLoad());
}
try
{
//run in order
for (IGuicePostStartup<?> iGuicePostStartup : value)
if(!futures.isEmpty())
{
try
{
iGuicePostStartup.postLoad();
}
catch (Throwable T)
CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();
if (ex != null)
{
log.log(Level.SEVERE,
"Cannot execute post startup - " + iGuicePostStartup
.getClass()
.getCanonicalName(),
T);
ex.shutdown();
ex.awaitTermination(30, TimeUnit.SECONDS);
}
}
}
else
catch (Exception e)
{
log.info("Starting Post Startup Group [" + key + "] in Parallel");
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (IGuicePostStartup<?> iGuicePostStartup : value)
{
futures.add(CompletableFuture.runAsync(iGuicePostStartup::postLoad));
}
try
{
CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();
}
catch (Exception e)
{
log.log(Level.SEVERE, "Exception in completing post startups", e);
}
log.log(Level.SEVERE, "Exception in completing post startups", e);
}

GuiceContext.log.fine("Completed with Post Startups Key [" + key + "]");
}
}
Expand All @@ -849,7 +834,7 @@ public GuiceConfig<?> getConfig()
*
* @return The list of guice post startups
*/
public Set<IGuicePostStartup> loadPostStartupServices()
public Set<IGuicePostStartup> loadPostStartupServices()
{
return new TreeSet<>(getLoader(IGuicePostStartup.class, ServiceLoader.load(IGuicePostStartup.class)));
}
Expand All @@ -859,7 +844,7 @@ public Set<IGuicePostStartup> loadPostStartupServices()
*
* @return The list of guice post startups
*/
public Set<IPathContentsRejectListScanner> loadPathRejectScanners()
public Set<IPathContentsRejectListScanner> loadPathRejectScanners()
{
return getLoader(IPathContentsRejectListScanner.class, true, ServiceLoader.load(IPathContentsRejectListScanner.class));
}
Expand All @@ -870,7 +855,7 @@ public Set<IPathContentsRejectListScanner> loadPathRejectScanners()
*
* @return The list of guice post startups
*/
public Set<IGuiceScanJarExclusions> loadJarRejectScanners()
public Set<IGuiceScanJarExclusions> loadJarRejectScanners()
{
return getLoader(IGuiceScanJarExclusions.class, true, ServiceLoader.load(IGuiceScanJarExclusions.class));
}
Expand All @@ -881,7 +866,7 @@ public Set<IGuiceScanJarExclusions> loadJarRejectScanners()
*
* @return The list of guice post startups
*/
public Set<IGuiceScanJarInclusions> loadJarInclusionScanners()
public Set<IGuiceScanJarInclusions> loadJarInclusionScanners()
{
return getLoader(IGuiceScanJarInclusions.class, true, ServiceLoader.load(IGuiceScanJarInclusions.class));
}
Expand All @@ -892,7 +877,7 @@ public Set<IGuiceScanJarInclusions> loadJarInclusionScanners()
*
* @return The list of guice post startups
*/
public Set<IGuicePreStartup> loadPreStartupServices()
public Set<IGuicePreStartup> loadPreStartupServices()
{
return new TreeSet<>(getLoader(IGuicePreStartup.class, true, ServiceLoader.load(IGuicePreStartup.class)));
}
Expand All @@ -902,7 +887,7 @@ public Set<IGuicePreStartup> loadPreStartupServices()
*
* @return The list of guice post startups
*/
public Set<IGuiceModule> loadIGuiceModules()
public Set<IGuiceModule> loadIGuiceModules()
{
return new TreeSet<>(getLoader(IGuiceModule.class, true, ServiceLoader.load(IGuiceModule.class)));
}
Expand All @@ -912,7 +897,7 @@ public Set<IGuiceModule> loadIGuiceModules()
*
* @return The list of guice configs
*/
public Set<IGuiceConfigurator> loadIGuiceConfigs()
public Set<IGuiceConfigurator> loadIGuiceConfigs()
{
return getLoader(IGuiceConfigurator.class, true, ServiceLoader.load(IGuiceConfigurator.class));
}
Expand Down Expand Up @@ -941,7 +926,7 @@ private void loadPreStartups()
* @return A set of them
*/
@SuppressWarnings("unchecked")

public <T extends Comparable<T>> Set<T> getLoader(Class<T> loaderType, ServiceLoader<T> serviceLoader)
{
if (!IGuiceContext
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.guicedee.guicedinjection.interfaces.IGuicePostStartup;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ParallelPostStartupTest1 implements IGuicePostStartup<ParallelPostStartupTest1>
{
@Override
public void postLoad()
public List<CompletableFuture<Boolean>> postLoad()
{
System.out.println("Starting 1");
return List.of(CompletableFuture.supplyAsync(() -> {
System.out.println("Starting 1");
return true;
}));
}

@Override
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.guicedee.guicedinjection.interfaces.IGuicePostStartup;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ParallelPostStartupTest2 implements IGuicePostStartup<ParallelPostStartupTest2>
{
@Override
public void postLoad()
public List<CompletableFuture<Boolean>> postLoad()
{
System.out.println("Starting 2");
return List.of(CompletableFuture.supplyAsync(() -> {
System.out.println("Starting 2");
return true;
}));
}

@Override
Expand Down

0 comments on commit daef326

Please sign in to comment.