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);
    }
  }
Example #2
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);
    }
  }