Example usage for org.springframework.util ClassUtils overrideThreadContextClassLoader

List of usage examples for org.springframework.util ClassUtils overrideThreadContextClassLoader

Introduction

In this page you can find the example usage for org.springframework.util ClassUtils overrideThreadContextClassLoader.

Prototype

@Nullable
public static ClassLoader overrideThreadContextClassLoader(@Nullable ClassLoader classLoaderToUse) 

Source Link

Document

Override the thread context ClassLoader with the environment's bean ClassLoader if necessary, i.e.

Usage

From source file:com.zuoxiaolong.niubi.job.spring.bean.SpringJobBeanFactory.java

/**
 * /* www  . jav  a2s.  c  om*/
 *
 * @throws BeansException
 */
public SpringJobBeanFactory(ClassLoader classLoader) throws BeansException {
    this.classLoader = classLoader;
    ClassUtils.overrideThreadContextClassLoader(classLoader);
    this.applicationContext = new ClassPathXmlApplicationContext(JobScanner.APPLICATION_CONTEXT_XML_PATH);
}

From source file:sample.tomcat.jndi.SampleTomcatJndiApplication.java

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override/*from  w w w  . j  a  va2 s  .c o m*/
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            tomcat.enableNaming();
            TomcatEmbeddedServletContainer container = super.getTomcatEmbeddedServletContainer(tomcat);
            Container context = container.getTomcat().getHost().findChild("");
            ClassUtils.overrideThreadContextClassLoader(context.getLoader().getClassLoader());
            return container;
        }

        @Override
        protected void postProcessContext(Context context) {
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/myDataSource");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", "org.hsqldb.jdbc.JDBCDriver");
            resource.setProperty("url", "jdbc:hsqldb:mem:mymemdb");

            context.getNamingResources().addResource(resource);
        }
    };
}

From source file:demo.tomcat.TomcatEmbeddedContext.java

public void deferredLoadOnStartup() {
    // Some older Servlet frameworks (e.g. Struts, BIRT) use the Thread context class
    // loader to create servlet instances in this phase. If they do that and then try
    // to initialize them later the class loader may have changed, so wrap the call to
    // loadOnStartup in what we think its going to be the main webapp classloader at
    // runtime./*from   w  w w  . j  av  a2s. co m*/
    ClassLoader classLoader = getLoader().getClassLoader();
    ClassLoader existingLoader = null;
    if (classLoader != null) {
        existingLoader = ClassUtils.overrideThreadContextClassLoader(classLoader);
    }

    if (this.overrideLoadOnStart) {
        // Earlier versions of Tomcat used a version that returned void. If that
        // version is used our overridden loadOnStart method won't have been called
        // and the original will have already run.
        super.loadOnStartup(findChildren());
    }
    if (existingLoader != null) {
        ClassUtils.overrideThreadContextClassLoader(existingLoader);
    }
}

From source file:net.testdriven.psiprobe.beans.LogResolverBean.java

public LogDestination getLogDestination(String logType, String webapp, boolean context, boolean root,
        String logName, String logIndex) {
    Context ctx = null;//from  w  w w  .jav a 2 s  . com
    Application application = null;
    if (webapp != null) {
        ctx = getContainerWrapper().getTomcatContainer().findContext(webapp);
        if (ctx != null) {
            application = ApplicationUtils.getApplication(ctx);
        }
    }

    if ("stdout".equals(logType) && logName != null) {
        return getStdoutLogDestination(logName);
    } else if ("catalina".equals(logType) && ctx != null) {
        return getCatalinaLogDestination(ctx, application);
    } else if (logIndex != null
            && ("jdk".equals(logType) || "log4j".equals(logType) || "logback".equals(logType))) {
        if (context && ctx != null) {
            return getCommonsLogDestination(ctx, application, logIndex);
        }
        ClassLoader cl;
        ClassLoader prevCl = null;
        if (ctx != null) {
            cl = ctx.getLoader().getClassLoader();
            prevCl = ClassUtils.overrideThreadContextClassLoader(cl);
        } else {
            cl = Thread.currentThread().getContextClassLoader().getParent();
        }
        try {
            if ((root || logName != null)) {
                if ("jdk".equals(logType)) {
                    return getJdk14LogDestination(cl, application, root, logName, logIndex);
                } else if ("log4j".equals(logType)) {
                    return getLog4JLogDestination(cl, application, root, logName, logIndex);
                } else if ("logback".equals(logType)) {
                    return getLogbackLogDestination(cl, application, root, logName, logIndex);
                }
            }
        } finally {
            if (prevCl != null) {
                ClassUtils.overrideThreadContextClassLoader(prevCl);
            }
        }
    }
    return null;
}

