コード例 #1
0
 /**
  * startLogger initializes and returns a file at logLoc with the results of logging at level
  * logLevel.
  *
  * @param logLoc location of the output log file- a string
  * @param logLevel logging level (is parsed by level.parse())
  * @return Logger object to log to.
  */
 public Logger startLogger(String logLoc, String logLevel) {
   try {
     fh = new FileHandler(logLoc); // sets output log file at logLoc
   } catch (SecurityException e) {
     e.printStackTrace();
     System.err.println("SecurityException establishing logger. Exiting...\n");
     System.exit(1);
   } catch (IOException e) {
     e.printStackTrace();
     System.err.println("IOException establishing logger. Exiting...\n");
     System.exit(1);
   }
   fh.setFormatter(new SimpleFormatter()); // format of log is 'human-readable' simpleformat
   logger.addHandler(fh); // attach formatter to logger
   logger.setLevel(Level.parse(logLevel)); // set log level
   return logger;
 }
コード例 #2
0
ファイル: Util.java プロジェクト: ncdc/qpid
  protected Util(String[] args) {
    CommandLineParser cmdLineParse = new PosixParser();

    Options options = new Options();
    options.addOption("h", "help", false, "show this help message and exit");
    options.addOption(
        OptionBuilder.withLongOpt("host")
            .withDescription("host to connect to (default 0.0.0.0)")
            .hasArg(true)
            .withArgName("HOST")
            .create('H'));
    options.addOption(
        OptionBuilder.withLongOpt("username")
            .withDescription("username to use for authentication")
            .hasArg(true)
            .withArgName("USERNAME")
            .create('u'));
    options.addOption(
        OptionBuilder.withLongOpt("password")
            .withDescription("password to use for authentication")
            .hasArg(true)
            .withArgName("PASSWORD")
            .create('w'));
    options.addOption(
        OptionBuilder.withLongOpt("port")
            .withDescription("port to connect to (default 5672)")
            .hasArg(true)
            .withArgName("PORT")
            .create('p'));
    options.addOption(
        OptionBuilder.withLongOpt("frame-size")
            .withDescription("specify the maximum frame size")
            .hasArg(true)
            .withArgName("FRAME_SIZE")
            .create('f'));
    options.addOption(
        OptionBuilder.withLongOpt("container-name")
            .withDescription("Container name")
            .hasArg(true)
            .withArgName("CONTAINER_NAME")
            .create('C'));

    options.addOption(OptionBuilder.withLongOpt("ssl").withDescription("Use SSL").create('S'));

    options.addOption(
        OptionBuilder.withLongOpt("remote-hostname")
            .withDescription("hostname to supply in the open frame")
            .hasArg(true)
            .withArgName("HOST")
            .create('O'));

    if (hasBlockOption())
      options.addOption(
          OptionBuilder.withLongOpt("block")
              .withDescription("block until messages arrive")
              .create('b'));

    if (hasCountOption())
      options.addOption(
          OptionBuilder.withLongOpt("count")
              .withDescription("number of messages to send (default 1)")
              .hasArg(true)
              .withArgName("COUNT")
              .create('c'));
    if (hasModeOption())
      options.addOption(
          OptionBuilder.withLongOpt("acknowledge-mode")
              .withDescription(
                  "acknowledgement mode: AMO|ALO|EO (At Least Once, At Most Once, Exactly Once")
              .hasArg(true)
              .withArgName("MODE")
              .create('k'));

    if (hasSubjectOption())
      options.addOption(
          OptionBuilder.withLongOpt("subject")
              .withDescription("subject message property")
              .hasArg(true)
              .withArgName("SUBJECT")
              .create('s'));

    if (hasSingleLinkPerConnectionMode())
      options.addOption(
          OptionBuilder.withLongOpt("single-link-per-connection")
              .withDescription(
                  "acknowledgement mode: AMO|ALO|EO (At Least Once, At Most Once, Exactly Once")
              .hasArg(false)
              .create('Z'));

    if (hasFilterOption())
      options.addOption(
          OptionBuilder.withLongOpt("filter")
              .withDescription("filter, e.g. exact-subject=hello; matching-subject=%.a.#")
              .hasArg(true)
              .withArgName("<TYPE>=<VALUE>")
              .create('F'));

    if (hasTxnOption()) {
      options.addOption("x", "txn", false, "use transactions");
      options.addOption(
          OptionBuilder.withLongOpt("batch-size")
              .withDescription("transaction batch size (default: 1)")
              .hasArg(true)
              .withArgName("BATCH-SIZE")
              .create('B'));
      options.addOption(
          OptionBuilder.withLongOpt("rollback-ratio")
              .withDescription("rollback ratio - must be between 0 and 1 (default: 0)")
              .hasArg(true)
              .withArgName("RATIO")
              .create('R'));
    }

    if (hasLinkDurableOption()) {
      options.addOption("d", "durable-link", false, "use a durable link");
    }

    if (hasStdInOption())
      options.addOption("i", "stdin", false, "read messages from stdin (one message per line)");

    options.addOption(
        OptionBuilder.withLongOpt("trace")
            .withDescription("trace logging specified categories: RAW, FRM")
            .hasArg(true)
            .withArgName("TRACE")
            .create('t'));
    if (hasSizeOption())
      options.addOption(
          OptionBuilder.withLongOpt("message-size")
              .withDescription("size to pad outgoing messages to")
              .hasArg(true)
              .withArgName("SIZE")
              .create('z'));

    if (hasResponseQueueOption())
      options.addOption(
          OptionBuilder.withLongOpt("response-queue")
              .withDescription("response queue to reply to")
              .hasArg(true)
              .withArgName("RESPONSE_QUEUE")
              .create('r'));

    if (hasLinkNameOption()) {
      options.addOption(
          OptionBuilder.withLongOpt("link")
              .withDescription("link name")
              .hasArg(true)
              .withArgName("LINK")
              .create('l'));
    }

    if (hasWindowSizeOption()) {
      options.addOption(
          OptionBuilder.withLongOpt("window-size")
              .withDescription("credit window size")
              .hasArg(true)
              .withArgName("WINDOW-SIZE")
              .create('W'));
    }

    CommandLine cmdLine = null;
    try {
      cmdLine = cmdLineParse.parse(options, args);

    } catch (ParseException e) {
      printUsage(options);
      System.exit(-1);
    }

    if (cmdLine.hasOption('h') || cmdLine.getArgList().isEmpty()) {
      printUsage(options);
      System.exit(0);
    }
    _host = cmdLine.getOptionValue('H', "0.0.0.0");
    _remoteHost = cmdLine.getOptionValue('O', null);
    String portStr = cmdLine.getOptionValue('p', "5672");
    String countStr = cmdLine.getOptionValue('c', "1");

    _useSSL = cmdLine.hasOption('S');

    if (hasWindowSizeOption()) {
      String windowSizeStr = cmdLine.getOptionValue('W', "100");
      _windowSize = Integer.parseInt(windowSizeStr);
    }

    if (hasSubjectOption()) {
      _subject = cmdLine.getOptionValue('s');
    }

    if (cmdLine.hasOption('u')) {
      _username = cmdLine.getOptionValue('u');
    }

    if (cmdLine.hasOption('w')) {
      _password = cmdLine.getOptionValue('w');
    }

    if (cmdLine.hasOption('F')) {
      _filter = cmdLine.getOptionValue('F');
    }

    _port = Integer.parseInt(portStr);

    _containerName = cmdLine.getOptionValue('C');

    if (hasBlockOption()) _block = cmdLine.hasOption('b');

    if (hasLinkNameOption()) _linkName = cmdLine.getOptionValue('l');

    if (hasLinkDurableOption()) _durableLink = cmdLine.hasOption('d');

    if (hasCountOption()) _count = Integer.parseInt(countStr);

    if (hasStdInOption()) _useStdIn = cmdLine.hasOption('i');

    if (hasSingleLinkPerConnectionMode()) _useMultipleConnections = cmdLine.hasOption('Z');

    if (hasTxnOption()) {
      _useTran = cmdLine.hasOption('x');
      _batchSize = Integer.parseInt(cmdLine.getOptionValue('B', "1"));
      _rollbackRatio = Double.parseDouble(cmdLine.getOptionValue('R', "0"));
    }

    if (hasModeOption()) {
      _mode = AcknowledgeMode.ALO;

      if (cmdLine.hasOption('k')) {
        _mode = AcknowledgeMode.valueOf(cmdLine.getOptionValue('k'));
      }
    }

    if (hasResponseQueueOption()) {
      _responseQueue = cmdLine.getOptionValue('r');
    }

    _frameSize = Integer.parseInt(cmdLine.getOptionValue('f', "65536"));

    if (hasSizeOption()) {
      _messageSize = Integer.parseInt(cmdLine.getOptionValue('z', "-1"));
    }

    String categoriesList = cmdLine.getOptionValue('t');
    String[] categories = categoriesList == null ? new String[0] : categoriesList.split("[, ]");
    for (String cat : categories) {
      if (cat.equalsIgnoreCase("FRM")) {
        FRAME_LOGGER.setLevel(Level.FINE);
        Formatter formatter =
            new Formatter() {
              @Override
              public String format(final LogRecord record) {
                return "[" + record.getMillis() + " FRM]\t" + record.getMessage() + "\n";
              }
            };
        for (Handler handler : FRAME_LOGGER.getHandlers()) {
          FRAME_LOGGER.removeHandler(handler);
        }
        Handler handler = new ConsoleHandler();
        handler.setLevel(Level.FINE);
        handler.setFormatter(formatter);
        FRAME_LOGGER.addHandler(handler);
      } else if (cat.equalsIgnoreCase("RAW")) {
        RAW_LOGGER.setLevel(Level.FINE);
        Formatter formatter =
            new Formatter() {
              @Override
              public String format(final LogRecord record) {
                return "[" + record.getMillis() + " RAW]\t" + record.getMessage() + "\n";
              }
            };
        for (Handler handler : RAW_LOGGER.getHandlers()) {
          RAW_LOGGER.removeHandler(handler);
        }
        Handler handler = new ConsoleHandler();
        handler.setLevel(Level.FINE);
        handler.setFormatter(formatter);
        RAW_LOGGER.addHandler(handler);
      }
    }

    _args = cmdLine.getArgs();
  }
