Skip to content

Commit aedc3ca

Browse files
maarztctrueden
authored andcommitted
Add LoggerPreprocessor to inject Logger instances
This PreprocessorPlugin affects modules with a single Parameter of type Logger. It will get a logger from LogService, named like the module's class, and assign it to the Parameter.
1 parent 555174d commit aedc3ca

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.module.process;
33+
34+
import org.scijava.log.LogService;
35+
import org.scijava.log.Logger;
36+
import org.scijava.module.Module;
37+
import org.scijava.module.ModuleItem;
38+
import org.scijava.module.ModuleService;
39+
import org.scijava.plugin.Parameter;
40+
import org.scijava.plugin.Plugin;
41+
42+
/**
43+
* This {@link PreprocessorPlugin} affects {@link Module}s with a single
44+
* {@link Parameter} of type {@link Logger}. It will assign a Logger to that
45+
* Parameter, that is named like the modules class.
46+
*
47+
* @author Matthias Arzt
48+
*/
49+
@Plugin(type = PreprocessorPlugin.class)
50+
public class LoggerPreprocessor extends AbstractPreprocessorPlugin {
51+
52+
@Parameter(required = false)
53+
private LogService logService;
54+
55+
@Parameter(required = false)
56+
private ModuleService moduleService;
57+
58+
// -- ModuleProcessor methods --
59+
60+
@Override
61+
public void process(final Module module) {
62+
if (logService == null || moduleService == null) return;
63+
64+
final ModuleItem<?> loggerInput = moduleService.getSingleInput(module,
65+
Logger.class);
66+
if (loggerInput == null || !loggerInput.isAutoFill()) return;
67+
68+
final String name = loggerInput.getName();
69+
module.setInput(name, logService.subLogger(module.getDelegateObject()
70+
.getClass().getSimpleName()));
71+
module.resolveInput(name);
72+
}
73+
74+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.module.process;
33+
34+
import static org.junit.Assert.assertTrue;
35+
36+
import java.util.concurrent.ExecutionException;
37+
38+
import org.junit.Test;
39+
import org.scijava.Context;
40+
import org.scijava.command.Command;
41+
import org.scijava.command.CommandModule;
42+
import org.scijava.command.CommandService;
43+
import org.scijava.log.LogService;
44+
import org.scijava.log.Logger;
45+
import org.scijava.log.TestLogListener;
46+
import org.scijava.plugin.Parameter;
47+
import org.scijava.plugin.Plugin;
48+
49+
/**
50+
* Tests {@link LoggerPreprocessor}.
51+
*
52+
* @author Matthias Arzt
53+
*/
54+
public class LoggerPreprocessorTest {
55+
56+
@Test
57+
public void testInjection() throws InterruptedException, ExecutionException {
58+
final Context context = new Context(CommandService.class);
59+
final CommandService commandService = context.service(CommandService.class);
60+
final LogService logService = context.service(LogService.class);
61+
final TestLogListener listener = new TestLogListener();
62+
logService.addListener(listener);
63+
64+
final CommandModule module = //
65+
commandService.run(CommandWithLogger.class, true).get();
66+
assertTrue(listener.hasLogged(m -> m.source().path().contains(CommandWithLogger.class.getSimpleName())));
67+
}
68+
69+
@Plugin(type = Command.class)
70+
public static class CommandWithLogger implements Command {
71+
72+
@Parameter
73+
public Logger log;
74+
75+
@Override
76+
public void run() {
77+
log.info("log from the command.");
78+
}
79+
}
80+
81+
}

0 commit comments

Comments
 (0)