Skip to content

Commit 38ff2cc

Browse files
committed
Refactored the dynamic module loading mechanism
The dynamic module loader no uses its own class loader which can add files from multiple directories. It is based on the standard URLClassLoader. The folder from which modules are loaded dynamically is no longer provided via command line arguments but via a system property. The name of the property is flux.pluginsdir; it expects an absolute path name. The default setting for the module directory has been removed. If the property is not set, no module directory is used. In addition, a second directory whose contents are loaded dynamically, is introduced for loading libraries typically provided by the user such as JDBC drivers or JNDI providers. This directory is configured via the flux.provideddir property. The maven config has been changed to place libraries with scope provided in the provided-libs dir instead of the standard libs dir. These libraries are not added to the class path. As first libary, the scope of the slf4j-log4j dependency has been changed into a provided dependency.
1 parent 80bd5cb commit 38ff2cc

File tree

11 files changed

+215
-168
lines changed

11 files changed

+215
-168
lines changed

pom.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
used to specify that all needed libraries are found under lib/ directory. -->
106106
<classpathPrefix>lib/</classpathPrefix>
107107
<!-- Specifies the main class of the application -->
108-
<mainClass>org.culturegraph.mf.cmdline.Flux</mainClass>
108+
<mainClass>org.culturegraph.mf.runner.Flux</mainClass>
109109
</manifest>
110110
<manifestEntries>
111111
<git-commit>${buildNumber}</git-commit>
@@ -222,22 +222,26 @@
222222
<version>2.0.0-SNAPSHOT</version>
223223
</dependency>
224224

225+
<dependency>
226+
<groupId>org.slf4j</groupId>
227+
<artifactId>slf4j-api</artifactId>
228+
<version>1.7.6</version>
229+
</dependency>
230+
225231
<dependency>
226232
<groupId>org.slf4j</groupId>
227233
<artifactId>slf4j-log4j12</artifactId>
228234
<version>1.7.2</version>
229-
<scope>runtime</scope>
235+
<scope>provided</scope>
230236
</dependency>
231237

232-
233238
<dependency>
234239
<groupId>junit</groupId>
235240
<artifactId>junit</artifactId>
236241
<version>4.11</version>
237242
<scope>test</scope>
238243
</dependency>
239244

240-
241245
</dependencies>
242246

243247
<profiles>

src/main/assembly/assembly.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@
99
<format>zip</format>
1010
</formats>
1111

12-
<!-- Adds dependencies to distribution package under lib directory -->
1312
<dependencySets>
13+
1414
<dependencySet>
1515
<useProjectArtifact>false</useProjectArtifact>
1616
<outputDirectory>lib</outputDirectory>
1717
<unpack>false</unpack>
1818
<directoryMode>0755</directoryMode>
1919
<fileMode>0644</fileMode>
2020
</dependencySet>
21+
22+
<dependencySet>
23+
<scope>provided</scope>
24+
<useProjectArtifact>false</useProjectArtifact>
25+
<outputDirectory>provided-libs</outputDirectory>
26+
<unpack>false</unpack>
27+
<directoryMode>0755</directoryMode>
28+
<fileMode>0644</fileMode>
29+
</dependencySet>
30+
2131
</dependencySets>
2232

2333
<fileSets>

src/main/config/java-options.conf

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# JVM options in this file are passed to the java command.
2-
# Environment variables can be specified by prefixing them
3-
# with $ (e.g. $VAR). The variable $METAFACTURE_HOME refers
4-
# to the folder containing the flux start-up script.
5-
# Undefined variables remain in the configuration.
6-
7-
-Xmx512m
8-
-Dlog4j.configuration="file:///$METAFACTURE_HOME/config/log4j.xml"
9-
10-
# Append additional options defined in the
11-
# environment (The start-up script ensures
12-
# that this variable is always defined):
13-
$FLUX_JAVA_OPTIONS
1+
# JVM options in this file are passed to the java command.
2+
# Environment variables can be specified by prefixing them
3+
# with $ (e.g. $VAR). The variable $METAFACTURE_HOME refers
4+
# to the folder containing the flux start-up script.
5+
# Undefined variables remain in the configuration.
6+
7+
-Xmx512m
8+
-Dlog4j.configuration="file:///$METAFACTURE_HOME/config/log4j.xml"
9+
-Dflux.pluginsdir="$METAFACTURE_HOME/plugins"
10+
-Dflux.provideddir="$METAFACTURE_HOME/provided-libs"
11+
12+
# Append additional options defined in the
13+
# environment (The start-up script ensures
14+
# that this variable is always defined):
15+
$FLUX_JAVA_OPTIONS