From source file:com.googlecode.psiprobe.beans.LogResolverBean.java

public LogDestination getLogDestination(String logType, String webapp, boolean context, boolean root,
        String logName, String logIndex) {
    Context ctx = null;//from w  w  w .jav  a  2  s. c  o  m
    Application application = null;
    if (webapp != null) {
        ctx = getContainerWrapper().getTomcatContainer().findContext(webapp);
        if (ctx != null) {
            application = ApplicationUtils.getApplication(ctx, getContainerWrapper());
        }
    }

    if ("stdout".equals(logType) && logName != null) {
        return getStdoutLogDestination(logName);
    } else if ("catalina".equals(logType) && ctx != null) {
        return getCatalinaLogDestination(ctx, application);
    } else if (logIndex != null
            && ("jdk".equals(logType) || "log4j".equals(logType) || "logback".equals(logType))
            || "tomcatSlf4jLogback".equals(logType)) {
        if (context && ctx != null) {
            return getCommonsLogDestination(ctx, application, logIndex);
        }
        ClassLoader cl;
        ClassLoader prevCl = null;
        if (ctx != null) {
            cl = ctx.getLoader().getClassLoader();
            prevCl = ClassUtils.overrideThreadContextClassLoader(cl);
        } else {
            cl = Thread.currentThread().getContextClassLoader().getParent();
        }
        try {
            if ((root || logName != null) && logIndex != null) {
                if ("jdk".equals(logType)) {
                    return getJdk14LogDestination(cl, application, root, logName, logIndex);
                } else if ("log4j".equals(logType)) {
                    return getLog4JLogDestination(cl, application, root, logName, logIndex);
                } else if ("logback".equals(logType)) {
                    return getLogbackLogDestination(cl, application, root, logName, logIndex);
                } else if ("tomcatSlf4jLogback".equals(logType)) {
                    return getLogbackTomcatJuliLogDestination(cl, application, root, logName, logIndex);
                }
            }
        } finally {
            if (prevCl != null) {
                ClassUtils.overrideThreadContextClassLoader(prevCl);
            }
        }
    }
    return null;
}

From source file:psiprobe.beans.LogResolverBean.java

/**
 * Gets the log destination./*from   w  w w .  j  a  v  a 2s . c o  m*/
 *
 * @param logType the log type
 * @param webapp the webapp
 * @param context the context
 * @param root the root
 * @param logName the log name
 * @param logIndex the log index
 * @return the log destination
 */
public LogDestination getLogDestination(String logType, String webapp, boolean context, boolean root,
        String logName, String logIndex) {

    Context ctx = null;
    Application application = null;
    if (webapp != null) {
        ctx = getContainerWrapper().getTomcatContainer().findContext(webapp);
        if (ctx != null) {
            application = ApplicationUtils.getApplication(ctx, getContainerWrapper());
        }
    }

    if ("stdout".equals(logType) && logName != null) {
        return getStdoutLogDestination(logName);
    } else if ("catalina".equals(logType) && ctx != null) {
        return getCatalinaLogDestination(ctx, application);
    } else if (logIndex != null
            && ("jdk".equals(logType) || "log4j".equals(logType) || "logback".equals(logType))
            || "tomcatSlf4jLogback".equals(logType)) {
        if (context && ctx != null) {
            return getCommonsLogDestination(ctx, application, logIndex);
        }
        ClassLoader cl;
        ClassLoader prevCl = null;
        if (ctx != null) {
            cl = ctx.getLoader().getClassLoader();
            prevCl = ClassUtils.overrideThreadContextClassLoader(cl);
        } else {
            cl = Thread.currentThread().getContextClassLoader().getParent();
        }
        try {
            if ((root || logName != null) && logIndex != null) {
                if ("jdk".equals(logType)) {
                    return getJdk14LogDestination(cl, application, root, logName, logIndex);
                } else if ("log4j".equals(logType)) {
                    return getLog4JLogDestination(cl, application, root, logName, logIndex);
                } else if ("logback".equals(logType)) {
                    return getLogbackLogDestination(cl, application, root, logName, logIndex);
                } else if ("tomcatSlf4jLogback".equals(logType)) {
                    return getLogbackTomcatJuliLogDestination(cl, application, root, logName, logIndex);
                }
            }
        } finally {
            if (prevCl != null) {
                ClassUtils.overrideThreadContextClassLoader(prevCl);
            }
        }
    }
    return null;
}

