Search in sources :

Example 11 with StandardServer

use of org.apache.catalina.core.StandardServer in project tomee by apache.

the class TomcatHelper method getServer.

public static StandardServer getServer() {
    StandardServer server = null;
    try {
        final Class<?> systemInstanceClass = Thread.currentThread().getContextClassLoader().loadClass("org.apache.openejb.loader.SystemInstance");
        final Object instance = systemInstanceClass.getDeclaredMethod("get").invoke(null);
        server = (StandardServer) systemInstanceClass.getDeclaredMethod("getComponent", Class.class).invoke(instance, StandardServer.class);
    } catch (final Exception classNotFoundException) {
    // ignored
    }
    if (server != null) {
        TomcatHelper.server = server;
        return server;
    }
    try {
        final Class<?> tomcatServerFactory = Class.forName("org.apache.catalina.ServerFactory");
        final Method getServerMethod = tomcatServerFactory.getMethod("getServer");
        server = (StandardServer) getServerMethod.invoke(null);
    } catch (final Exception e) {
    // ignored
    }
    if (server != null) {
        TomcatHelper.server = server;
        return server;
    }
    if (TomcatHelper.server != null) {
        // try it before next one otherwise we depend on "Catalina" name which can change
        return TomcatHelper.server;
    }
    // if this fails, we'll try and get a reference from the platform mbean server
    try {
        final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        server = (StandardServer) mbeanServer.getAttribute(new ObjectName("Catalina:type=Server"), "managedResource");
    } catch (final Exception e) {
    // ignored
    }
    if (server != null) {
        TomcatHelper.server = server;
        return server;
    }
    // if this still fails, that's too bad.
    return TomcatHelper.server;
}
Also used : StandardServer(org.apache.catalina.core.StandardServer) Method(java.lang.reflect.Method) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

StandardServer (org.apache.catalina.core.StandardServer)11 File (java.io.File)5 Properties (java.util.Properties)3 Service (org.apache.catalina.Service)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 URL (java.net.URL)2 Connector (org.apache.catalina.connector.Connector)2 AprLifecycleListener (org.apache.catalina.core.AprLifecycleListener)2 StandardHost (org.apache.catalina.core.StandardHost)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 Container (org.apache.catalina.Container)1 Engine (org.apache.catalina.Engine)1