Beispiel #1
0
  protected void setLog() {
    LoggerConfig lc = null;
    Level logLevel = SEVERE;
    if (parameters.get("-diagnostics") != null) logLevel = FINEST;
    if (parameters.get("-verbose") != null || parameters.get("-v") != null) logLevel = FINER;
    if (parameters.get("-debug") != null || parameters.get("-d") != null) logLevel = FINE;
    if (parameters.get("-quiet") != null || parameters.get("-q") != null) {
      logLevel = null;
      System.setOut(new NullPrintStream(System.out));
      System.setErr(System.out);
    }
    String paramValue = parameters.get("-logger");
    if (paramValue != null) addLoggerClass(paramValue);

    boolean needLogConfigRead = logLevel != SEVERE;
    paramValue = parameters.get("-logfile");
    if (paramValue == null) paramValue = parameters.get("-l");
    needLogConfigRead |= paramValue != null;
    if (needLogConfigRead == false) {
      if (System.getProperty(CONFIG_FILE_PROP) == null && System.getenv(CONFIG_FILE_PROP) != null) {
        System.setProperty(CONFIG_FILE_PROP, System.getenv(CONFIG_FILE_PROP));
        needLogConfigRead = true;
      }
    } else {
      lc = new LoggerConfig();
      lc.setLogLevel(logLevel);
      if (paramValue != null) lc.setLogFile(paramValue);
      String logConfigFile = lc.create();
      if (logConfigFile != null) {
        System.setProperty(CONFIG_FILE_PROP, logConfigFile);
        // System.out.println("Config "+logConfigFile);
      }
    }
    if (needLogConfigRead)
      try {
        LogManager.getLogManager().readConfiguration();
      } catch (IOException ioe) {
        logger.setLevel(WARNING);
        logger.warning("Logger configuration cannot be read, " + ioe);
      }
    logger.setLevel(logLevel);
    // set some additional log parameters
  }