From source file:com.googlecode.psiprobe.AbstractTomcatContainer.java

/**
 * Compiles a list of JSPs. Names of JSP files are expected to be relative to the webapp root. The method
 * updates summary with compilation details.
 *
 * @param context/*from   ww  w. j  a  va 2s. co  m*/
 * @param summary
 * @param names
 */
public void recompileJsps(Context context, Summary summary, List names) {
    ServletConfig servletConfig = (ServletConfig) context.findChild("jsp");
    if (servletConfig != null) {
        if (summary != null) {
            synchronized (servletConfig) {
                ServletContext sctx = context.getServletContext();
                Options opt = new EmbeddedServletOptions(servletConfig, sctx);

                JspRuntimeContext jrctx = new JspRuntimeContext(sctx, opt);
                try {
                    //
                    // we need to pass context classloader here, so the jsps can reference /WEB-INF/classes and
                    // /WEB-INF/lib. JspCompilationContext would only take URLClassLoader, so we fake it
                    //
                    URLClassLoader classLoader = new URLClassLoader(new URL[] {},
                            context.getLoader().getClassLoader());
                    for (Iterator it = names.iterator(); it.hasNext();) {
                        String name = (String) it.next();
                        long time = System.currentTimeMillis();
                        JspCompilationContext jcctx = createJspCompilationContext(name, false, opt, sctx, jrctx,
                                classLoader);
                        ClassLoader prevCl = ClassUtils.overrideThreadContextClassLoader(classLoader);
                        try {
                            Item item = (Item) summary.getItems().get(name);
                            if (item != null) {
                                try {
                                    org.apache.jasper.compiler.Compiler c = jcctx.createCompiler();
                                    c.compile();
                                    item.setState(Item.STATE_READY);
                                    item.setException(null);
                                    logger.info("Compiled " + name + ": OK");
                                } catch (Exception e) {
                                    item.setState(Item.STATE_FAILED);
                                    item.setException(e);
                                    logger.info("Compiled " + name + ": FAILED", e);
                                }
                                item.setCompileTime(System.currentTimeMillis() - time);
                            } else {
                                logger.error(name + " is not on the summary list, ignored");
                            }
                        } finally {
                            ClassUtils.overrideThreadContextClassLoader(prevCl);
                        }
                    }
                } finally {
                    jrctx.destroy();
                }
            }
        } else {
            logger.error("summary is null for " + context.getName() + ", request ignored");
        }
    } else {
        logger.error("Context " + context.getName() + " does not have \"jsp\" servlet");
    }
}

From source file:net.testdriven.psiprobe.beans.LogResolverBean.java

private void interrogateContext(Context ctx, List<LogDestination> allAppenders) {
    Application application = ApplicationUtils.getApplication(ctx);
    ClassLoader cl = ctx.getLoader().getClassLoader();

    try {/*  w  w  w . ja  va  2  s.com*/
        Object contextLogger = getContainerWrapper().getTomcatContainer().getLogger(ctx);
        if (contextLogger != null) {
            if (contextLogger.getClass().getName().startsWith("org.apache.commons.logging")) {
                CommonsLoggerAccessor commonsAccessor = new CommonsLoggerAccessor();
                commonsAccessor.setTarget(contextLogger);
                commonsAccessor.setApplication(application);
                allAppenders.addAll(commonsAccessor.getDestinations());
            } else if (contextLogger.getClass().getName().startsWith("org.apache.catalina.logger")) {
                CatalinaLoggerAccessor catalinaAccessor = new CatalinaLoggerAccessor();
                catalinaAccessor.setApplication(application);
                catalinaAccessor.setTarget(contextLogger);
                allAppenders.add(catalinaAccessor);
            }
        }
    } catch (Throwable e) {
        logger.error("Could not interrogate context logger for " + ctx.getName()
                + ". Enable debug logging to see the trace stack");
        logger.debug("  Stack trace:", e);
        //
        // make sure we always re-throw ThreadDeath
        //
        if (e instanceof ThreadDeath) {
            throw (ThreadDeath) e;
        }
    }

    if (application.isAvailable()) {
        ClassLoader prevCl = ClassUtils.overrideThreadContextClassLoader(cl);
        try {
            interrogateClassLoader(cl, application, allAppenders);
        } catch (Exception e) {
            logger.error("Could not interrogate classloader loggers for " + ctx.getName()
                    + ". Enable debug logging to see the trace stack");
            logger.debug("  Stack trace:", e);
        } finally {
            if (prevCl != null) {
                ClassUtils.overrideThreadContextClassLoader(prevCl);
            }
        }
    }
}

