Search in sources :

Example 1 with WebappClassLoader

use of org.apache.catalina.loader.WebappClassLoader in project uavstack by uavorg.

the class TomcatPlusIT method onResourceCreate.

/**
 * on Resource Create
 */
public Object onResourceCreate(Object... args) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        /**
         * after tomcat8, tomcat use ParallelWebappClassLoader instead of WebappClassLoader as it's webapp's
         * classloader, both of them are extends WebappClassLoaderBase
         */
        Class<?> cls = cl.loadClass("org.apache.catalina.loader.WebappClassLoaderBase");
        if (!cls.isAssignableFrom(cl.getClass())) {
            return args[0];
        }
    } catch (ClassNotFoundException e) {
        /**
         * before tomcat7.0.64(include), WebappClassLoaderBase doesn't exist
         */
        if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
            return args[0];
        }
    }
    /**
     * for Application Starting's Resource Create
     */
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE, false);
    if (context == null) {
        return args[0];
    }
    StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
    if (sc == null) {
        return args[0];
    }
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    context.put(InterceptConstants.CONTEXTPATH, ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true));
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    getBasePath(context, sContext);
    context.put(InterceptConstants.RESOURCEOBJ, args[0]);
    context.put(InterceptConstants.RESOURCECFG, args[1]);
    iSupport.doIntercept(context);
    InterceptContext ic = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE);
    ic.put(InterceptConstants.CONTEXTOBJ, sc);
    return context.get(InterceptConstants.RESOURCEOBJ);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) ServletContext(javax.servlet.ServletContext)

Example 2 with WebappClassLoader

use of org.apache.catalina.loader.WebappClassLoader in project uavstack by uavorg.

the class SpringBootTomcatPlusIT method onResourceInit.

@Override
public void onResourceInit(Object... args) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
        return;
    }
    /**
     * for Application Starting's Resource Init
     */
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
    /**
     * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do
     * anything on this we may use its webappclassloader's parent as the classloader
     */
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader().getParent());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
    context.put(InterceptConstants.CONTEXTPATH, contextPath);
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    String basePath = sContext.getRealPath("");
    /*
         * NOTE: springboot couldn't get the basePath through method "getRealPath", temporary process
         */
    if (basePath == null) {
        basePath = "";
    } else if (basePath.lastIndexOf("/") == (basePath.length() - 1) || basePath.lastIndexOf("\\") == (basePath.length() - 1)) {
        basePath = basePath.substring(0, basePath.length() - 1);
    }
    context.put(InterceptConstants.BASEPATH, basePath);
    iSupport.doIntercept(context);
}
Also used : WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) ServletContext(javax.servlet.ServletContext)

Example 3 with WebappClassLoader

use of org.apache.catalina.loader.WebappClassLoader in project uavstack by uavorg.

the class SpringBootTomcatPlusIT method onResourceCreate.

@Override
public Object onResourceCreate(Object... args) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
        return args[0];
    }
    /**
     * for Application Starting's Resource Create
     */
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE);
    StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
    /**
     * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do
     * anything on this we may use its webappclassloader's parent as the classloader
     */
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader().getParent());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
    context.put(InterceptConstants.CONTEXTPATH, contextPath);
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    String basePath = sContext.getRealPath("");
    /*
         * NOTE: springboot couldn't get the basePath through method "getRealPath", temporary process
         */
    if (basePath == null) {
        basePath = "";
    } else if (basePath.lastIndexOf("/") == (basePath.length() - 1) || basePath.lastIndexOf("\\") == (basePath.length() - 1)) {
        basePath = basePath.substring(0, basePath.length() - 1);
    }
    context.put(InterceptConstants.BASEPATH, basePath);
    context.put(InterceptConstants.RESOURCEOBJ, args[0]);
    context.put(InterceptConstants.RESOURCECFG, args[1]);
    iSupport.doIntercept(context);
    return context.get(InterceptConstants.RESOURCEOBJ);
}
Also used : WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) ServletContext(javax.servlet.ServletContext)

Example 4 with WebappClassLoader

use of org.apache.catalina.loader.WebappClassLoader in project uavstack by uavorg.

the class TomcatPlusIT method onResourceInit.

/**
 * on Resource Init
 *
 * @param args
 */
public void onResourceInit(Object... args) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        /**
         * after tomcat8, tomcat use ParallelWebappClassLoader instead of WebappClassLoader as it's webapp's
         * classloader, both of them are extends WebappClassLoaderBase
         */
        Class<?> cls = cl.loadClass("org.apache.catalina.loader.WebappClassLoaderBase");
        if (!cls.isAssignableFrom(cl.getClass())) {
            return;
        }
    } catch (ClassNotFoundException e) {
        /**
         * before tomcat7.0.64(include), WebappClassLoaderBase doesn't exist
         */
        if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
            return;
        }
    }
    /**
     * for Application Starting's Resource Init
     */
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    if (context == null) {
        return;
    }
    StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
    if (sc == null) {
        return;
    }
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    context.put(InterceptConstants.CONTEXTPATH, ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true));
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    getBasePath(context, sContext);
    iSupport.doIntercept(context);
    InterceptContext ic = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    ic.put(InterceptConstants.CONTEXTOBJ, sc);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) ServletContext(javax.servlet.ServletContext)

Example 5 with WebappClassLoader

use of org.apache.catalina.loader.WebappClassLoader in project uavstack by uavorg.

the class TomcatPlusIT method chgParentClassloader.

/**
 *  when use embeddedTomcat and the webappclassloader is undefined, the webappclassloader will use systemclassloader as it's parentclassloader which coundn't load mof jars.
 *  we chg it's parentclassloader to currentThread's contextclassloader(normally it counld load uavmof jars).
 */
public ClassLoader chgParentClassloader(Object... args) {
    ClassLoader cl = (ClassLoader) args[0];
    StandardContext sc = (StandardContext) args[1];
    if (cl != ClassLoader.getSystemClassLoader()) {
        return cl;
    } else {
        sc.setDelegate(true);
        return Thread.currentThread().getContextClassLoader();
    }
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)5 WebappClassLoader (org.apache.catalina.loader.WebappClassLoader)5 InterceptSupport (com.creditease.monitor.interceptframework.InterceptSupport)4 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)4 ServletContext (javax.servlet.ServletContext)4