18
18
import java .io .File ;
19
19
import java .io .FilenameFilter ;
20
20
import java .io .IOException ;
21
+ import java .net .MalformedURLException ;
22
+ import java .net .URISyntaxException ;
21
23
import java .net .URL ;
22
24
import java .net .URLClassLoader ;
23
25
import java .util .HashMap ;
34
36
35
37
/**
36
38
* @author Markus Michael Geipel
37
- *
39
+ *
38
40
*/
39
41
public final class Flux {
40
42
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
+
41
47
private static final Pattern VAR_PATTERN = Pattern .compile ("([^=]*)=(.*)" );
42
48
private static final String SCRIPT_HOME = "FLUX_DIR" ;
43
49
50
+ private static final String MODULES_DIR_ARG = "-modules=" ;
51
+
44
52
private Flux () {
45
53
// no instances
46
54
}
@@ -49,34 +57,27 @@ private Flux() {
49
57
* @param args
50
58
* @throws IOException
51
59
* @throws RecognitionException
60
+ * @throws URISyntaxException
52
61
*/
53
62
public static void main (final String [] args ) throws IOException , RecognitionException {
54
63
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 );
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 ;
70
71
}
71
72
72
- if (args .length < 1 ) {
73
+ if (args .length < ( fileArg + 1 ) ) {
73
74
FluxProgramm .printHelp (System .out );
74
75
System .exit (2 );
75
76
} else {
76
77
77
- final File fluxFile = new File (args [0 ]);
78
+ final File fluxFile = new File (args [fileArg ]);
78
79
if (!fluxFile .exists ()) {
79
- System .err .println ("File not found: " + args [0 ]);
80
+ System .err .println ("File not found: " + args [fileArg ]);
80
81
System .exit (1 );
81
82
return ;
82
83
}
@@ -85,7 +86,10 @@ public boolean accept(final File dir, final String name) {
85
86
final Map <String , String > vars = new HashMap <String , String >();
86
87
vars .put (SCRIPT_HOME , fluxFile .getAbsoluteFile ().getParent () + System .getProperty ("file.separator" ));
87
88
88
- for (int i = 1 ; i < args .length ; ++i ) {
89
+ for (int i = fileArg + 1 ; i < args .length ; ++i ) {
90
+ if (args [i ].startsWith (MODULES_DIR_ARG )) {
91
+ continue ;
92
+ }
89
93
final Matcher matcher = VAR_PATTERN .matcher (args [i ]);
90
94
if (!matcher .find ()) {
91
95
FluxProgramm .printHelp (System .err );
@@ -96,9 +100,48 @@ public boolean accept(final File dir, final String name) {
96
100
97
101
// run parser and builder
98
102
FluxCompiler .compile (ResourceUtil .getStream (fluxFile ), vars ).start ();
103
+ }
104
+ }
99
105
106
+ private static File getModulesDir (final String [] args ) {
107
+ File modulesDir = new File (MODULES_DIR );
108
+
109
+ File programDir = null ;
110
+ try {
111
+ programDir = new File (Flux .class .getProtectionDomain ().getCodeSource ().getLocation ().toURI ());
112
+ } catch (final URISyntaxException e ) {
113
+ // Ignore the programDir, if it is not available
114
+ }
115
+ if (programDir != null ) {
116
+ if (programDir .getName ().endsWith (JAR_FILE_EXTENSION )) {
117
+ programDir = programDir .getParentFile ();
118
+ }
119
+ modulesDir = new File (programDir , MODULES_DIR );
120
+ }
121
+
122
+ if (args .length > 0 && args [0 ].startsWith (MODULES_DIR_ARG )) {
123
+ modulesDir = new File (args [0 ].substring (MODULES_DIR_ARG .length ()));
100
124
}
125
+
126
+ return modulesDir ;
101
127
}
102
128
129
+ private static void loadModules (final File modulesDir ) throws MalformedURLException {
130
+ if (modulesDir .exists ()) {
131
+ final FilenameFilter filter = new FilenameFilter () {
132
+ @ Override
133
+ public boolean accept (final File dir , final String name ) {
134
+ return name .endsWith (JAR_FILE_EXTENSION ) || name .endsWith (CLASS_FILE_EXTENSION );
135
+ }
136
+ };
137
+ final List <URL > moduleURLs = new LinkedList <URL >();
138
+ for (final File file : modulesDir .listFiles (filter )) {
139
+ moduleURLs .add (file .getAbsoluteFile ().toURI ().toURL ());
140
+ }
141
+ final URLClassLoader moduleLoader = new URLClassLoader (moduleURLs .toArray (new URL [0 ]), Thread
142
+ .currentThread ().getContextClassLoader ());
143
+ Thread .currentThread ().setContextClassLoader (moduleLoader );
144
+ }
145
+ }
103
146
104
147
}
0 commit comments