From source file:com.googlecode.psiprobe.beans.LogResolverBean.java

private void interrogateContext(Context ctx, List allAppenders) {
    Application application = ApplicationUtils.getApplication(ctx, getContainerWrapper());
    ClassLoader cl = ctx.getLoader().getClassLoader();

    try {// w w  w.ja  va  2s .c  o m
        Object contextLogger = getContainerWrapper().getTomcatContainer().getLogger(ctx);
        if (contextLogger != null) {
            if (contextLogger.getClass().getName().startsWith("org.apache.commons.logging")) {
                CommonsLoggerAccessor commonsAccessor = new CommonsLoggerAccessor();
                commonsAccessor.setTarget(contextLogger);
                commonsAccessor.setApplication(application);
                allAppenders.addAll(commonsAccessor.getDestinations());
            } else if (contextLogger.getClass().getName().startsWith("org.apache.catalina.logger")) {
                CatalinaLoggerAccessor catalinaAccessor = new CatalinaLoggerAccessor();
                catalinaAccessor.setApplication(application);
                catalinaAccessor.setTarget(contextLogger);
                allAppenders.add(catalinaAccessor);
            }
        }
    } catch (Throwable e) {
        logger.error("Could not interrogate context logger for " + ctx.getName()
                + ". Enable debug logging to see the trace stack");
        logger.debug("  Stack trace:", e);
        //
        // make sure we always re-throw ThreadDeath
        //
        if (e instanceof ThreadDeath) {
            throw (ThreadDeath) e;
        }
    }

    if (application.isAvailable()) {
        ClassLoader prevCl = ClassUtils.overrideThreadContextClassLoader(cl);
        try {
            interrogateClassLoader(cl, application, allAppenders);
        } catch (Exception e) {
            logger.error("Could not interrogate classloader loggers for " + ctx.getName()
                    + ". Enable debug logging to see the trace stack");
            logger.debug("  Stack trace:", e);
        } finally {
            if (prevCl != null) {
                ClassUtils.overrideThreadContextClassLoader(prevCl);
            }
        }
    }
}

From source file:psiprobe.beans.LogResolverBean.java

/**
 * Interrogate context./* w  ww . j  ava2 s  .  c om*/
 *
 * @param ctx the ctx
 * @param allAppenders the all appenders
 */
private void interrogateContext(Context ctx, List<LogDestination> allAppenders) {
    Application application = ApplicationUtils.getApplication(ctx, getContainerWrapper());
    ClassLoader cl = ctx.getLoader().getClassLoader();

    try {
        Object contextLogger = ctx.getLogger();
        if (contextLogger != null) {
            if (contextLogger.getClass().getName().startsWith("org.apache.commons.logging")) {
                CommonsLoggerAccessor commonsAccessor = new CommonsLoggerAccessor();
                commonsAccessor.setTarget(contextLogger);
                commonsAccessor.setApplication(application);
                allAppenders.addAll(commonsAccessor.getDestinations());
            } else if (contextLogger.getClass().getName().startsWith("org.apache.catalina.logger")) {
                CatalinaLoggerAccessor catalinaAccessor = new CatalinaLoggerAccessor();
                catalinaAccessor.setApplication(application);
                catalinaAccessor.setTarget(contextLogger);
                allAppenders.add(catalinaAccessor);
            }
        }
    } catch (Exception e) {
        logger.error("Could not interrogate context logger for {}. Enable debug logging to see the trace stack",
                ctx.getName());
        logger.debug("  Stack trace:", e);
    }

    if (application.isAvailable()) {
        ClassLoader prevCl = ClassUtils.overrideThreadContextClassLoader(cl);
        try {
            interrogateClassLoader(cl, application, allAppenders);
        } catch (Exception e) {
            logger.error(
                    "Could not interrogate classloader loggers for {}. Enable debug logging to see the trace stack",
                    ctx.getName());
            logger.debug("  Stack trace:", e);
        } finally {
            if (prevCl != null) {
                ClassUtils.overrideThreadContextClassLoader(prevCl);
            }
        }
    }
}