1818import java .io .File ;
1919import java .io .FilenameFilter ;
2020import java .io .IOException ;
21+ import java .net .MalformedURLException ;
22+ import java .net .URISyntaxException ;
2123import java .net .URL ;
2224import java .net .URLClassLoader ;
2325import java .util .HashMap ;
3436
3537/**
3638 * @author Markus Michael Geipel
37- *
39+ *
3840 */
3941public final class Flux {
4042 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+
4147 private static final Pattern VAR_PATTERN = Pattern .compile ("([^=]*)=(.*)" );
4248 private static final String SCRIPT_HOME = "FLUX_DIR" ;
4349
50+ private static final String MODULES_DIR_ARG = "-modules=" ;
51+
4452 private Flux () {
4553 // no instances
4654 }
@@ -49,34 +57,27 @@ private Flux() {
4957 * @param args
5058 * @throws IOException
5159 * @throws RecognitionException
60+ * @throws URISyntaxException
5261 */
5362 public static void main (final String [] args ) throws IOException , RecognitionException {
5463
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 ;
7071 }
7172
72- if (args .length < 1 ) {
73+ if (args .length < ( fileArg + 1 ) ) {
7374 Flow .printHelp (System .out );
7475 System .exit (2 );
7576 } else {
7677
77- final File fluxFile = new File (args [0 ]);
78+ final File fluxFile = new File (args [fileArg ]);
7879 if (!fluxFile .exists ()) {
79- System .err .println ("File not found: " + args [0 ]);
80+ System .err .println ("File not found: " + args [fileArg ]);
8081 System .exit (1 );
8182 return ;
8283 }
@@ -85,7 +86,10 @@ public boolean accept(final File dir, final String name) {
8586 final Map <String , String > vars = new HashMap <String , String >();
8687 vars .put (SCRIPT_HOME , fluxFile .getAbsoluteFile ().getParent () + System .getProperty ("file.separator" ));
8788
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+ }
8993 final Matcher matcher = VAR_PATTERN .matcher (args [i ]);
9094 if (!matcher .find ()) {
9195 Flow .printHelp (System .err );
@@ -96,11 +100,51 @@ public boolean accept(final File dir, final String name) {
96100
97101 // run parser and builder
98102 final List <Flow > flows = FluxCompiler .compile (ResourceUtil .getStream (fluxFile ), vars );
99- for (Flow flow : flows ) {
103+ for (final Flow flow : flows ) {
100104 flow .start ();
101105 }
102106 }
103107 }
104108
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+ }
124+
125+ if (args .length > 0 && args [0 ].startsWith (MODULES_DIR_ARG )) {
126+ modulesDir = new File (args [0 ].substring (MODULES_DIR_ARG .length ()));
127+ }
128+
129+ return modulesDir ;
130+ }
131+
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+ }
105149
106150}
0 commit comments