@SuppressWarnings({ "rawtypes" }) static void storeThreadLocalsInDeployContext(Set<ThreadLocal> beforeActionSnapshot, Set<ThreadLocal> afterActionSnapshot, LimbusContextInternal limbusContext) { Set<ThreadLocal> threadLocalsDelta = removeAddedThreadLocales(beforeActionSnapshot, afterActionSnapshot); // Store the ThreadLocals added from the context action in limbus context limbusContext.setThreadLocalsSet(threadLocalsDelta); // Restore the ThreadLocals to the state before a context action was executed LimbusUtil.addThreadLocals(beforeActionSnapshot); }
@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); } }