2525import java .util .Map ;
2626import java .util .Optional ;
2727import java .util .Set ;
28- import java .util .function .Consumer ;
2928import java .util .function .Predicate ;
30- import java .util .logging .Logger ;
3129import java .util .stream .Collectors ;
3230
3331/**
@@ -80,17 +78,10 @@ public static ModuleContainer.Builder builder() {
8078 */
8179 private final ModuleConstructor constructor ;
8280
83- private final Consumer <Class <? extends Module >> modulePopulator = s -> {
84- // If we have a module annotation, we are golden.
85- if (s .isAnnotationPresent (ModuleData .class )) {
86- ModuleData md = s .getAnnotation (ModuleData .class );
87- discoveredModules .put (md .id ().toLowerCase (), new ModuleSpec (s , md ));
88- } else {
89- String id = s .getName ().toLowerCase ();
90- Logger .getLogger ("QuickStart Module Loader" ).warning (MessageFormat .format ("The module {0} does not have a ModuleData annotation associated with it. We're just assuming an ID of {0}." , id ));
91- discoveredModules .put (id , new ModuleSpec (s , id , LoadingStatus .ENABLED , false ));
92- }
93- };
81+ /**
82+ * The logger to use.
83+ */
84+ private final LoggerProxy loggerProxy ;
9485
9586 /**
9687 * Constructs a {@link ModuleContainer} and starts discovery of the modules.
@@ -101,13 +92,14 @@ public static ModuleContainer.Builder builder() {
10192 *
10293 * @throws QuickStartModuleDiscoveryException if there is an error starting the Module Container.
10394 */
104- private <N extends ConfigurationNode > ModuleContainer (ConfigurationLoader <N > configurationLoader , ClassLoader loader , String packageBase , ModuleConstructor constructor ) throws QuickStartModuleDiscoveryException {
95+ private <N extends ConfigurationNode > ModuleContainer (ConfigurationLoader <N > configurationLoader , ClassLoader loader , String packageBase , ModuleConstructor constructor , LoggerProxy loggerProxy ) throws QuickStartModuleDiscoveryException {
10596
10697 try {
107- this .config = new SystemConfig <>(configurationLoader );
98+ this .config = new SystemConfig <>(configurationLoader , loggerProxy );
10899 this .constructor = constructor ;
109100 this .classLoader = loader ;
110101 this .packageLocation = packageBase ;
102+ this .loggerProxy = loggerProxy ;
111103
112104 discoverModules ();
113105 } catch (Exception e ) {
@@ -133,7 +125,17 @@ private void discoverModules() throws IOException, QuickStartModuleDiscoveryExce
133125 }
134126
135127 // Put the modules into the discoverer.
136- modules .forEach (modulePopulator );
128+ modules .forEach (s -> {
129+ // If we have a module annotation, we are golden.
130+ if (s .isAnnotationPresent (ModuleData .class )) {
131+ ModuleData md = s .getAnnotation (ModuleData .class );
132+ discoveredModules .put (md .id ().toLowerCase (), new ModuleSpec (s , md ));
133+ } else {
134+ String id = s .getName ().toLowerCase ();
135+ loggerProxy .warn (MessageFormat .format ("The module {0} does not have a ModuleData annotation associated with it. We're just assuming an ID of {0}." , id ));
136+ discoveredModules .put (id , new ModuleSpec (s , id , LoadingStatus .ENABLED , false ));
137+ }
138+ });
137139
138140 // Modules discovered. Create the Module Config adapter.
139141 Map <String , LoadingStatus > m = discoveredModules .entrySet ().stream ().filter (x -> !x .getValue ().isMandatory ())
@@ -149,11 +151,11 @@ private void discoverModules() throws IOException, QuickStartModuleDiscoveryExce
149151 try {
150152 discoveredModules .get (k ).setStatus (v );
151153 } catch (IllegalStateException ex ) {
152- Logger . getLogger ( "QuickStart" ). warning ("A mandatory module can't have its status changed by config. Falling back to FORCELOAD for " + k );
154+ loggerProxy . warn ("A mandatory module can't have its status changed by config. Falling back to FORCELOAD for " + k );
153155 }
154156 });
155157 } catch (ObjectMappingException e ) {
156- Logger . getLogger ( "QuickStart" ). warning ("Could not load modules config, falling back to defaults." );
158+ loggerProxy . warn ("Could not load modules config, falling back to defaults." );
157159 e .printStackTrace ();
158160 }
159161
@@ -246,6 +248,7 @@ public void loadModules(boolean failOnOneError) throws QuickStartModuleLoaderExc
246248 } catch (Exception construction ) {
247249 construction .printStackTrace ();
248250 ms .setPhase (ModulePhase .ERRORED );
251+ loggerProxy .error ("The module " + ms .getModuleClass ().getName () + " failed to construct." );
249252
250253 if (failOnOneError ) {
251254 currentPhase = ConstructionPhase .ERRORED ;
@@ -288,6 +291,7 @@ public void loadModules(boolean failOnOneError) throws QuickStartModuleLoaderExc
288291 construction .printStackTrace ();
289292 modules .remove (s );
290293 ms .setPhase (ModulePhase .ERRORED );
294+ loggerProxy .error ("The module " + ms .getModuleClass ().getName () + " failed to enable." );
291295
292296 if (failOnOneError ) {
293297 currentPhase = ConstructionPhase .ERRORED ;
@@ -333,6 +337,7 @@ public static class Builder {
333337 private String packageToScan ;
334338 private ModuleConstructor constructor ;
335339 private ClassLoader classLoader ;
340+ private LoggerProxy loggerProxy ;
336341
337342 /**
338343 * Sets the {@link ConfigurationLoader} that will handle the module loading.
@@ -379,6 +384,17 @@ public Builder setClassLoader(ClassLoader classLoader) {
379384 return this ;
380385 }
381386
387+ /**
388+ * Sets the {@link LoggerProxy} to use for log messages.
389+ *
390+ * @param loggerProxy The logger proxy to use.
391+ * @return This {@link Builder}, for chaining.
392+ */
393+ public Builder setLoggerProxy (LoggerProxy loggerProxy ) {
394+ this .loggerProxy = loggerProxy ;
395+ return this ;
396+ }
397+
382398 /**
383399 * Builds a {@link ModuleContainer}.
384400 *
@@ -397,8 +413,12 @@ public ModuleContainer build() throws QuickStartModuleDiscoveryException {
397413 classLoader = getClass ().getClassLoader ();
398414 }
399415
400- Metadata .getStartupMessage ().ifPresent (x -> Logger .getLogger ("QuickStart" ).info (x ));
401- return new ModuleContainer (configurationLoader , classLoader , packageToScan , constructor );
416+ if (loggerProxy == null ) {
417+ loggerProxy = DefaultLogger .INSTANCE ;
418+ }
419+
420+ Metadata .getStartupMessage ().ifPresent (x -> loggerProxy .info (x ));
421+ return new ModuleContainer (configurationLoader , classLoader , packageToScan , constructor , loggerProxy );
402422 }
403423 }
404424
0 commit comments