Search in sources :

Example 1 with Server

use of org.apache.catalina.Server in project tomcat by apache.

the class SetParentClassLoaderRule method stopServer.

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();
        try (FileInputStream fis = new FileInputStream(file)) {
            InputSource is = new InputSource(file.toURI().toURL().toString());
            is.setByteStream(fis);
            digester.push(this);
            digester.parse(is);
        } catch (Exception e) {
            log.error("Catalina.stop: ", e);
            System.exit(1);
        }
    } 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) {
        try (Socket socket = new Socket(s.getAddress(), s.getPort());
            OutputStream 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);
        }
    } else {
        log.error(sm.getString("catalina.stopServer"));
        System.exit(1);
    }
}
Also used : InputSource(org.xml.sax.InputSource) LifecycleException(org.apache.catalina.LifecycleException) Server(org.apache.catalina.Server) OutputStream(java.io.OutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) File(java.io.File) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 2 with Server

use of org.apache.catalina.Server in project bamboobsc by billchen198318.

the class HostUtils method getHttpPort.

public static int getHttpPort() {
    int port = 8080;
    MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    ObjectName name;
    try {
        name = new ObjectName("Catalina", "type", "Server");
        try {
            Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
            Service[] services = server.findServices();
            for (Service service : services) {
                for (Connector connector : service.findConnectors()) {
                    ProtocolHandler protocolHandler = connector.getProtocolHandler();
                    if (protocolHandler instanceof Http11Protocol || protocolHandler instanceof Http11AprProtocol || protocolHandler instanceof Http11NioProtocol) {
                        port = connector.getPort();
                    }
                }
            }
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
        } catch (MBeanException e) {
            e.printStackTrace();
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
    } catch (MalformedObjectNameException e) {
        e.printStackTrace();
    }
    return port;
}
Also used : Connector(org.apache.catalina.connector.Connector) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MalformedObjectNameException(javax.management.MalformedObjectNameException) Http11NioProtocol(org.apache.coyote.http11.Http11NioProtocol) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) InstanceNotFoundException(javax.management.InstanceNotFoundException) Service(org.apache.catalina.Service) Http11AprProtocol(org.apache.coyote.http11.Http11AprProtocol) ObjectName(javax.management.ObjectName) ProtocolHandler(org.apache.coyote.ProtocolHandler) Http11Protocol(org.apache.coyote.http11.Http11Protocol) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer)

Example 3 with Server

use of org.apache.catalina.Server in project tomcat70 by apache.

the class MBeanFactory method createStandardServiceEngine.

/**
 * Creates a new StandardService and StandardEngine.
 *
 * @param domain       Domain name for the container instance
 * @param defaultHost  Name of the default host to be used in the Engine
 * @param baseDir      Base directory value for Engine
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardServiceEngine(String domain, String defaultHost, String baseDir) throws Exception {
    if (!(container instanceof Server)) {
        throw new Exception("Container not Server");
    }
    StandardEngine engine = new StandardEngine();
    engine.setDomain(domain);
    engine.setName(domain);
    engine.setDefaultHost(defaultHost);
    engine.setBaseDir(baseDir);
    Service service = new StandardService();
    service.setContainer(engine);
    service.setName(domain);
    ((Server) container).addService(service);
    return engine.getObjectName().toString();
}
Also used : StandardEngine(org.apache.catalina.core.StandardEngine) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) StandardService(org.apache.catalina.core.StandardService) Service(org.apache.catalina.Service) StandardService(org.apache.catalina.core.StandardService)

Example 4 with Server

use of org.apache.catalina.Server in project tomcat70 by apache.

the class MBeanUtils method createObjectName.

/**
 * Create an <code>ObjectName</code> for this
 * <code>ContextResourceLink</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param resourceLink The ContextResourceLink to be named
 *
 * @exception MalformedObjectNameException if a name cannot be created
 */
public static ObjectName createObjectName(String domain, ContextResourceLink resourceLink) throws MalformedObjectNameException {
    ObjectName name = null;
    String quotedResourceLinkName = ObjectName.quote(resourceLink.getName());
    Object container = resourceLink.getNamingResources().getContainer();
    if (container instanceof Server) {
        name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Global" + ",name=" + quotedResourceLinkName);
    } else if (container instanceof Context) {
        Context context = ((Context) container);
        ContextName cn = new ContextName(context.getName(), false);
        Container host = context.getParent();
        name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Context,context=" + cn.getDisplayName() + ",host=" + host.getName() + ",name=" + quotedResourceLinkName);
    }
    return (name);
}
Also used : Context(org.apache.catalina.Context) Container(org.apache.catalina.Container) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) ObjectName(javax.management.ObjectName) ContextName(org.apache.catalina.util.ContextName)

Example 5 with Server

use of org.apache.catalina.Server in project tomcat70 by apache.

the class MBeanUtils method createObjectName.

/**
 * Create an <code>ObjectName</code> for this
 * <code>Service</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param environment The ContextEnvironment to be named
 *
 * @exception MalformedObjectNameException if a name cannot be created
 */
public static ObjectName createObjectName(String domain, ContextEnvironment environment) throws MalformedObjectNameException {
    ObjectName name = null;
    Object container = environment.getNamingResources().getContainer();
    if (container instanceof Server) {
        name = new ObjectName(domain + ":type=Environment" + ",resourcetype=Global,name=" + environment.getName());
    } else if (container instanceof Context) {
        Context context = ((Context) container);
        ContextName cn = new ContextName(context.getName(), false);
        Container host = context.getParent();
        name = new ObjectName(domain + ":type=Environment" + ",resourcetype=Context,context=" + cn.getDisplayName() + ",host=" + host.getName() + ",name=" + environment.getName());
    }
    return (name);
}
Also used : Context(org.apache.catalina.Context) Container(org.apache.catalina.Container) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) ObjectName(javax.management.ObjectName) ContextName(org.apache.catalina.util.ContextName)

Aggregations

Server (org.apache.catalina.Server)44 Context (org.apache.catalina.Context)20 MBeanServer (javax.management.MBeanServer)18 ObjectName (javax.management.ObjectName)18 Container (org.apache.catalina.Container)11 Service (org.apache.catalina.Service)8 Host (org.apache.catalina.Host)7 LifecycleException (org.apache.catalina.LifecycleException)7 NamingContext (org.apache.naming.NamingContext)7 IOException (java.io.IOException)6 StandardService (org.apache.catalina.core.StandardService)6 ContextName (org.apache.catalina.util.ContextName)6 NamingException (javax.naming.NamingException)5 File (java.io.File)4 ServletContext (javax.servlet.ServletContext)4 Engine (org.apache.catalina.Engine)4 OutputStream (java.io.OutputStream)3 ConnectException (java.net.ConnectException)3 Socket (java.net.Socket)3 LogManager (java.util.logging.LogManager)3