コード例 #3
0
ファイル: CLIOptions.java プロジェクト: joliebig/Hercules
  public void parse() {
    PerfPrediction predict = new PerfPrediction();
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
      cmd = parser.parse(options, args);

      if (cmd.hasOption("h")) {
        help();
      }

      if (cmd.getOptions().length == 0) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
          if (reader.ready()) {
            predict.add(reader, "");
            predict.printFixedValues();
          } else {
            help();
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        return;
      }

      if (cmd.hasOption("cs") && cmd.getOptionValue("cs").equals("sqlite")) {
        if (cmd.hasOption("f")) {
          File location = new File(cmd.getOptionValue("f"));
          if (location.isDirectory()) {
            setSQLitePredictionScenarioNo(location);
            if (cmd.hasOption("im")) {
              if (isValidMode(cmd.getOptionValue("im"))) {
                if (cmd.hasOption("pm")) {
                  if (isValidMode(cmd.getOptionValue("pm"))) {
                    this.INPUT_MODE = cmd.getOptionValue("im");
                    this.PREDICT_MODE = cmd.getOptionValue("pm");
                    try {
                      predict.addPrediction(location, this.INPUT_MODE);
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
                    predict.predict(this.PREDICT_MODE, getSQLiteDir(), location);

                  } else {
                    log.log(
                        Level.SEVERE, "invalid mode for option '-pm " + validModesToString() + "'");
                  }
                } else {
                  log.log(
                      Level.SEVERE, "missing prediction mode '-pm " + validModesToString() + "'");
                }
              } else {
                log.log(Level.SEVERE, "invalid mode for option '-im " + validModesToString() + "'");
              }
            } else {
              log.log(Level.SEVERE, "missing input mode '-im " + validModesToString() + "'");
            }

          } else {
            log.log(
                Level.SEVERE,
                "'-f <location>' does not point to a folder",
                cmd.getOptionValue("f"));
          }
        } else {
          log.log(Level.SEVERE, "Use option '-f <location>'", cmd.getOptionValue("f"));
        }
      }
      if (cmd.hasOption("rc")) {
        GenerateRandomConfig.generateConfigs(cmd.getOptionValue("rc"));
      }
      if (cmd.hasOption("fo")) {
        try (BufferedReader reader = new BufferedReader(new FileReader(cmd.getOptionValue("fo")))) {
          predict.add(reader, "");
          predict.printFixedValues();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      if (cmd.hasOption("md")) {
        Accumulator.enableMedian();
      }
      if (cmd.hasOption("acc")) {
        Accumulator.start(new File(cmd.getOptionValue("acc")));
      }

    } catch (ParseException e) {
      log.log(Level.SEVERE, "Failed to parse comand line properties", e);
      help();
    }
  }
コード例 #4
0
public class Gio {

  private static Logger logger = Logger.getLogger("");
  /** create logger object */
  private FileHandler fh = null;
  /** creates filehandler for logging */
  private Properties genProps = new Properties();
  /** holds all the property values */
  private Properties origWeights = null;
  /** the loaded weights file. */
  private Properties newWeights = null;
  /**
   * the revised weights, following LearnAlgorithm. written to disk by shutdown(). also used in
   * loading weights during init()
   */
  private UserIO userInterface = null;
  /** means of interacting with the user */
  private PolicyDatabase pdb;
  /** Policy database object */
  private NetworkR nr;
  /** Network Resource (community advice database) */
  private PolicyObject po;
  /** The new PolicyObject that is parsed * */

  /**
   * Constructor fo gio class. There should only be one. Consider this a singleton instance to call
   * I/O messages on. Constructs and parses command line arguements as well.
   *
   * @throws Exception Mostly from loadWeights, but should also happen for loadFromConfig
   */
  public Gio(String[] args) throws Exception {
    this(args, null);
  }

  /**
   * A constructor permitting a user interface class to launch everything and be in control.
   *
   * @param args any commandline arguements
   * @param ui the known UserIO object
   * @throws Exception Mostly from loadWeights, but should also happen for loadFromConfig
   */
  public Gio(String[] args, UserIO ui) throws Exception {

    userInterface = ui;
    genProps = loadFromConfig("./PrivacyAdviser.cfg");
    loadCLO(args);
    // TODO add method to check validity of genProps (after each file load, clo load, and ui load).

    if ((genProps.getProperty("genConfig") != null)
        && (genProps.getProperty("genConfig") != "./PrivacyAdvisor.cfg")) {
      System.err.println("clo config call");
      genProps = loadFromConfig(genProps.getProperty("genConfig")); // TODO merge, not override
      loadCLO(args);
    }

    // start the logger
    logger =
        startLogger(
            genProps.getProperty("loglocation", "./LOG.txt"),
            genProps.getProperty("loglevel", "INFO"));
    if (userInterface == null) {
      selectUI(genProps.getProperty("userIO"));
    }
    selectPDB(genProps.getProperty("policyDB"));

    // load the weights configuration file
    origWeights = new Properties();
    origWeights = loadWeights();
    if (Boolean.parseBoolean(genProps.getProperty("useNet", "false"))) {
      startNetwork();
    } else {
      nr = null;
    }
  }

  /**
   * call the user interface's general configuration method if the userInit option is true, and a
   * user interface exists
   */
  public void configUI() {
    if (Boolean.parseBoolean(genProps.getProperty("userInit", "false"))
        && !(userInterface == null)) {
      userInterface.user_init(genProps);
    }
  }

  public void setGenProps(Properties genProps) {
    this.genProps = genProps;
  }

  /**
   * accepts the direct commandline options, then parses & implements them.
   *
   * @param args
   */
  // TODO add exception for invalid options
  private void loadCLO(String[] args) {
    Options options = new Options();

    String[][] clolist = {
      // {"variable/optionname", "description"},
      {"genConfig", "general configuration file location"},
      {"inWeightsLoc", "input weights configuration file location"},
      {"inDBLoc", "input database file location"},
      {"outWeightsLoc", "output weights configuration file location"},
      {"outDBLoc", "output database file location"},
      {"p3pLocation", "adding to DB: single policy file location"},
      {"p3pDirLocation", "adding to DB: multiple policy directory location"},
      {"newDB", "create new database in place of old one (doesn't check for existence of old one"},
      {"newPolicyLoc", "the policy object to process"},
      {"userResponse", "response to specified policy"},
      {"userIO", "user interface"},
      {"userInit", "initialization via user interface"},
      {"policyDB", "PolicyDatabase backend"},
      {"cbrV", "CBR to use"},
      {"blanketAccept", "automatically accept the user suggestion"},
      {"loglevel", "level of things save to the log- see java logging details"},
      {"policyDB", "PolicyDatabase backend"},
      {"NetworkRType", "Network Resource type"},
      {"NetworkROptions", "Network Resource options"},
      {"confidenceLevel", "Confidence threshold for consulting a networked resource"},
      {"useNet", "use networking options"},
      {"loglocation", "where to save the log file"},
      {"loglevel", "the java logging level to use. See online documentation for enums."}
    };

    for (String[] i : clolist) {
      options.addOption(i[0], true, i[1]);
    }

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
      cmd = parser.parse(options, args);
    } catch (ParseException e) {
      System.err.println("Error parsing commandline arguments.");
      e.printStackTrace();
      System.exit(3);
    }
    /*
    for(String i : args)
    {
    	System.err.println(i);
    }
    */
    for (String[] i : clolist) {
      if (cmd.hasOption(i[0])) {
        System.err.println("found option i: " + i);
        genProps.setProperty(i[0], cmd.getOptionValue(i[0]));
      }
    }
    System.err.println(genProps);
  }

  /**
   * converts a string into a valid CBR
   *
   * @param string the string to parse
   * @return the CBR to use
   * @throws Exception
   */
  private CBR parseCBR(String string) throws Exception {

    try {
      return (string == null) ? (null) : (new CBR(this)).parse(string);
    } catch (Exception e) {
      System.err.println("error parsing CBR, exiting.");
      e.printStackTrace();
      System.exit(5);
      return null;
    }
  }

  /**
   * Should parse a string to select, initialize, and return one of the policy databases coded
   *
   * @param optionValue the string to parse
   * @return the policy database being used
   */
  private void selectPDB(String optionValue) {
    try {
      Class<?> cls = Class.forName("com.kpro.datastorage." + optionValue);

      @SuppressWarnings("rawtypes")
      Class[] params = new Class[2];
      params[0] = String.class;
      params[1] = String.class;
      Method m = cls.getDeclaredMethod("getInstance", params);

      Object[] argslist = new Object[2];
      argslist[0] = genProps.getProperty("inDBLoc");
      argslist[1] = genProps.getProperty("outDBLoc", genProps.getProperty("inDBLoc"));
      pdb = (PolicyDatabase) m.invoke(null, argslist);
    } catch (Exception e) {
      System.err.println("Selected PolicyDatabase not found");
    }

    if (pdb == null) {
      System.err.println("pdb null in selectPDB");
    }
  }

  /**
   * Should parse a string to select, initialize, and return the user interface selected
   *
   * @param optionValue the string to parse
   * @return the user interface to use
   */
  private void selectUI(String optionValue) {
    try {
      Class<?> cls = Class.forName("com.kpro.ui." + optionValue);
      userInterface = (UserIO) cls.newInstance();
    } catch (Exception e) {
      System.err.println("Selected UserIO not found");
    }
  }

  /**
   * Should parse a string to select, initialize, and return one of the actions (result of checking
   * an object) coded.
   *
   * @param optionValue the string to parse
   * @return the action to apply to the new policy
   */
  private Action parseAct(String optionValue) {
    return (optionValue == null) ? (null) : (new Action().parse(optionValue));
  }

  /**
   * Loads the general configuration file, either from provided string, or default location
   * (./PrivacyAdviser.cfg)
   *
   * @param location of configuration file
   * @return properties object corresponding to given configuration file
   */
  // TODO add exception for invalid options
  public Properties loadFromConfig(String fileLoc) {
    Properties configFile = new Properties();

    try {
      File localConfig = new File(fileLoc);
      InputStream is = null;
      if (localConfig.exists()) {
        is = new FileInputStream(localConfig);
      } else {
        System.err.println(
            "No configuration file at " + fileLoc + ". Please place one in the working directory.");
        System.exit(3);
      }
      configFile.load(is);
    } catch (IOException e) {
      e.printStackTrace();
      System.err.println("IOException reading first configuration file. Exiting...\n");
      System.exit(1);
    }

    return configFile;
  }

  /**
   * Loads the weights configuration file, from the provided location
   *
   * @param location of configuration file <---- ????
   * @return properties object corresponding to given configuration file
   * @throws Exception if there's an issue reading the file (if it doesn't exist, or has an IO
   *     error)
   */
  public Properties loadWeights() throws Exception {
    try {
      if (genProps.getProperty("inWeightsLoc") == null) {
        System.err.println("inWeightsLoc in Gio/LoadWeights is null");
      }
      File localConfig = new File(genProps.getProperty("inWeightsLoc"));
      //			System.out.println(genProps.getProperty("inWeightsLoc"));
      InputStream is = null;
      if (localConfig.exists()) {
        is = new FileInputStream(localConfig);
      } else {
        System.err.println(
            "No weights file is available at "
                + genProps.getProperty("inWeightsLoc")
                + " . Please place one in the working directory.");
        throw new Exception(
            "In class Gio.java:loadWeights(), file "
                + genProps.getProperty("inWeightsLoc")
                + " doesn't exist.");
      }
      origWeights = new Properties();
      origWeights.load(is);
    } catch (IOException e) {
      e.printStackTrace();
      System.err.println("IOException reading the weights configuration file....\n");
      throw new Exception(
          "In class Gio.java:loadWeights(), IOException loading the weights from file "
              + genProps.getProperty("inWeightsLoc")
              + " .");
    }

    return origWeights;
  }

  /**
   * startLogger initializes and returns a file at logLoc with the results of logging at level
   * logLevel.
   *
   * @param logLoc location of the output log file- a string
   * @param logLevel logging level (is parsed by level.parse())
   * @return Logger object to log to.
   */
  public Logger startLogger(String logLoc, String logLevel) {
    try {
      fh = new FileHandler(logLoc); // sets output log file at logLoc
    } catch (SecurityException e) {
      e.printStackTrace();
      System.err.println("SecurityException establishing logger. Exiting...\n");
      System.exit(1);
    } catch (IOException e) {
      e.printStackTrace();
      System.err.println("IOException establishing logger. Exiting...\n");
      System.exit(1);
    }
    fh.setFormatter(new SimpleFormatter()); // format of log is 'human-readable' simpleformat
    logger.addHandler(fh); // attach formatter to logger
    logger.setLevel(Level.parse(logLevel)); // set log level
    return logger;
  }

  /**
   * Loads the case history into cache. This is where the background database chosen.
   *
   * @param dLoc the location of the database
   */
  public void loadDB() {
    try {
      // TODO what about if we want to create a new db?
      if (!Boolean.parseBoolean(genProps.getProperty("newDB"))) {
        pdb.loadDB();
      }
    } catch (Exception e) {
      System.err.println("Something wrong with loading DB");
    }
    try {
      loadCLPolicies();
    } catch (Exception e) {
      System.err.println("Error. Probably wrong path to P3P folder");
    }
  }

  /** loads [additional] policies from commandline (either -p or -f) */
  private void loadCLPolicies() {
    // we already checked to make sure we have one of the options avaliable
    File pLoc = null;
    PolicyObject p = null;

    if (genProps.getProperty("p3pLocation", null) != null) {
      pLoc = new File(genProps.getProperty("p3pLocation"));
      if (!pLoc.exists()) {
        System.err.println(
            "no file found at p3p policy location specified by the -p3p option: "
                + genProps.getProperty("p3pLocation"));
        System.err.println("current location is " + System.getProperty("user.dir"));
        System.exit(1);
      }
      try {
        p = (new P3PParser()).parse(pLoc.getAbsolutePath());
        if (p.getContextDomain() == null) {
          p.setContext(
              new Context(
                  new Date(System.currentTimeMillis()),
                  new Date(System.currentTimeMillis()),
                  genProps.getProperty("p3pLocation")));
        }
        pdb.addPolicy(p);
      } catch (Exception e) {
        System.err.println("Error with parsing or database");
        e.printStackTrace();
        // System.exit(5);
      }
    }
    if (genProps.getProperty("p3pDirLocation", null) != null) {
      pLoc = new File(genProps.getProperty("p3pDirLocation"));
      File[] pfiles = pLoc.listFiles();

      // System.err.println("pfiles for p3pDirLocation: "+pfiles);
      for (int i = 0; i < pfiles.length; i++) {

        pLoc = (pfiles[i]);
        if (!pLoc.exists()) {
          System.err.println(
              "no file found at p3p policy location specified by the -p3pDirLocation option, "
                  + genProps.getProperty("p3pDirLocation"));
          System.exit(1);
        }
        try {

          p = (new P3PParser()).parse(pLoc.getAbsolutePath());
          if (p.getContext().getDomain() == null) {
            p.setContext(
                new Context(
                    new Date(System.currentTimeMillis()),
                    new Date(System.currentTimeMillis()),
                    pfiles[i].getAbsolutePath()));
          }
          pdb.addPolicy(p);
        } catch (Exception e) {
          System.err.println("Einar needs to fix this parsing error.");
          e.printStackTrace();
        }
      }
    }
  }

  /**
   * returns the only policy database
   *
   * @return the policy database
   */
  public PolicyDatabase getPDB() {
    return pdb;
  }

  /** closes resources and write everything to file */
  public void shutdown() {

    pdb.closeDB(); // save the db
    if (newWeights == null) {
      newWeights = origWeights;
    }
    writePropertyFile(
        newWeights, genProps.getProperty("outWeightsLoc", genProps.getProperty("inWeightsLoc")));
    userInterface.closeResources();
    System.out.println("preferences saved and closed without error.");
  }

  /**
   * writes a property file to disk
   *
   * @param wprops the property file to write
   * @param wloc where to write to
   */
  private void writePropertyFile(Properties wprops, String wloc) {
    if (wprops == null) System.out.println("wrops null in gio/writepropertyfile");
    if (wloc == null) System.out.println("wloc null in gio/writepropertyfile");
    try {
      wprops.store(new FileOutputStream(wloc), null);
    } catch (IOException e) {
      System.err.println("Error writing weights to file.");
      e.printStackTrace();
      System.exit(3);
    }
  }

  /**
   * Generates handles response. This is were we would pass stuff to cli or gui, etc
   *
   * @param n the processed policy object
   * @return the policyObjected as accepted by user (potentially modified
   */
  public PolicyObject userResponse(PolicyObject n) {
    if ((parseAct(genProps.getProperty("userResponse", null)) == null)
        && !Boolean.parseBoolean(genProps.getProperty("blanketAccept", "false"))) {
      return userInterface.userResponse(n);
    } else {
      if (Boolean.parseBoolean(genProps.getProperty("blanketAccept", "false"))) {
        return n.setAction(n.getAction().setOverride(true));
      } else {
        return n.setAction(parseAct(genProps.getProperty("userResponse", null)));
      }
    }
  }

  /**
   * returns the policy object from the policyObject option
   *
   * @return the policy object to be processed
   */
  public void loadPO() {

    if (genProps.getProperty("newPolicyLoc", null) == null)
      System.err.println("newPolLoc == null in gio:loadPO");
    File pLoc = new File(genProps.getProperty("newPolicyLoc", null));
    if (!pLoc.exists()) {
      System.err.println("no file found at p3p policy location specified by the new policy option");
      System.exit(1);
    }

    po = (new P3PParser()).parse(pLoc.getAbsolutePath());
    // TODO make sure that the context is parsed if avaliable
    if (po.getContext().getDomain() == null) {
      po.setContext(
          new Context(
              new Date(System.currentTimeMillis()),
              new Date(System.currentTimeMillis()),
              genProps.getProperty("newPolicyLoc")));
    }
  }

  public PolicyObject getPO() {
    return po;
  }

  /**
   * returns the true if it should only build
   *
   * @return true if a CBR should NOT be run
   */
  public boolean isBuilding() {
    return (genProps.getProperty("newPolicyLoc", null) == null);
  }

  /**
   * saves the new weights to a buffer variable before writing in the shutdown call
   *
   * @param newWeightP the new weights file to save
   */
  public void setWeights(Properties newWeightP) {
    newWeights = newWeightP;
  }

  /**
   * returns the CBR to use
   *
   * @return the cbr to use
   * @throws Exception
   */
  public CBR getCBR() throws Exception {
    return parseCBR(genProps.getProperty("cbrV", null));
  }

  /**
   * returns the originally imported set of weights
   *
   * @return the weights for policy attributes
   */
  public Properties getWeights() {
    return origWeights;
  }

  /**
   * shows the database on the user interface, if the user interface exists and no user response is
   * specied and there is no 'blanketAccept' option.
   */
  public void showDatabase() {
    if (userInterface != null) // the user interface exists
    {
      if (!genProps.contains("userResponse")) // if we have no preknown user response
      {
        if (genProps.getProperty("blanketAccept", null) == null) // if we have no blanket accept
        {
          userInterface.showDatabase(pdb); // we must be running interactive
        }
      }
    }
  }

  /**
   * GUI classes should use this to ensure the user passes valid files to load.
   *
   * @param filepath path of the file to check
   * @return true if the file exists, else false
   */
  public boolean fileExists(String filepath) {
    return (new File(filepath)).exists();
  }

  /**
   * Starts the NetworkR specificied by the configuration settings.
   *
   * @throws ClassNotFoundException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws InstantiationException
   * @throws SecurityException
   * @throws IllegalArgumentException
   */
  private void startNetwork()
      throws ClassNotFoundException, IllegalArgumentException, SecurityException,
          InstantiationException, IllegalAccessException, InvocationTargetException {
    // System.err.println("startnetwork called");
    Class<?> cls = Class.forName("com.kpro.datastorage." + genProps.getProperty("NetworkRType"));
    if (cls == null) {
      System.err.println("NetworkRType incorrect- null in Gio.startNetwork()");
    }
    nr =
        (NetworkR)
            cls.getDeclaredConstructors()[0].newInstance(genProps.getProperty("NetworkROptions"));
    // System.err.println("nr in startNetwork="+nr);
  }

  public NetworkR getNR() {
    return nr;
  }

  /**
   * gets the confidence level threshold from the configuration
   *
   * @return the confidence threshold
   */
  public double getConfLevel() {
    return Double.parseDouble(genProps.getProperty("confidenceLevel", "1.0"));
  }
}
コード例 #5
0
ファイル: CLIOptions.java プロジェクト: joliebig/Hercules
public class CLIOptions {
  private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
  private String[] args = null;
  private Options options = new Options();
  private int SQLITE_PREDICTION_SCENARIO;
  private final List<String> VALID_MODES =
      Arrays.asList("featurewise", "pairwise", "codecoverage", "random", "allyes");
  private String INPUT_MODE;
  private String PREDICT_MODE;
  // private final String TYPECHEF_SQLITE_IFDEFTOIF_DIR = new
  // File(System.getProperty("user.dir")).getParentFile().getParent()
  // + File.separator + "TypeChef-SQLiteIfdeftoif" + File.separator + "optionstructs_ifdeftof";
  // private final String TYPECHEF_SQLITE_IFDEFTOIF_DIR = new
  // File(System.getProperty("user.dir")).getParentFile().getParent()
  // + File.separator + "TypeChef" + File.separator + "TypeChef-SQLiteIfdeftoif" + File.separator +
  // "optionstructs_ifdeftoif";

  private String getSQLiteDir() {
    File current;
    current = new File(System.getProperty("user.dir"));
    while (!current.getName().equals("Hercules")) {
      current = current.getParentFile();
    }
    return current.getParent()
        + File.separator
        + "TypeChef-SQLiteIfdeftoif"
        + File.separator
        + "optionstructs_ifdeftoif";
  }

  private String validModesToString() {
    StringBuilder sb = new StringBuilder();
    String seperator = "";
    sb.append("[");
    for (String current : VALID_MODES) {
      sb.append(seperator);
      seperator = "/";
      sb.append(current);
    }
    sb.append("]");
    return sb.toString();
  }

  public CLIOptions(String[] args) {
    this.args = args;
    options.addOption("h", "help", false, "display help");
    options.addOption("cs", "case study", true, "Set case study: [sqlite]");
    options.addOption("f", "folder", true, "Folder location of result files");
    options.addOption("im", "inputmode", true, "Set input mode: " + validModesToString());
    options.addOption("pm", "predictmode", true, "Set predict mode: " + validModesToString());
    options.addOption("rc", "randomconfig", true, "Generate random config from random.csv");
    options.addOption(
        "fo",
        "fixoutput",
        true,
        "Fixes hashmap values from executing a performance binary: [executed output file location]");
    options.addOption(
        "acc",
        "accumulate",
        true,
        "Accumulates data from multiple performance runs in given directory");
    options.addOption(
        "md", "median", false, "Changes accumulation mode to use median instead of mean");
  }

  public void parse() {
    PerfPrediction predict = new PerfPrediction();
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
      cmd = parser.parse(options, args);

      if (cmd.hasOption("h")) {
        help();
      }

      if (cmd.getOptions().length == 0) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
          if (reader.ready()) {
            predict.add(reader, "");
            predict.printFixedValues();
          } else {
            help();
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        return;
      }

      if (cmd.hasOption("cs") && cmd.getOptionValue("cs").equals("sqlite")) {
        if (cmd.hasOption("f")) {
          File location = new File(cmd.getOptionValue("f"));
          if (location.isDirectory()) {
            setSQLitePredictionScenarioNo(location);
            if (cmd.hasOption("im")) {
              if (isValidMode(cmd.getOptionValue("im"))) {
                if (cmd.hasOption("pm")) {
                  if (isValidMode(cmd.getOptionValue("pm"))) {
                    this.INPUT_MODE = cmd.getOptionValue("im");
                    this.PREDICT_MODE = cmd.getOptionValue("pm");
                    try {
                      predict.addPrediction(location, this.INPUT_MODE);
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
                    predict.predict(this.PREDICT_MODE, getSQLiteDir(), location);

                  } else {
                    log.log(
                        Level.SEVERE, "invalid mode for option '-pm " + validModesToString() + "'");
                  }
                } else {
                  log.log(
                      Level.SEVERE, "missing prediction mode '-pm " + validModesToString() + "'");
                }
              } else {
                log.log(Level.SEVERE, "invalid mode for option '-im " + validModesToString() + "'");
              }
            } else {
              log.log(Level.SEVERE, "missing input mode '-im " + validModesToString() + "'");
            }

          } else {
            log.log(
                Level.SEVERE,
                "'-f <location>' does not point to a folder",
                cmd.getOptionValue("f"));
          }
        } else {
          log.log(Level.SEVERE, "Use option '-f <location>'", cmd.getOptionValue("f"));
        }
      }
      if (cmd.hasOption("rc")) {
        GenerateRandomConfig.generateConfigs(cmd.getOptionValue("rc"));
      }
      if (cmd.hasOption("fo")) {
        try (BufferedReader reader = new BufferedReader(new FileReader(cmd.getOptionValue("fo")))) {
          predict.add(reader, "");
          predict.printFixedValues();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      if (cmd.hasOption("md")) {
        Accumulator.enableMedian();
      }
      if (cmd.hasOption("acc")) {
        Accumulator.start(new File(cmd.getOptionValue("acc")));
      }

    } catch (ParseException e) {
      log.log(Level.SEVERE, "Failed to parse comand line properties", e);
      help();
    }
  }

  private void setSQLitePredictionScenarioNo(File folder) {
    this.SQLITE_PREDICTION_SCENARIO = Integer.parseInt(folder.getName());
  }

  private Boolean isValidMode(String mode) {
    return VALID_MODES.contains(mode);
  }

  private void help() {
    HelpFormatter formatter = new HelpFormatter();
    formatter.setWidth(90);
    formatter.printHelp("PerfTimes.jar", options);
    System.exit(0);
  }
}