public void stopServer(String[] arguments) {

    if (arguments != null) {
      arguments(arguments);
    }

    Server s = getServer();
    if (s == null) {
      // Create and execute our Digester
      Digester digester = createStopDigester();
      digester.setClassLoader(Thread.currentThread().getContextClassLoader());
      File file = configFile();
      try {
        InputSource is = new InputSource("file://" + file.getAbsolutePath());
        FileInputStream fis = new FileInputStream(file);
        is.setByteStream(fis);
        digester.push(this);
        digester.parse(is);
        fis.close();
      } catch (Exception e) {
        log.error("Catalina.stop: ", e);
        System.exit(1);
      }
    } else {
      // Server object already present. Must be running as a service
      if (s instanceof Lifecycle) {
        try {
          ((Lifecycle) s).stop();
        } catch (LifecycleException e) {
          log.error("Catalina.stop: ", e);
        }
        return;
      }
      // else fall down
    }

    // Stop the existing server
    s = getServer();
    try {
      if (s.getPort() > 0) {
        String hostAddress = InetAddress.getByName("localhost").getHostAddress();
        Socket socket = new Socket(hostAddress, getServer().getPort());
        OutputStream stream = socket.getOutputStream();
        String shutdown = s.getShutdown();
        for (int i = 0; i < shutdown.length(); i++) stream.write(shutdown.charAt(i));
        stream.flush();
        stream.close();
        socket.close();
      } else {
        log.error(sm.getString("catalina.stopServer"));
        System.exit(1);
      }
    } catch (IOException e) {
      log.error("Catalina.stop: ", e);
      System.exit(1);
    }
  }
  void configure(Configuration configuration) {
    long start = System.nanoTime();
    initBaseDir(configuration);
    Digester digester = new FakeCatalina().createStartDigester();
    digester.push(this);

    URL tomcatResource = configuration.getConfigurationURL();
    if (tomcatResource == null) {
      tomcatResource = getClass().getResource("/tomcat-server.xml");
    }

    File configurationFile = new File(configuration.getConfigurationDir(), SERVER_CONFIG_FILE_NAME);
    if (configurationFile.exists()) {
      try {
        tomcatResource = configurationFile.toURI().toURL();
      } catch (MalformedURLException e) {
        LOG.error("Exception while starting Tomcat:", e);
        throw new RuntimeException("Exception while starting Tomcat", e);
      }
    }
    if (tomcatResource != null) {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("Configure using resource " + tomcatResource);

        digester.parse(tomcatResource.openStream());
        long elapsed = start - System.nanoTime();
        if (LOG.isInfoEnabled()) {
          LOG.info("configuration processed in {} ms", (elapsed / 1000000));
        }
      } catch (IOException e) {
        LOG.error("Exception while starting Tomcat:", e);
        throw new RuntimeException("Exception while starting Tomcat", e);
      } catch (SAXException e) {
        LOG.error("Exception while starting Tomcat:", e);
        throw new RuntimeException("Exception while starting Tomcat", e);
      } finally {
        Thread.currentThread().setContextClassLoader(loader);
      }
    }

    mergeConfiguration(configuration);
  }
Esempio n. 3
0
  /** Start a new server instance. */
  public void load() {

    long t1 = System.nanoTime();

    initDirs();

    // Before digester - it may be needed

    initNaming();

    // Create and execute our Digester
    Digester digester = createStartDigester();

    InputSource inputSource = null;
    InputStream inputStream = null;
    File file = null;
    try {
      file = configFile();
      inputStream = new FileInputStream(file);
      inputSource = new InputSource(file.toURI().toURL().toString());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.debug(sm.getString("catalina.configFail", file), e);
      }
    }
    if (inputStream == null) {
      try {
        inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());
        inputSource =
            new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());
      } catch (Exception e) {
        if (log.isDebugEnabled()) {
          log.debug(sm.getString("catalina.configFail", getConfigFile()), e);
        }
      }
    }

    // This should be included in catalina.jar
    // Alternative: don't bother with xml, just create it manually.
    if (inputStream == null) {
      try {
        inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
        inputSource =
            new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());
      } catch (Exception e) {
        if (log.isDebugEnabled()) {
          log.debug(sm.getString("catalina.configFail", "server-embed.xml"), e);
        }
      }
    }

    if (inputStream == null || inputSource == null) {
      if (file == null) {
        log.warn(sm.getString("catalina.configFail", getConfigFile() + "] or [server-embed.xml]"));
      } else {
        log.warn(sm.getString("catalina.configFail", file.getAbsolutePath()));
        if (file.exists() && !file.canRead()) {
          log.warn("Permissions incorrect, read permission is not allowed on the file.");
        }
      }
      return;
    }

    try {
      inputSource.setByteStream(inputStream);
      digester.push(this);
      digester.parse(inputSource);
    } catch (SAXParseException spe) {
      log.warn("Catalina.start using " + getConfigFile() + ": " + spe.getMessage());
      return;
    } catch (Exception e) {
      log.warn("Catalina.start using " + getConfigFile() + ": ", e);
      return;
    } finally {
      try {
        inputStream.close();
      } catch (IOException e) {
        // Ignore
      }
    }

    getServer().setCatalina(this);

    // Stream redirection
    initStreams();

    // Start the new server
    try {
      getServer().init();
    } catch (LifecycleException e) {
      if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {
        throw new java.lang.Error(e);
      } else {
        log.error("Catalina.start", e);
      }
    }

    long t2 = System.nanoTime();
    if (log.isInfoEnabled()) {
      log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");
    }
  }
