Beispiel #1
0
 public static void exec(Runnable toRun, String[] args, boolean exit, LogInterface logInterface) {
   // --Init
   log = logInterface;
   // (cleanup)
   ignoredClasspath = new String[0];
   runName = "<unnamed>";
   outputDB = null;
   dataDB = null;
   execDir = null;
   logger = null;
   // (bootstrap)
   Map<String, String> options = parseOptions(args); // get options
   fillOptions(BOOTSTRAP_CLASSES, options, false); // bootstrap
   log.bootstrap();
   log.startTrack("init");
   // (fill options)
   Class<?>[] visibleClasses = getVisibleClasses(options); // get classes
   Map<String, Field> optionFields = fillOptions(visibleClasses, options); // fill
   try {
     initDatabase(visibleClasses, options, optionFields); // database
   } catch (DatabaseException e) {
     log.warn(LOG_TAG, e.getMessage());
   }
   dumpOptions(options); // file dump
   log.endTrack("init");
   log.setup();
   // --Run Program
   try {
     log.startTrack("main");
     toRun.run();
     log.endTrack("main"); // ends main
     log.startTrack("flushing");
     if (logger != null) {
       logger.save();
     }
   } catch (Throwable e) { // catch everything
     log.exception(e);
     System.err.flush();
     if (logger != null) {
       exitMessage = e.getClass().getName() + ": " + e.getMessage();
       logger.suggestFlush(); // not a save!
     }
     log.exit(ExitCode.FATAL_EXCEPTION);
   }
   log.endTrack("flushing");
   if (exit) {
     log.exit(ExitCode.OK); // soft exit
   }
 }
Beispiel #2
0
  @SuppressWarnings("rawtypes")
  protected static final Map<String, Field> fillOptions(
      Class<?>[] classes, Map<String, String> options, boolean ensureAllOptions) {

    // --Get Fillable Options
    Map<String, Field> canFill = new HashMap<String, Field>();
    Map<String, Marker> required = new HashMap<String, Marker>();
    Map<String, String> interner = new HashMap<String, String>();
    for (Class c : classes) {
      Field[] fields = null;
      try {
        fields = c.getDeclaredFields();
      } catch (Throwable e) {
        log.debug(
            LOG_TAG,
            "Could not check fields for class: "
                + c.getName()
                + "  (caused by "
                + e.getClass()
                + ": "
                + e.getMessage()
                + ")");
        continue;
      }

      for (Field f : fields) {
        Option o = f.getAnnotation(Option.class);
        if (o != null) {
          // (check if field is static)
          if ((f.getModifiers() & Modifier.STATIC) == 0) {
            log.err(LOG_TAG, "Option can only be applied to static field: " + c + "." + f);
            System.exit(ExitCode.BAD_OPTION.code);
          }
          // (required marker)
          Marker mark = null;
          if (o.required()) {
            mark = new Marker();
            mark.unset();
          }
          // (add main name)
          String name = o.name().toLowerCase();
          if (name.equals("")) {
            name = f.getName().toLowerCase();
          }
          if (canFill.containsKey(name)) {
            String name1 =
                canFill.get(name).getDeclaringClass().getCanonicalName()
                    + "."
                    + canFill.get(name).getName();
            String name2 = f.getDeclaringClass().getCanonicalName() + "." + f.getName();
            if (!name1.equals(name2)) {
              log.err(
                  LOG_TAG,
                  "Multiple declarations of option " + name + ": " + name1 + " and " + name2);
              System.exit(ExitCode.BAD_OPTION.code);
            } else {
              log.err(
                  LOG_TAG,
                  "Class is in classpath multiple times: "
                      + canFill.get(name).getDeclaringClass().getCanonicalName());
            }
          }
          canFill.put(name, f);
          if (mark != null) required.put(name, mark);
          interner.put(name, name);
          // (add alternate names)
          if (!o.alt().equals("")) {
            for (String alt : o.alt().split(" *, *")) {
              alt = alt.toLowerCase();
              if (canFill.containsKey(alt) && !alt.equals(name))
                throw new IllegalArgumentException(
                    "Multiple declarations of option "
                        + alt
                        + ": "
                        + canFill.get(alt)
                        + " and "
                        + f);
              canFill.put(alt, f);
              if (mark != null) required.put(alt, mark);
              interner.put(alt, name);
            }
          }
        }
      }
    }

    // --Fill Options
    for (String key : options.keySet()) {
      String rawKey = key;
      key = key.toLowerCase();
      // (get values)
      String value = options.get(rawKey);
      Field target = canFill.get(key);
      // (mark required option as fulfilled)
      Marker mark = required.get(key);
      if (mark != null) {
        mark.set();
      }
      // (fill the field)
      if (target != null) {
        // (case: declared option)
        fillField(target, value);
      } else if (ensureAllOptions) {
        // (case: undeclared option)
        // split the key
        int lastDotIndex = rawKey.lastIndexOf('.');
        if (lastDotIndex < 0) {
          log.err(LOG_TAG, "Unrecognized option: " + key);
          System.exit(ExitCode.BAD_OPTION.code);
        }
        String className = rawKey.substring(0, lastDotIndex);
        String fieldName = rawKey.substring(lastDotIndex + 1);
        // get the class
        Class clazz = null;
        try {
          clazz = ClassLoader.getSystemClassLoader().loadClass(className);
        } catch (Exception e) {
          log.err(LOG_TAG, "Could not set option: " + rawKey + "; no such class: " + className);
          System.exit(ExitCode.BAD_OPTION.code);
        }
        // get the field
        try {
          target = clazz.getField(fieldName);
        } catch (Exception e) {
          log.err(
              LOG_TAG,
              "Could not set option: "
                  + rawKey
                  + "; no such field: "
                  + fieldName
                  + " in class: "
                  + className);
          System.exit(ExitCode.BAD_OPTION.code);
        }
        fillField(target, value);
      }
    }

    // --Ensure Required
    boolean good = true;
    for (String key : required.keySet()) {
      if (!required.get(key).isSet()) {
        log.err(
            LOG_TAG,
            "Missing required option: "
                + interner.get(key)
                + "   <in class: "
                + canFill.get(key).getDeclaringClass()
                + ">");
        required.get(key).set(); // don't duplicate error messages
        good = false;
      }
    }
    if (!good) {
      System.exit(ExitCode.BAD_OPTION.code);
    }

    return canFill;
  }