Example usage for java.lang Thread getContextClassLoader

List of usage examples for java.lang Thread getContextClassLoader

Introduction

In this page you can find the example usage for java.lang Thread getContextClassLoader.

Prototype

@CallerSensitive
public ClassLoader getContextClassLoader() 

Source Link

Document

Returns the context ClassLoader for this thread.

Usage

From source file:net.lightbody.bmp.proxy.jetty.http.HttpContext.java

/** Stop the context.
 *//*  w  w w.  ja  v a2  s  .  co m*/
protected void doStop() throws Exception {
    if (_httpServer == null)
        throw new InterruptedException("Destroy called");

    synchronized (this) {
        // Notify the container for the stop
        Thread thread = Thread.currentThread();
        ClassLoader lastContextLoader = thread.getContextClassLoader();
        try {
            if (_loader != null)
                thread.setContextClassLoader(_loader);
            Iterator handlers = _handlers.iterator();
            while (handlers.hasNext()) {
                HttpHandler handler = (HttpHandler) handlers.next();
                if (handler.isStarted()) {
                    try {
                        handler.stop();
                    } catch (Exception e) {
                        log.warn(LogSupport.EXCEPTION, e);
                    }
                }
            }

            if (_requestLog != null)
                _requestLog.stop();
        } finally {
            thread.setContextClassLoader(lastContextLoader);
        }

        // TODO this is a poor test
        if (_loader instanceof ContextLoader) {
            ((ContextLoader) _loader).destroy();
            LogFactory.release(_loader);
        }

        _loader = null;
    }
    _resources.flushCache();
    _resources.stop();
}

From source file:net.lightbody.bmp.proxy.jetty.http.HttpContext.java

protected synchronized void doStart() throws Exception {
    if (isStarted())
        return;/* ww  w  .  ja  va  2 s. c  o m*/

    if (_httpServer.getServerClasses() != null)
        _serverClasses = _httpServer.getServerClasses();
    if (_httpServer.getSystemClasses() != null)
        _systemClasses = _httpServer.getSystemClasses();

    _resources.start();

    statsReset();

    if (_httpServer == null)
        throw new IllegalStateException("No server for " + this);

    // start the context itself
    _resources.getMimeMap();
    _resources.getEncodingMap();

    // Setup realm
    if (_userRealm == null && _authenticator != null) {
        _userRealm = _httpServer.getRealm(_realmName);
        if (_userRealm == null)
            log.warn("No Realm: " + _realmName);
    }

    // setup the context loader
    initClassLoader(false);

    // Set attribute if needed
    String attr = getInitParameter(__fileClassPathAttr);
    if (attr != null && attr.length() > 0)
        setAttribute(attr, getFileClassPath());

    // Start the handlers
    Thread thread = Thread.currentThread();
    ClassLoader lastContextLoader = thread.getContextClassLoader();
    try {
        if (_loader != null)
            thread.setContextClassLoader(_loader);

        if (_requestLog != null)
            _requestLog.start();

        startHandlers();
    } finally {
        thread.setContextClassLoader(lastContextLoader);
        getHandlers();
    }

}

From source file:org.codehaus.groovy.ant.Groovy.java

/**
 * Exec the statement.//from   w  ww  .  j  av a 2  s  . c o m
 *
 * @param txt the groovy source to exec
 * @param out not used?
 */
