org.apache.catalina.loader.WebappClassLoader Java Examples

The following examples show how to use org.apache.catalina.loader.WebappClassLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TomcatInstrumentableClassLoader.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static void shallowCopyFieldState(final WebappClassLoader src, final WebappClassLoader dest) {
	Class<?> targetClass = WebappClassLoader.class;
	// Keep backing up the inheritance hierarchy.
	do {
		Field[] fields = targetClass.getDeclaredFields();
		for (Field field : fields) {
			// Do not copy resourceEntries - it's a cache that holds class entries.
			if (!(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()) ||
					field.getName().equals("resourceEntries"))) {
				try {
					field.setAccessible(true);
					Object srcValue = field.get(src);
					field.set(dest, srcValue);
				}
				catch (IllegalAccessException ex) {
					throw new IllegalStateException(
							"Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
				}
			}
		}
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);
}
 
Example #2
Source File: TomcatInstrumentableClassLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Delegate for LoadTimeWeaver's {@code getThrowawayClassLoader} method.
 * Typically called through ReflectiveLoadTimeWeaver.
 * @see org.springframework.instrument.classloading.LoadTimeWeaver#getThrowawayClassLoader
 * @see org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver
 */
public ClassLoader getThrowawayClassLoader() {
	WebappClassLoader tempLoader = new WebappClassLoader();
	// Use reflection to copy all the fields since they are not exposed any other way.
	shallowCopyFieldState(this, tempLoader);
	return tempLoader;
}
 
Example #3
Source File: SpringBootTomcatPlusIT.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #4
Source File: SpringBootTomcatPlusIT.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #5
Source File: TomcatPlusIT.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: TomcatPlusIT.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}