Beispiel #2
0
  public Configuration(String... args) {
    parameters =
        new HashMap<String, String>() {
          public String put(String key, String val) {
            if (reserved.containsKey(key) == false) {
              if (notRecognizableParam == null) notRecognizableParam = new ArrayList<String>();
              notRecognizableParam.add(key);
            }
            return super.put(key, val);
          }
        };
    defines = new HashMap<String, String>();
    targets = new ArrayList<String>();
    initReserved();
    String key = null;
    boolean inRunArgs = false;
    for (String arg : args) {
      if (inRunArgs) {
        if (arguments == null) arguments = new ArrayList<String>();
        arguments.add(arg);
      } else {
        if (arg.startsWith("-")) {
          if (key != null) parameters.put(key, "");
          if (arg.equals("--")) {
            inRunArgs = true;
            continue;
          }
          if (arg.startsWith("-D")) {
            int ep = arg.indexOf('=');
            if (ep > 0) {
              key = null;
              defines.put(arg.substring(2, ep), arg.substring(ep + 1));
              continue;
            }
          }
          key = arg;
        } else if (key != null) {
          if (key.startsWith("-D")) defines.put(key.substring(2), arg);
          else parameters.put(key, arg);
          key = null;
        } else targets.add(arg);
      }
    }
    if (key != null) parameters.put(key, "");
    setLog();
    if (parameters.get("-easter-eggs") != null) System.out.printf("Author: Dmitriy Rogatkin\n");
    if (parameters.get("-version") != null) {
      System.out.printf(
          "7Bee version %d.%d.%d compiled on %s\r\n", 1, 1, 2, org.bee.CompileStamp.getStamp());
      exit(0);
    }
    if (parameters.get("-h") != null || parameters.get("-help") != null) printHelp();
    if (exitCode != null) return;
    String extGrammar = parameters.get("-g");
    if (extGrammar == null) extGrammar = parameters.get("-grammar");
    if (extGrammar != null) reloadGrammar(extGrammar);
    beeFile = parameters.get("-f");
    if (beeFile == null) beeFile = parameters.get("-file");
    if (beeFile == null) beeFile = parameters.get("-buildfile");
    boolean searchUp = false;
    File currentDir = null;
    if (beeFile == null) {
      beeFile = parameters.get("-s");
      if (beeFile == null) beeFile = parameters.get("-find");
      if (beeFile != null) {
        searchUp = true;
        if (beeFile.length() == 0) beeFile = null;
        else if (new File(beeFile).exists() == false) {
          currentDir = new File("./").getAbsoluteFile().getParentFile();
          while (currentDir != null && new File(currentDir, beeFile).exists() == false) {
            currentDir = currentDir.getParentFile();
          }
        }
      }
    }
    if (beeFile == null) {
      try {
        final Pattern p = Pattern.compile("bee[^/\\?:*]*.xml");
        do {
          String[] beeFiles =
              (currentDir == null ? new File("./") : currentDir)
                  .list(
                      new FilenameFilter() {
                        public boolean accept(File dir, String name) {
                          return p.matcher(name).matches();
                        }
                      });
          if (beeFiles.length > 0) {
            // TODO possible to process all matching build scripts file names
            Arrays.sort(
                beeFiles,
                new Comparator<String>() {
                  public int compare(String o1, String o2) {
                    return o1.substring(0, o1.length() - 4)
                        .compareTo(o2.substring(0, o2.length() - 4));
                  }
                });

            // System.out.println(Arrays.toString(beeFiles));
            beeFile = beeFiles[0];
            break;
          } else {
            if (currentDir == null) currentDir = new File("./").getAbsoluteFile();
            currentDir = currentDir.getParentFile();
          }
        } while (searchUp && currentDir != null);
      } catch (NullPointerException npe) {
        logger.log(SEVERE, "Can't reach current directory.");
      } catch (ArrayIndexOutOfBoundsException aioobe) {
        logger.log(SEVERE, "No any files in current directory matching to build file pattern.");
      }
    }
    if (searchUp && currentDir != null) {
      System.setProperty("user.dir", currentDir.toString());
      List<String> cmdLine = new ArrayList<String>();
      cmdLine.add(
          System.getProperty("java.home")
              + File.separatorChar
              + "bin"
              + File.separatorChar
              + "java");
      cmdLine.add("-DJAVA_HOME=" + System.getProperty("JAVA_HOME"));
      cmdLine.add("-jar");
      cmdLine.add(
          new File(System.getenv("BEE_HOME"), "lib" + File.separatorChar + "bee.jar").toString());
      for (String arg : args) cmdLine.add(arg);
      try {
        // Process p = Runtime.getRuntime().exec((String[])cmdLine.toArray(new
        // String[cmdLine.size()]), null, currentDir);
        ProcessBuilder pb = new ProcessBuilder(cmdLine);
        pb.directory(currentDir);
        logger.log(FINEST, "restart:" + cmdLine);
        final Process p = pb.start();
        new StreamCatcher(p.getErrorStream(), System.err).start();
        new StreamCatcher(p.getInputStream(), System.out).start();
        InFeeder ifr = new InFeeder(System.in, p.getOutputStream());
        Runtime.getRuntime()
            .addShutdownHook(
                new Thread() {
                  public void run() {
                    p.destroy();
                  }
                });
        ifr.start();
        try {
          p.waitFor();
        } catch (InterruptedException ie) {
          //
        }
        ifr.terminate();
        System.exit(0);
      } catch (IOException ioe) {
        logger.log(SEVERE, "Couldn't change current directory by restarting." + ioe);
      }
    }
    readExtraProperties();
    // TODO: command options need to be implemented: -lib, -inputhandler
    // System.out.printf("Work dir %s set %s\n", new File("./").getAbsolutePath(),
    // System.getProperty("user.dir"));
    // TODO: check for dtd config file to fill descriptors
    logger.log(
        CONFIG, "Build file {0} in {1}", new Object[] {beeFile, System.getProperty("user.dir")});
    if (notRecognizableParam != null)
      logger.severe("The following options weren't recognized " + notRecognizableParam);
  }