示例#1
0
  private static int getPriority(YajswConfigurationImpl config) {
    String priority = config.getString("wrapper.ntservice.process_priority", "");

    if ("LOW".equals(priority)) return Process.PRIORITY_LOW;
    else if ("BELOW_NORMAL".equals(priority)) return Process.PRIORITY_BELOW_NORMAL;
    else if ("NORMAL".equals(priority)) return Process.PRIORITY_NORMAL;
    else if ("ABOVE_NORMAL".equals(priority)) return Process.PRIORITY_ABOVE_NORMAL;
    else if ("HIGH".equals(priority)) return Process.PRIORITY_HIGH;
    return Process.PRIORITY_UNDEFINED;
  }
示例#2
0
  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
    String wrapperJar = WrapperLoader.getWrapperJar();
    // set home dir of the service to the wrapper jar parent, so that we may
    // find required libs
    String homeDir = new File(wrapperJar).getParent();
    if (!OperatingSystem.instance().setWorkingDir(homeDir))
      System.out.println(
          "could not set working dir. pls check configuration or user rights :" + homeDir);
    YajswConfigurationImpl _config = new YajswConfigurationImpl(false);
    boolean dbg = _config.getBoolean("wrapper.debug", false);
    int debug = dbg ? _config.getInt("wrapper.debug.level", 3) : 0;
    service = new WrapperMainServiceWin();
    // set service shutdown timeout
    service.setServiceName(_config.getString("wrapper.ntservice.name"));
    long timeout =
        _config.getInt("wrapper.shutdown.timeout", Constants.DEFAULT_SHUTDOWN_TIMEOUT) * 1000;
    timeout += _config.getInt("wrapper.script.STOP.timeout", 0) * 1000;
    timeout += _config.getInt("wrapper.script.SHUTDOWN.timeout", 0) * 1000;
    timeout += _config.getInt("wrapper.script.IDLE.timeout", 0) * 1000;
    timeout += _config.getInt("wrapper.script.ABORT.timeout", 0) * 1000;
    if (timeout > Integer.MAX_VALUE) timeout = Integer.MAX_VALUE;
    service.setStopTimeout((int) timeout);

    timeout = _config.getInt("wrapper.startup.timeout", Constants.DEFAULT_STARTUP_TIMEOUT) * 1000;
    if (timeout > Integer.MAX_VALUE) timeout = Integer.MAX_VALUE;
    service.setStartupTimeout((int) timeout);

    service.setAutoReportStartup(_config.getBoolean("wrapper.ntservice.autoreport.startup", true));

    if (_config.containsKey("wrapperx.config")) {
      List<Object> configs = _config.getList("wrapperx.config");
      wList = WrappedProcessFactory.createProcessList(new HashMap(), configs, true);
      for (WrappedProcess p : wList) {
        p.setService(service);
      }
    } else {
      WrappedProcess w = WrappedProcessFactory.createProcess(_config);
      // set service in wrapper so that we may stop the service in case
      // the application terminates and we need to shutdown the wrapper
      w.setService(service);
      w.init();
      wList.add(w);
    }

    w = wList.get(0);

    int priority = getPriority(_config);
    if (priority != -1) {
      int myPid = OperatingSystem.instance().processManagerInstance().currentProcessId();
      WindowsXPProcess.setProcessPriority(myPid, priority);
    }

    // start the applications
    // the wrapper may have to wait longer for the application to come up ->
    // start the application
    // in a separate thread and then check that the wrapper is up after a
    // max timeout
    // but return as soon as possible to the windows service controller
    final long maxStartTime = w.getMaxStartTime();
    final Future future =
        pool.submit(
            new Runnable() {
              public void run() {
                try {
                  Thread.yield();
                  wList.startAll();
                } catch (Throwable ex) {
                  ex.printStackTrace();
                  w.getWrapperLogger()
                      .info("Win Service: error starting wrapper " + ex.getMessage());
                  Runtime.getRuntime().halt(999);
                }
              }
            });
    pool.execute(
        new Runnable() {
          public void run() {
            try {
              future.get(maxStartTime, TimeUnit.MILLISECONDS);
            } catch (Exception ex) {
              ex.printStackTrace();
              w.getWrapperLogger()
                  .info(
                      "Win Service: wrapper did not start within "
                          + maxStartTime
                          + " ms "
                          + ex.getMessage());
              Runtime.getRuntime().halt(999);
            }
          }
        });
    if (debug > 2) w.getWrapperLogger().info("Win service: before service init");

    service.setDebug(debug);

    // init the service for signaling with services.exe. app will hang
    // here until service is stopped
    service.init();
    // service has terminated -> halt the wrapper jvm
    if (debug > 0) w.getWrapperLogger().info("Win service: terminated correctly");
    try {
      if (_config.getBoolean("wrapper.update.auto", false)) {
        w.update(null, false);
      }
    } catch (Exception ex) {

    }
    Runtime.getRuntime().halt(0);
  }