/**
   * This method should setup the flags/switches for the java command which will determine the
   * actual command to be executed.
   *
   * <p>Sets up flags like :
   *
   * <ul>
   *   <li>-Dem.home
   *   <li>-Dprocess.name
   * </ul>
   *
   * <br>
   * add computes the classpath.
   */
  protected void updateFlags(ServerCommand serverCmd) {

    String bootDir = System.getProperty("em.home");
    String appType = System.getProperty("appType");
    String appInst = System.getProperty("appInst");

    String javaClasspath = serverCmd.getClasspath();
    String processName = getProcessNameForLog();

    List jvmFlags = serverCmd.getJVMFlags();

    int cpToken = -1;

    for (int i = 0; i < jvmFlags.size(); i++) {
      String token = (String) jvmFlags.get(i);
      if (token.startsWith("-classpath")) {
        cpToken = i;
        javaClasspath = null;
      } else if (token.startsWith("-Dem.home")) {
        bootDir = null;
      } else if (token.startsWith("-Dprocess.name")) {
        processName = null;
      }
    }

    List addonJVMFlags = new LinkedList();

    if (bootDir != null) {
      addonJVMFlags.add("-Dem.home=" + bootDir);
    }

    if (processName != null) {
      addonJVMFlags.add("-Dprocess.name=" + processName);
    }

    if (!(appType == null || appType.trim().equals(""))) {
      addonJVMFlags.add("-DappType=" + appType);
    }

    if (!(appInst == null || appInst.trim().equals(""))) {
      addonJVMFlags.add("-DappInst=" + appInst);
    }

    if (cpToken != -1) {
      jvmFlags.remove(cpToken);
      String str = (String) jvmFlags.remove(cpToken);
      serverCmd.setClasspath(str);
    }

    jvmFlags.addAll(addonJVMFlags);
  }
  /**
   * This method should update the ServerCommand "struct" and setup the necessary values for the
   * server to be executed under OptimizeIt.
   */
  protected void updateServerCommandForOptimizeIt(ServerCommand serverCmd) {

    mLogger.config(mPropertyPrefix + " checking for optit");

    String optitPrefix = WDConstants.WD_PREFIX + ".java.profilers.OptimizeIt.";

    String home = DCPLib.getProperty(optitPrefix + "home", null);
    if (home == null) {
      mLogger.warning("OptimizeItHome is not specified. Cannot start OptimizeIt");
      return;
    }

    if (!(new java.io.File(home)).exists()) {
      mLogger.warning("OptimizeItHome : " + home + " does not exist." + " Cannot start OptimizeIt");
      return;
    }

    String classPath = DCPLib.getProperty(optitPrefix + "addonClassPath", "");
    classPath = WDUtil.sreplace(classPath, "OPTIMIZEIT_HOME", home);
    serverCmd.setClasspath(serverCmd.getClasspath() + File.pathSeparator + classPath);

    String libPath = DCPLib.getProperty(optitPrefix + "addonLibPath", "");
    libPath = WDUtil.sreplace(libPath, "OPTIMIZEIT_HOME", home);

    String sysLibPath = serverCmd.getLibPath();
    if (sysLibPath == null) sysLibPath = System.getProperty("java.library.path", "");
    serverCmd.setLibPath(libPath + File.pathSeparator + sysLibPath);

    String javaArgs = DCPLib.getProperty(optitPrefix + "javaArgs", "");
    javaArgs = WDUtil.sreplace(javaArgs, "OPTIMIZEIT_HOME", home);
    List jvmFlags = serverCmd.getJVMFlags();
    jvmFlags.add(0, javaArgs);

    String profilerClass = DCPLib.getProperty(optitPrefix + "class", "intuitive.audit.Audit");

    if (mOptitAuditPort == null) {
      mOptitAuditPort = getOptimizeItAuditPort(mPropertyPrefix);
    }
    System.out.println("OptimizeIt for " + mName + " is running at " + mOptitAuditPort);
    mLogger.config("OptimizeIt running at " + mOptitAuditPort);

    String optitArgs = DCPLib.getProperty(optitPrefix + "args", "");
    optitArgs = WDUtil.sreplace(optitArgs, "OPTIMIZEIT_HOME", home);
    optitArgs = WDUtil.sreplace(optitArgs, "AUDIT_PORT", mOptitAuditPort);

    String mainClass = serverCmd.getMainClassName();
    List appArgs = serverCmd.getAppArgs();

    serverCmd.setMainClassName(profilerClass);

    appArgs.add(0, mainClass);
    appArgs.add(0, optitArgs);
  }