src/main/config/log4j.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
</layout>
1010
</appender>
1111

12-
1312
<root>
1413
<priority value="info" />
1514
<appender-ref ref="stdout" />
1615
</root>
1716

17+
<logger name="org.culturegraph.mf.runner.util.DirectoryClassLoader">
18+
<priority value="warning" />
19+
</logger>
20+
1821
</log4j:configuration>

src/main/java/org/culturegraph/mf/cmdline/Flux.java

Lines changed: 0 additions & 147 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2013 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.runner;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
24+
25+
import org.antlr.runtime.RecognitionException;
26+
import org.culturegraph.mf.flux.FluxCompiler;
27+
import org.culturegraph.mf.flux.parser.FluxProgramm;
28+
import org.culturegraph.mf.runner.util.DirectoryClassLoader;
29+
import org.culturegraph.mf.util.ResourceUtil;
30+
31+
/**
32+
* @author Markus Michael Geipel
33+
* @author Christoph Böhme
34+
*
35+
*/
36+
public final class Flux {
37+
38+
public static final String PLUGINS_DIR_PROPERTY = "flux.pluginsdir";
39+
public static final String PROVIDED_DIR_PROPERTY = "flux.provideddir";
40+
41+
private static final Pattern VAR_PATTERN = Pattern.compile("([^=]*)=(.*)");
42+
private static final String SCRIPT_HOME = "FLUX_DIR";
43+
44+
private Flux() {
45+
// No instances allowed
46+
}
47+
48+
public static void main(final String[] args) throws IOException, RecognitionException {
49+
50+
loadCustomJars();
51+
52+
if (args.length < (1)) {
53+
FluxProgramm.printHelp(System.out);
54+
System.exit(2);
55+
} else {
56+
57+
final File fluxFile = new File(args[0]);
58+
if (!fluxFile.exists()) {
59+
System.err.println("File not found: " + args[0]);
60+
System.exit(1);
61+
return;
62+
}
63+
64+
// get variable assignments
65+
final Map<String, String> vars = new HashMap<String, String>();
66+
vars.put(SCRIPT_HOME, fluxFile.getAbsoluteFile().getParent()
67+
+ System.getProperty("file.separator"));
68+
69+
for (int i = 1; i < args.length; ++i) {
70+
final Matcher matcher = VAR_PATTERN.matcher(args[i]);
71+
if (!matcher.find()) {
72+
FluxProgramm.printHelp(System.err);
73+
return;
74+
}
75+
vars.put(matcher.group(1), matcher.group(2));
76+
}
77+
78+
// run parser and builder
79+
FluxCompiler.compile(ResourceUtil.getStream(fluxFile), vars).start();
80+
}
81+
}
82+
83+
private static void loadCustomJars() {
84+
final DirectoryClassLoader dirClassLoader = new DirectoryClassLoader(getClassLoader());
85+
86+
final String pluginsDir = System.getProperty(PLUGINS_DIR_PROPERTY);
87+
if (pluginsDir != null) {
88+
dirClassLoader.addDirectory(new File(pluginsDir));
89+
}
90+
final String providedDir = System.getProperty(PROVIDED_DIR_PROPERTY);
91+
if (providedDir != null) {
92+
dirClassLoader.addDirectory(new File(providedDir));
93+
}
94+
95+
setClassLoader(dirClassLoader);
96+
}
97+
98+
private static ClassLoader getClassLoader() {
99+
return Thread.currentThread().getContextClassLoader();
100+
}
101+
102+
private static void setClassLoader(final ClassLoader classLoader) {
103+
Thread.currentThread().setContextClassLoader(classLoader);
104+
}
105+
106+
}

src/main/java/org/culturegraph/mf/cmdline/MorphVis.java renamed to src/main/java/org/culturegraph/mf/runner/MorphVis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.culturegraph.mf.cmdline;
16+
package org.culturegraph.mf.runner;
1717

1818
import java.io.File;
1919
import java.io.FileOutputStream;

0 commit comments

Comments
 (0)