Skip to content

Commit 7164274

Browse files
author
Dev-Bjorn
committed
V1.1.0: Custom logger option added + vulnerability fix
1 parent f84e220 commit 7164274

File tree

11 files changed

+340
-48
lines changed

11 files changed

+340
-48
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Add this to your `pom.xml`.
1717
<dependency>
1818
<groupId>nl.devoxist</groupId>
1919
<artifactId>module-scheduler</artifactId>
20-
<version>1.0.0</version>
20+
<version>1.1.0</version>
2121
</dependency>
2222
```
2323

@@ -122,6 +122,27 @@ order `ModuleScheduler#beforeModuleExecute` -> `Module#onExecute` -> `ModuleSche
122122
new Scheduler(new ModuleSchedulerProcess());
123123
```
124124

125+
#### Module Scheduler Settings
126+
127+
#### Modules
128+
129+
The modules which are going to be loaded can be added through the `ModuleSchedulerSettings#addModule` method. The
130+
modules can be added through their class path or their `Class`.
131+
132+
#### Input Registers
133+
134+
The registers that are used in to construct the modules. These modules need to have a constructor with the parameters
135+
types registered in the input registries and/or can be the modules that are loaded. These registries can be registered
136+
through the `ModuleSchedulerSettings#addRegisters` method. Note: The modules that are getting loaded do not have to be
137+
registered in the input registers.
138+
139+
#### Output Register
140+
141+
This is a register that contains the constructed modules that are loaded inside the loading process.
142+
143+
#### Logger
144+
145+
This is the logger of the process that is going to be used to output the messages of process.
125146

126147
### Contributors
127148

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<groupId>nl.devoxist</groupId>
3030
<artifactId>module-scheduler</artifactId>
3131
<packaging>jar</packaging>
32-
<version>1.0.0</version>
32+
<version>1.1.0</version>
3333

3434
<name>Module Scheduler</name>
3535
<description>An API to order tasks in the correct order</description>

