示例#1
0
 public static List<CompileParameter> initializeParameters(
     final Context context, final String path) {
   final List<CompileParameter> parameters =
       new ArrayList<CompileParameter>(DEFAULT_PARAMETERS.length + 2);
   parameters.add(new Help(parameters));
   parameters.add(new PropertiesFile(parameters));
   Collections.addAll(parameters, DEFAULT_PARAMETERS);
   final File loc = new File(path);
   final File[] jars =
       loc.listFiles(
           new FileFilter() {
             public boolean accept(File file) {
               return file.getPath().toLowerCase().endsWith(".jar");
             }
           });
   final List<URL> urls = new ArrayList<URL>(jars != null ? jars.length : 0);
   if (jars != null) {
     for (final File j : jars) {
       try {
         urls.add(j.toURI().toURL());
       } catch (MalformedURLException ex) {
         context.error(ex);
       }
     }
   }
   final URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls.size()]));
   final ServiceLoader<CompileParameter> plugins = ServiceLoader.load(CompileParameter.class, ucl);
   for (final CompileParameter cp : plugins) {
     parameters.add(cp);
   }
   // HACK: service loader will lock jars on Windows
   // close is not available in Java6, so use reflection to invoke it
   if (Utils.isWindows()) {
     try {
       final Method close = ucl.getClass().getMethod("close");
       if (close != null) {
         close.invoke(ucl);
       }
     } catch (NoSuchMethodError ignore) {
     } catch (Exception ignore) {
     }
   }
   return parameters;
 }
示例#2
0
 private static boolean parse(
     final String[] args, final Context context, final List<CompileParameter> parameters) {
   if (args.length == 1 && ("/?".equals(args[0]) || "-?".equals(args[0]) || "?".equals(args[0]))) {
     showHelpAndExit(context, true, parameters);
     return false;
   }
   final List<ParameterParser> customParsers = new ArrayList<ParameterParser>();
   for (final CompileParameter cp : parameters) {
     if (cp instanceof ParameterParser) {
       customParsers.add((ParameterParser) cp);
     }
   }
   final List<String> errors = new ArrayList<String>();
   for (String a : args) {
     if (a.startsWith("-") || a.startsWith("/")) a = a.substring(1);
     final int eq = a.indexOf('=');
     final String name = a.substring(0, eq != -1 ? eq : a.length());
     final String value = eq == -1 ? null : a.substring(eq + 1);
     final CompileParameter cp = from(name, parameters);
     if (cp == null) {
       boolean matched = false;
       for (final ParameterParser parser : customParsers) {
         final Either<Boolean> tryParse = parser.tryParse(name, value, context);
         if (!tryParse.isSuccess()) {
           errors.add(tryParse.explainError());
           matched = true;
           break;
         } else if (tryParse.get()) {
           matched = true;
           break;
         }
       }
       if (!matched) {
         errors.add("Unknown parameter: " + name);
       }
     } else {
       if (eq == -1 && cp.getUsage() != null) {
         if (cp instanceof ParameterParser) {
           Either<Boolean> tryParse = ((ParameterParser) cp).tryParse(name, null, context);
           if (tryParse.isSuccess() && tryParse.get()) {
             context.put(cp, null);
           } else {
             errors.add("Expecting " + cp.getUsage() + " after = for " + a);
           }
         } else {
           errors.add("Expecting " + cp.getUsage() + " after = for " + a);
         }
       } else {
         context.put(cp, value);
       }
     }
   }
   if (args.length == 0 || errors.size() > 0) {
     for (final String err : errors) {
       context.error(err);
     }
     showHelpAndExit(context, args.length == errors.size(), parameters);
     return false;
   }
   return true;
 }