Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit 2d7e2fe

Browse files
committed
Add LoggerProxy
1 parent 7108823 commit 2d7e2fe

File tree

6 files changed

+99
-27
lines changed

6 files changed

+99
-27
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart;
6+
7+
import java.util.logging.Logger;
8+
9+
public class DefaultLogger implements LoggerProxy {
10+
11+
public final static DefaultLogger INSTANCE = new DefaultLogger();
12+
13+
private DefaultLogger() {}
14+
15+
@Override
16+
public void info(String message) {
17+
Logger.getLogger("QuickStart").info(message);
18+
}
19+
20+
@Override
21+
public void warn(String message) {
22+
Logger.getLogger("QuickStart").warning(message);
23+
}
24+
25+
@Override
26+
public void error(String message) {
27+
Logger.getLogger("QuickStart").severe(message);
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This file is part of QuickStart Module Loader, licensed under the MIT License (MIT). See the LICENSE.txt file
3+
* at the root of this project for more details.
4+
*/
5+
package uk.co.drnaylor.quickstart;
6+
7+
/**
8+
* An interface that allows logging to occur with other frameworks.
9+
*/
10+
public interface LoggerProxy {
11+
12+
void info(String message);
13+
14+
void warn(String message);
15+
16+
void error(String message);
17+
}

src/main/java/uk/co/drnaylor/quickstart/ModuleContainer.java

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Optional;
2727
import java.util.Set;
28-
import java.util.function.Consumer;
2928
import java.util.function.Predicate;
30-
import java.util.logging.Logger;
3129
import 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

src/main/java/uk/co/drnaylor/quickstart/SystemConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@
2222
public final class SystemConfig<N extends ConfigurationNode, T extends ConfigurationLoader<N>> extends AbstractAdaptableConfig<N, T> {
2323

2424
private final String modulesNode = "modules";
25+
private final LoggerProxy proxy;
2526
private ModulesConfigAdapter configAdapter;
2627

27-
SystemConfig(T loader) throws IOException {
28+
SystemConfig(T loader, LoggerProxy proxy) throws IOException {
2829
super(loader);
30+
this.proxy = proxy;
2931
}
3032

3133
void attachModulesConfig(Map<String, LoadingStatus> defaults) throws IOException {
3234
Preconditions.checkNotNull(defaults);
3335
Preconditions.checkState(configAdapter == null);
3436

35-
configAdapter = new ModulesConfigAdapter(new HashMap<>(defaults));
37+
HashMap<String, LoadingStatus> h = new HashMap<>(defaults);
38+
configAdapter = new ModulesConfigAdapter(h, proxy);
3639
this.attachConfigAdapter(modulesNode, configAdapter);
3740
}
3841

src/main/java/uk/co/drnaylor/quickstart/config/ModulesConfigAdapter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import com.google.common.reflect.TypeToken;
99
import ninja.leaping.configurate.ConfigurationNode;
1010
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
11+
import uk.co.drnaylor.quickstart.LoggerProxy;
1112
import uk.co.drnaylor.quickstart.enums.LoadingStatus;
1213

1314
import java.util.HashMap;
14-
import java.util.logging.Logger;
1515

1616
/**
1717
* Configuration adapter that handles the module statuses.
@@ -22,9 +22,11 @@ public final class ModulesConfigAdapter extends AbstractConfigAdapter<HashMap<St
2222

2323
private final TypeToken<HashMap<String, LoadingStatus>> tt = new TypeToken<HashMap<String, LoadingStatus>>() {};
2424
private final HashMap<String, LoadingStatus> defaults;
25+
private final LoggerProxy proxy;
2526

26-
public ModulesConfigAdapter(HashMap<String, LoadingStatus> defaults) {
27+
public ModulesConfigAdapter(HashMap<String, LoadingStatus> defaults, LoggerProxy proxy) {
2728
this.defaults = defaults;
29+
this.proxy = proxy;
2830
}
2931

3032
@Override
@@ -43,7 +45,7 @@ protected HashMap<String, LoadingStatus> convertFromConfigurateNode(Configuratio
4345
try {
4446
value = node.getValue(new TypeToken<HashMap<String, LoadingStatus>>() {});
4547
} catch (ObjectMappingException e) {
46-
Logger.getLogger("QuickStart").warning(e.getMessage());
48+
proxy.warn(e.getMessage());
4749
}
4850

4951
if (value == null) {

src/test/java/uk/co/drnaylor/quickstart/tests/tests/ModuleConfigurationAdapterTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Assert;
1212
import org.junit.Before;
1313
import org.junit.Test;
14+
import uk.co.drnaylor.quickstart.DefaultLogger;
1415
import uk.co.drnaylor.quickstart.SystemConfig;
1516
import uk.co.drnaylor.quickstart.config.ModulesConfigAdapter;
1617
import uk.co.drnaylor.quickstart.enums.LoadingStatus;
@@ -33,13 +34,13 @@ public void beforeTests() throws Exception {
3334

3435
Constructor<?> ctor = SystemConfig.class.getDeclaredConstructors()[0];
3536
ctor.setAccessible(true);
36-
config = (SystemConfig<ConfigurationNode, ConfigurationLoader<ConfigurationNode>>) ctor.newInstance(loader);
37+
config = (SystemConfig<ConfigurationNode, ConfigurationLoader<ConfigurationNode>>) ctor.newInstance(loader, DefaultLogger.INSTANCE);
3738

3839
HashMap<String, LoadingStatus> m = Maps.newHashMap();
3940
m.put("d", LoadingStatus.DISABLED);
4041
m.put("e", LoadingStatus.ENABLED);
4142
m.put("f", LoadingStatus.FORCELOAD);
42-
config.attachConfigAdapter(ModulesConfigAdapter.modulesKey, new ModulesConfigAdapter(m));
43+
config.attachConfigAdapter(ModulesConfigAdapter.modulesKey, new ModulesConfigAdapter(m, DefaultLogger.INSTANCE));
4344
}
4445

4546
@Test

0 commit comments

Comments
 (0)