@SuppressWarnings("rawtypes") private static <R, E extends Exception> R doContextAction(PluginClassLoader classloader, LimbusContextAction<R, E> callable) throws E { Lang.denyNull("Context action", callable); // Safe the current context classloader ClassLoader contextClassLoaderBefore = Thread.currentThread() .getContextClassLoader(); // Take the thread local before snaphsot. Set<ThreadLocal> beforeActionSnapshot = LimbusUtil.getCurrentThreadLocals(); try { // Set the plugin context Thread.currentThread() .setContextClassLoader(classloader); // Perform the actual action R retVal = callable.doAction(); // Return the actual compuation. return retVal; } finally { // Restore the old context classloader Thread.currentThread() .setContextClassLoader(contextClassLoaderBefore); // Take the thread local after snaphsot. Set<ThreadLocal> afterActionSnapshot = LimbusUtil.getCurrentThreadLocals(); // Remove all thread locals added by this context action. LimbusUtil.removeAddedThreadLocales(beforeActionSnapshot, afterActionSnapshot); LimbusUtil.addThreadLocals(beforeActionSnapshot); } }
@Override @SuppressWarnings("rawtypes") public <R, E extends Throwable> R doContextAction(LimbusContextAction<R, E> callable) throws E { Lang.denyNull("Context action", callable); // Safe the current context classloader ClassLoader contextClassLoaderBefore = Thread.currentThread() .getContextClassLoader(); // Take the thread local before snaphsot. Set<ThreadLocal> beforeActionSnapshot = getCurrentThreadLocals(); // Add the plugin's thread locals recorded before LimbusUtil.addThreadLocals(getThreadLocalsSet()); try { // Set the plugin context Thread.currentThread() .setContextClassLoader(getClassloader()); // Perform the actual action R retVal = callable.doAction(); // Return the actual compuation. return retVal; } finally { // Restore the old context classloader Thread.currentThread() .setContextClassLoader(contextClassLoaderBefore); // Take the thread local after snaphsot. Set<ThreadLocal> afterActionSnapshot = getCurrentThreadLocals(); // Do the combi-action of removing all added thread locals and store the added ones to the plugin's thread local // management. LimbusUtil.storeThreadLocalsInDeployContext(beforeActionSnapshot, afterActionSnapshot, this); } }