Skip to content

Commit 00146ea

Browse files
author
mgeipel
committed
fixed #102
1 parent 2b6de58 commit 00146ea

File tree

1 file changed

+24
-70
lines changed

1 file changed

+24
-70
lines changed

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

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.io.File;
1919
import java.io.FilenameFilter;
2020
import java.io.IOException;
21-
import java.net.MalformedURLException;
22-
import java.net.URISyntaxException;
2321
import java.net.URL;
2422
import java.net.URLClassLoader;
2523
import java.util.HashMap;
@@ -30,25 +28,19 @@
3028
import java.util.regex.Pattern;
3129

3230
import org.antlr.runtime.RecognitionException;
33-
import org.culturegraph.mf.flux.Flow;
3431
import org.culturegraph.mf.flux.FluxCompiler;
32+
import org.culturegraph.mf.flux.parser.FluxProgramm;
3533
import org.culturegraph.mf.util.ResourceUtil;
3634

3735
/**
3836
* @author Markus Michael Geipel
39-
*
37+
*
4038
*/
4139
public final class Flux {
4240
public static final String MODULES_DIR = "modules";
43-
44-
private static final String JAR_FILE_EXTENSION = ".jar";
45-
private static final String CLASS_FILE_EXTENSION = ".class";
46-
4741
private static final Pattern VAR_PATTERN = Pattern.compile("([^=]*)=(.*)");
4842
private static final String SCRIPT_HOME = "FLUX_DIR";
4943

50-
private static final String MODULES_DIR_ARG = "-modules=";
51-
5244
private Flux() {
5345
// no instances
5446
}
@@ -57,27 +49,34 @@ private Flux() {
5749
* @param args
5850
* @throws IOException
5951
* @throws RecognitionException
60-
* @throws URISyntaxException
6152
*/
6253
public static void main(final String[] args) throws IOException, RecognitionException {
6354

64-
loadModules(getModulesDir(args));
65-
66-
final int fileArg;
67-
if (args.length > 0 && args[0].startsWith(MODULES_DIR_ARG)) {
68-
fileArg = 1;
69-
} else {
70-
fileArg = 0;
55+
final File modulesDir = new File(MODULES_DIR);
56+
if (modulesDir.exists()) {
57+
final FilenameFilter filter = new FilenameFilter() {
58+
@Override
59+
public boolean accept(final File dir, final String name) {
60+
return name.endsWith(".jar") || name.endsWith(".class");
61+
}
62+
};
63+
final List<URL> moduleURLs = new LinkedList<URL>();
64+
for (File file : modulesDir.listFiles(filter)) {
65+
moduleURLs.add(file.getAbsoluteFile().toURI().toURL());
66+
}
67+
final URLClassLoader moduleLoader = new URLClassLoader(moduleURLs.toArray(new URL[0]), Thread
68+
.currentThread().getContextClassLoader());
69+
Thread.currentThread().setContextClassLoader(moduleLoader);
7170
}
7271

73-
if (args.length < (fileArg + 1)) {
74-
Flow.printHelp(System.out);
72+
if (args.length < 1) {
73+
FluxProgramm.printHelp(System.out);
7574
System.exit(2);
7675
} else {
7776

78-
final File fluxFile = new File(args[fileArg]);
77+
final File fluxFile = new File(args[0]);
7978
if (!fluxFile.exists()) {
80-
System.err.println("File not found: " + args[fileArg]);
79+
System.err.println("File not found: " + args[0]);
8180
System.exit(1);
8281
return;
8382
}
@@ -86,65 +85,20 @@ public static void main(final String[] args) throws IOException, RecognitionExce
8685
final Map<String, String> vars = new HashMap<String, String>();
8786
vars.put(SCRIPT_HOME, fluxFile.getAbsoluteFile().getParent() + System.getProperty("file.separator"));
8887

89-
for (int i = fileArg + 1; i < args.length; ++i) {
90-
if (args[i].startsWith(MODULES_DIR_ARG)) {
91-
continue;
92-
}
88+
for (int i = 1; i < args.length; ++i) {
9389
final Matcher matcher = VAR_PATTERN.matcher(args[i]);
9490
if (!matcher.find()) {
95-
Flow.printHelp(System.err);
91+
FluxProgramm.printHelp(System.err);
9692
return;
9793
}
9894
vars.put(matcher.group(1), matcher.group(2));
9995
}
10096

10197
// run parser and builder
102-
final List<Flow> flows = FluxCompiler.compile(ResourceUtil.getStream(fluxFile), vars);
103-
for (final Flow flow : flows) {
104-
flow.start();
105-
}
106-
}
107-
}
108-
109-
private static File getModulesDir(final String[] args) {
110-
File modulesDir = new File(MODULES_DIR);
111-
112-
File programDir = null;
113-
try {
114-
programDir = new File(Flux.class.getProtectionDomain().getCodeSource().getLocation().toURI());
115-
} catch (final URISyntaxException e) {
116-
// Ignore the programDir, if it is not available
117-
}
118-
if (programDir != null) {
119-
if (programDir.getName().endsWith(JAR_FILE_EXTENSION)) {
120-
programDir = programDir.getParentFile();
121-
}
122-
modulesDir = new File(programDir, MODULES_DIR);
123-
}
98+
FluxCompiler.compile(ResourceUtil.getStream(fluxFile), vars).start();
12499

125-
if (args.length > 0 && args[0].startsWith(MODULES_DIR_ARG)) {
126-
modulesDir = new File(args[0].substring(MODULES_DIR_ARG.length()));
127100
}
128-
129-
return modulesDir;
130101
}
131102

132-
private static void loadModules(final File modulesDir) throws MalformedURLException {
133-
if (modulesDir.exists()) {
134-
final FilenameFilter filter = new FilenameFilter() {
135-
@Override
136-
public boolean accept(final File dir, final String name) {
137-
return name.endsWith(JAR_FILE_EXTENSION) || name.endsWith(CLASS_FILE_EXTENSION);
138-
}
139-
};
140-
final List<URL> moduleURLs = new LinkedList<URL>();
141-
for (final File file : modulesDir.listFiles(filter)) {
142-
moduleURLs.add(file.getAbsoluteFile().toURI().toURL());
143-
}
144-
final URLClassLoader moduleLoader = new URLClassLoader(moduleURLs.toArray(new URL[0]), Thread
145-
.currentThread().getContextClassLoader());
146-
Thread.currentThread().setContextClassLoader(moduleLoader);
147-
}
148-
}
149103

150104
}

0 commit comments

Comments
 (0)