src/main/java/nl/devoxist/modulescheduler/Scheduler.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,20 @@ public Scheduler(ModuleScheduler moduleScheduler) {
8989
* @since 1.0.0
9090
*/
9191
private void run() {
92-
this.moduleScheduler.updateSettings(this.moduleSchedulerSettings);
92+
try {
93+
this.moduleScheduler.updateSettings(this.moduleSchedulerSettings);
9394

94-
Set<Class<? extends Module>> modules = this.moduleSchedulerSettings.getModules();
95-
Map<Class<? extends Module>, ModuleInformation<?>>
96-
moduleInformationSet = DependencyResolver.resolveDependencies(modules);
95+
Set<Class<? extends Module>> modules = this.moduleSchedulerSettings.getModules();
96+
Map<Class<? extends Module>, ModuleInformation<?>>
97+
moduleInformationSet = DependencyResolver.resolveDependencies(modules);
9798

98-
this.moduleSchedulerInformation.setModuleInformationMap(moduleInformationSet);
99-
Set<Stage> stages = Staging.stageModules(this.moduleSchedulerInformation);
99+
this.moduleSchedulerInformation.setModuleInformationMap(moduleInformationSet);
100100

101-
StageRunner.runStages(this.moduleSchedulerSettings, this.moduleScheduler, stages);
101+
Set<Stage> stages = Staging.stageModules(this.moduleSchedulerInformation);
102+
103+
StageRunner.runStages(this.moduleSchedulerSettings, this.moduleScheduler, stages);
104+
} catch (InterruptedException ignored) {
105+
}
102106
}
103107

104108
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2023 Devoxist, Dev-Bjorn
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package nl.devoxist.modulescheduler.console;
24+
25+
import java.util.logging.LogRecord;
26+
27+
/**
28+
* {@link Formatter} is used for the formatting of the logging in the console and is a subclass of
29+
* {@link java.util.logging.Formatter}.
30+
*
31+
* @author Dev-Bjorn
32+
* @version 1.0.0
33+
* @see Formatter
34+
* @since 1.1.0
35+
*/
36+
public class Formatter extends java.util.logging.Formatter {
37+
38+
/**
39+
* Format the given log record and return the formatted string.
40+
* <p>
41+
* The resulting formatted String will normally include a
42+
* localized and formatted version of the LogRecord's message field.
43+
* It is recommended to use the {@link Formatter#formatMessage}
44+
* convenience method to localize and format the message field.
45+
*
46+
* @param record the log record to be formatted.
47+
*
48+
* @return the formatted log record
49+
*
50+
* @since 1.1.0
51+
*/
52+
@Override
53+
public String format(LogRecord record) {
54+
return record.getMessage() + Console.NEWLINE;
55+
}
56+
}

src/main/java/nl/devoxist/modulescheduler/path/PathCyclePrinter.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222

2323
package nl.devoxist.modulescheduler.path;
2424

25+
import nl.devoxist.modulescheduler.ModuleScheduler;
2526
import nl.devoxist.modulescheduler.console.Console;
27+
import nl.devoxist.modulescheduler.settings.ModuleSchedulerInformation;
2628

2729
import java.util.concurrent.CompletableFuture;
2830
import java.util.concurrent.ExecutionException;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
2933

3034
/**
3135
* The process of path resolver and printing of the {@link Path}. This is meant to be for to showcase the cycle
3236
* dependencies.
3337
*
3438
* @author Dev-Bjorn
35-
* @version 1.0.0
39+
* @version 1.1.0
3640
* @since 1.0.0
3741
*/
3842
public class PathCyclePrinter extends Thread {
@@ -42,6 +46,12 @@ public class PathCyclePrinter extends Thread {
4246
* @since 1.0.0
4347
*/
4448
private final Path firstPath;
49+
/**
50+
* The information of the current running {@link ModuleScheduler}
51+
*
52+
* @since 1.1.0
53+
*/
54+
private final ModuleSchedulerInformation moduleSchedulerInformation;
4555
/**
4656
* The current path of the resolver.
4757
*
@@ -64,11 +74,13 @@ public class PathCyclePrinter extends Thread {
6474
/**
6575
* Construct a {@link PathCyclePrinter} object that is responsible for the printing of a path cycle.
6676
*
67-
* @param path The path that needs to be printed.
77+
* @param moduleSchedulerInformation The information of the current running {@link ModuleScheduler}
78+
* @param path The path that needs to be printed.
6879
*
6980
* @since 1.0.0
7081
*/
71-
PathCyclePrinter(Path path) {
82+
PathCyclePrinter(ModuleSchedulerInformation moduleSchedulerInformation, Path path) {
83+
this.moduleSchedulerInformation = moduleSchedulerInformation;
7284
this.path = path;
7385
this.firstPath = path;
7486

@@ -78,12 +90,13 @@ public class PathCyclePrinter extends Thread {
7890
/**
7991
* Construct and print the cycle of {@link Path}.
8092
*
81-
* @param path The path that needs to be printed.
93+
* @param moduleSchedulerInformation The information of the current running {@link ModuleScheduler}
94+
* @param path The path that needs to be printed.
8295
*
8396
* @since 1.0.0
8497
*/
85-
public static void printPath(Path path) {
86-
PathCyclePrinter builder = new PathCyclePrinter(path);
98+
public static void printPath(ModuleSchedulerInformation moduleSchedulerInformation, Path path) {
99+
PathCyclePrinter builder = new PathCyclePrinter(moduleSchedulerInformation, path);
87100
builder.start();
88101
builder.print();
89102
}
@@ -98,8 +111,7 @@ public static void printPath(Path path) {
98111
public void run() {
99112
StringBuilder builder = new StringBuilder();
100113

101-
builder.append(Console.NEWLINE)
102-
.append("[WARN] Dependency cycle detected: ");
114+
builder.append("[WARN] Dependency cycle detected: ");
103115

104116
while (path.hasPreviousPath()) {
105117
builder.append(Console.NEWLINE).append("[WARN] -> %s".formatted(path.getCls().getName()));
@@ -130,7 +142,9 @@ public void print() {
130142
} catch (InterruptedException | ExecutionException ignored) {
131143
return;
132144
}
133-
this.completableFuture.thenRun(() -> System.out.println(value)).thenRun(() -> System.exit(0));
145+
146+
Logger logger = this.moduleSchedulerInformation.getModuleSchedulerSettings().getLogger();
147+
this.completableFuture.thenRun(() -> logger.log(Level.WARNING, value));
134148
}
135149

136150
}

src/main/java/nl/devoxist/modulescheduler/settings/ModuleSchedulerSettings.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
package nl.devoxist.modulescheduler.settings;
2424

2525
import nl.devoxist.modulescheduler.Module;
26+
import nl.devoxist.modulescheduler.ModuleScheduler;
27+
import nl.devoxist.modulescheduler.console.Formatter;
2628
import nl.devoxist.modulescheduler.exception.ModuleException;
2729
import nl.devoxist.typeresolver.register.Register;
2830

2931
import java.util.*;
32+
import java.util.logging.ConsoleHandler;
33+
import java.util.logging.Logger;
3034

3135
/**
3236
* This object gives the options to manipulate the process.
@@ -55,6 +59,25 @@ public class ModuleSchedulerSettings {
5559
* @since 1.0.0
5660
*/
5761
private Register outputRegister = new Register();
62+
/**
63+
* The {@link Logger} of the current running {@link ModuleScheduler}.
64+
*
65+
* @see Logger
66+
* @since 1.1.0
67+
*/
68+
private Logger logger;
69+
70+
{
71+
logger = Logger.getAnonymousLogger();
72+
73+
logger.setUseParentHandlers(false);
74+
75+
ConsoleHandler consoleHandler = new ConsoleHandler();
76+
consoleHandler.setFormatter(new Formatter());
77+
78+
logger.addHandler(consoleHandler);
79+
80+
}
5881

5982
/**
6083
* Add a {@link Module} to the load process. If the module is added to the process it will be used to order the
@@ -76,7 +99,7 @@ public void addModule(Class<? extends Module> moduleCls) {
7699
* class and where that class has been implemented with {@link Module}.
77100
*
78101
* @throws ClassNotFoundException If the class was not found
79-
* @throws ModuleException If the class, that is retrieved of the path, is not extended with {@link Module}.
102+
* @throws ModuleException If the class, that is retrieved by its path, is not extended with {@link Module}.
80103
* @since 1.0.0
81104
*/
82105
public void addModule(String modulePath) throws ClassNotFoundException {
@@ -94,7 +117,7 @@ public void addModule(String modulePath) throws ClassNotFoundException {
94117
/**
95118
* Get the {@link Module}s that needs to in the load process.
96119
*
97-
* @return The {@link Module}s that needs to be in the load process.
120+
* @return An unmodifiable {@link Set} of the {@link Module}s that needs to be in the load process.
98121
*
99122
* @since 1.0.0
100123
*/
@@ -107,6 +130,7 @@ public Set<Class<? extends Module>> getModules() {
107130
*
108131
* @param registers The registers that will be added to the {@link Set} of {@link Register}s.
109132
*
133+
* @see Register
110134
* @since 1.0.0
111135
*/
112136
public void addRegisters(Register... registers) {
@@ -116,8 +140,9 @@ public void addRegisters(Register... registers) {
116140
/**
117141
* Get the {@link Set} of registries that are used to construct the {@link Module}.
118142
*
119-
* @return The {@link Set} of registries that are used to construct the {@link Module}.
143+
* @return An unmodifiable {@link Set} of registries that are used to construct the {@link Module}.
120144
*
145+
* @see Register
121146
* @since 1.0.0
122147
*/
123148
public Set<Register> getRegistries() {
@@ -130,6 +155,7 @@ public Set<Register> getRegistries() {
130155
*
131156
* @param outputRegister The output registry of the modules that are getting loaded.
132157
*
158+
* @see Register
133159
* @since 1.0.0
134160
*/
135161
public void setOutputRegister(Register outputRegister) {
@@ -140,14 +166,39 @@ public void setOutputRegister(Register outputRegister) {
140166
* Get the output registery of the module scheduler. This will contain all the loaded modules after the
141167
* modules are loaded.
142168
*
143-
* @return The output registery of the module scheduler.
169+
* @return The cloned output registery of the module scheduler.
144170
*
171+
* @see Register
145172
* @since 1.0.0
146173
*/
147174
public Register getOutputRegister() {
148175
return this.outputRegister.clone();
149176
}
150177

178+
/**
179+
* Set the {@link Logger} of the current running {@link ModuleScheduler}.
180+
*
181+
* @param logger The {@link Logger} of the current running {@link ModuleScheduler}
182+
*
183+
* @see Logger
184+
* @since 1.1.0
185+
*/
186+
public void setLogger(Logger logger) {
187+
this.logger = logger;
188+
}
189+
190+
/**
191+
* Get the {@link Logger} of the current running {@link ModuleScheduler}.
192+
*
193+
* @return The {@link Logger} of the current running {@link ModuleScheduler}
194+
*
195+
* @see Logger
196+
* @since 1.1.0
197+
*/
198+
public Logger getLogger() {
199+
return logger;
200+
}
201+
151202
/**
152203
* Creates and returns a copy of this object. The precise meaning
153204
* of "copy" may depend on the class of the object. The general

0 commit comments

Comments
 (0)