Esempio n. 4
0
  public void stopServer(String[] arguments) {

    if (arguments != null) {
      arguments(arguments);
    }

    Server s = getServer();
    if (s == null) {
      // Create and execute our Digester
      Digester digester = createStopDigester();
      File file = configFile();
      FileInputStream fis = null;
      try {
        InputSource is = new InputSource(file.toURI().toURL().toString());
        fis = new FileInputStream(file);
        is.setByteStream(fis);
        digester.push(this);
        digester.parse(is);
      } catch (Exception e) {
        log.error("Catalina.stop: ", e);
        System.exit(1);
      } finally {
        if (fis != null) {
          try {
            fis.close();
          } catch (IOException e) {
            // Ignore
          }
        }
      }
    } else {
      // Server object already present. Must be running as a service
      try {
        s.stop();
      } catch (LifecycleException e) {
        log.error("Catalina.stop: ", e);
      }
      return;
    }

    // Stop the existing server
    s = getServer();
    if (s.getPort() > 0) {
      Socket socket = null;
      OutputStream stream = null;
      try {
        socket = new Socket(s.getAddress(), s.getPort());
        stream = socket.getOutputStream();
        String shutdown = s.getShutdown();
        for (int i = 0; i < shutdown.length(); i++) {
          stream.write(shutdown.charAt(i));
        }
        stream.flush();
      } catch (ConnectException ce) {
        log.error(
            sm.getString(
                "catalina.stopServer.connectException",
                s.getAddress(),
                String.valueOf(s.getPort())));
        log.error("Catalina.stop: ", ce);
        System.exit(1);
      } catch (IOException e) {
        log.error("Catalina.stop: ", e);
        System.exit(1);
      } finally {
        if (stream != null) {
          try {
            stream.close();
          } catch (IOException e) {
            // Ignore
          }
        }
        if (socket != null) {
          try {
            socket.close();
          } catch (IOException e) {
            // Ignore
          }
        }
      }
    } else {
      log.error(sm.getString("catalina.stopServer"));
      System.exit(1);
    }
  }
  /** Start a new server instance. */
  public void load() {

    long t1 = System.nanoTime();

    initDirs();

    // Before digester - it may be needed

    initNaming();

    // Create and execute our Digester
    Digester digester = createStartDigester();

    InputSource inputSource = null;
    InputStream inputStream = null;
    File file = null;
    try {
      file = configFile();
      inputStream = new FileInputStream(file);
      inputSource = new InputSource("file://" + file.getAbsolutePath());
    } catch (Exception e) {;
    }
    if (inputStream == null) {
      try {
        inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());
        inputSource =
            new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());
      } catch (Exception e) {;
      }
    }

    // This should be included in catalina.jar
    // Alternative: don't bother with xml, just create it manually.
    if (inputStream == null) {
      try {
        inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
        inputSource =
            new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());
      } catch (Exception e) {;
      }
    }

    if ((inputStream == null) && (file != null)) {
      log.warn("Can't load server.xml from " + file.getAbsolutePath());
      if (file.exists() && !file.canRead()) {
        log.warn("Permissions incorrect, read permission is not allowed on the file.");
      }
      return;
    }

    try {
      inputSource.setByteStream(inputStream);
      digester.push(this);
      digester.parse(inputSource);
      inputStream.close();
    } catch (Exception e) {
      log.warn("Catalina.start using " + getConfigFile() + ": ", e);
      return;
    }

    // Stream redirection
    initStreams();

    // Start the new server
    if (getServer() instanceof Lifecycle) {
      try {
        getServer().initialize();
      } catch (LifecycleException e) {
        if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"))
          throw new java.lang.Error(e);
        else log.error("Catalina.start", e);
      }
    }

    long t2 = System.nanoTime();
    if (log.isInfoEnabled())
      log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");
  }