protected void execGroovy(final String txt, final PrintStream out) {
    log.debug("execGroovy()");

    // Check and ignore empty statements
    if ("".equals(txt.trim())) {
        return;
    }

    log.verbose("Script: " + txt);
    if (classpath != null) {
        log.debug("Explicit Classpath: " + classpath.toString());
    }

    if (fork) {
        log.debug("Using fork mode");
        try {
            createClasspathParts();
            createNewArgs(txt);
            super.setFork(fork);
            super.setClassname(useGroovyShell ? "groovy.lang.GroovyShell" : "org.codehaus.groovy.ant.Groovy");
            configureCompiler();
            super.execute();
        } catch (Exception e) {
            StringWriter writer = new StringWriter();
            new ErrorReporter(e, false).write(new PrintWriter(writer));
            String message = writer.toString();
            throw new BuildException("Script Failed: " + message, e, getLocation());
        }
        return;
    }

    Object mavenPom = null;
    final Project project = getProject();
    final ClassLoader baseClassLoader;
    ClassLoader savedLoader = null;
    final Thread thread = Thread.currentThread();
    boolean maven = "org.apache.commons.grant.GrantProject".equals(project.getClass().getName());
    // treat the case Ant is run through Maven, and
    if (maven) {
        if (contextClassLoader) {
            throw new BuildException("Using setContextClassLoader not permitted when using Maven.",
                    getLocation());
        }
        try {
            final Object propsHandler = project.getClass().getMethod("getPropsHandler").invoke(project);
            final Field contextField = propsHandler.getClass().getDeclaredField("context");
            contextField.setAccessible(true);
            final Object context = contextField.get(propsHandler);
            mavenPom = InvokerHelper.invokeMethod(context, "getProject", new Object[0]);
        } catch (Exception e) {
            throw new BuildException("Impossible to retrieve Maven's Ant project: " + e.getMessage(),
                    getLocation());
        }
        // load groovy into "root.maven" classloader instead of "root" so that
        // groovy script can access Maven classes
        baseClassLoader = mavenPom.getClass().getClassLoader();
    } else {
        baseClassLoader = GroovyShell.class.getClassLoader();
    }
    if (contextClassLoader || maven) {
        savedLoader = thread.getContextClassLoader();
        thread.setContextClassLoader(GroovyShell.class.getClassLoader());
    }

    final String scriptName = computeScriptName();
    final GroovyClassLoader classLoader = new GroovyClassLoader(baseClassLoader);
    addClassPathes(classLoader);
    configureCompiler();
    final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
    try {
        parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
    } finally {
        groovy.resetLoadedClasses();
        groovy.getClassLoader().clearCache();
        if (contextClassLoader || maven)
            thread.setContextClassLoader(savedLoader);
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

/**
 * Invoked before a region is closed//from  ww  w.  jav a2 s.c o  m
 * @param abortRequested true if the server is aborting
 */
public void preClose(final boolean abortRequested) throws IOException {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).preClose(ctx, abortRequested);
            } catch (Throwable e) {
                handleCoprocessorThrowable(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

/**
 * Invoked after a region is closed/*from   w  w w .  j  av a2s.  c o m*/
 * @param abortRequested true if the server is aborting
 */
public void postClose(final boolean abortRequested) {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).postClose(ctx, abortRequested);
            } catch (Throwable e) {
                handleCoprocessorThrowableNoRethrow(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }
        }
        shutdown(env);
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

public void preSplitAfterPONR() throws IOException {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).preSplitAfterPONR(ctx);
            } catch (Throwable e) {
                handleCoprocessorThrowable(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }/*from  ww w  . j  a  va2s. com*/
            if (ctx.shouldComplete()) {
                break;
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

public void postStartRegionOperation(final Operation op) throws IOException {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).postStartRegionOperation(ctx, op);
            } catch (Throwable e) {
                handleCoprocessorThrowable(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }//from w  w w. j  a v a 2  s  .  co m
            if (ctx.shouldComplete()) {
                break;
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

public void postCloseRegionOperation(final Operation op) throws IOException {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).postCloseRegionOperation(ctx, op);
            } catch (Throwable e) {
                handleCoprocessorThrowable(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }/*from  w  w  w  .ja  va 2s . c  om*/
            if (ctx.shouldComplete()) {
                break;
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

/**
 * Invoked before a region open./*w ww.j a v a 2 s  .c o m*/
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void preOpen() throws IOException {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).preOpen(ctx);
            } catch (Throwable e) {
                handleCoprocessorThrowable(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }
            if (ctx.shouldComplete()) {
                break;
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost.java

/**
 * Invoked after a region open/*www .j a  v a  2  s  .co  m*/
 */
public void postOpen() {
    ObserverContext<RegionCoprocessorEnvironment> ctx = null;
    for (RegionEnvironment env : coprocessors) {
        if (env.getInstance() instanceof RegionObserver) {
            ctx = ObserverContext.createAndPrepare(env, ctx);
            Thread currentThread = Thread.currentThread();
            ClassLoader cl = currentThread.getContextClassLoader();
            try {
                currentThread.setContextClassLoader(env.getClassLoader());
                ((RegionObserver) env.getInstance()).postOpen(ctx);
            } catch (Throwable e) {
                handleCoprocessorThrowableNoRethrow(env, e);
            } finally {
                currentThread.setContextClassLoader(cl);
            }
            if (ctx.shouldComplete()) {
                break;
            }
        }
    }
}