/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2016, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.clustering.tomcat;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.function.Consumer;

/**
 * Performs some action with a given context class loader.
 * @author Paul Ferraro
 */
public class ContextClassLoaderAction implements Consumer<Runnable> {

    private final ClassLoader loader;

    public ContextClassLoaderAction(ClassLoader loader) {
        this.loader = loader;
    }

    @Override
    public void accept(Runnable task) {
        ClassLoader loader = getContextClassLoader();
        setContextClassLoader(this.loader);
        try {
            task.run();
        } finally {
            setContextClassLoader(loader);
        }
    }

    private static ClassLoader getContextClassLoader() {
        PrivilegedAction<ClassLoader> contextClassLoaderAction = () -> Thread.currentThread().getContextClassLoader();
        return AccessController.doPrivileged(contextClassLoaderAction);
    }

    private static void setContextClassLoader(ClassLoader loader) {
        PrivilegedAction<Void> action = () -> {
            Thread.currentThread().setContextClassLoader(loader);
            return null;
        };
        AccessController.doPrivileged(action);
    }
}

@@ -1057,16 +1057,52 @@
 omcat;%0A%0A
+import java.lang.ref.WeakReference;%0A
 import j
@@ -1386,14 +1386,140 @@
 ate 
-final 
+interface ClassLoaderContext extends AutoCloseable %7B%0A        @Override%0A        void close();%0A    %7D%0A%0A    private final WeakReference%3C
 Clas
@@ -1521,24 +1521,25 @@
 %3CClassLoader
+%3E
  loader;%0A%0A  
@@ -1616,22 +1616,43 @@
 oader = 
+new WeakReference%3C%3E(
 loader
+)
 ;%0A    %7D%0A
@@ -1714,16 +1714,21 @@
         
+try (
 ClassLoa
@@ -1734,98 +1734,67 @@
 ader
- loader = getContextClassLoader();%0A        setContextClassLoader(this.loader);%0A        try
+Context context = getClassLoaderContext(this.loader.get()))
  %7B%0A 
@@ -1829,41 +1829,76 @@
    %7D
- finally %7B
 %0A    
+%7D%0A%0A
     
-    set
+private static ClassLoaderContext getClassLoader
 Context
+(
 Clas
@@ -1908,91 +1908,63 @@
 ader
-(
+ 
 loader)
-;%0A        %7D
+ %7B
 %0A    
-%7D%0A%0A
     
-private static ClassLoader getContextClassLoader() %7B
+if (loader == null) return () -%3E %7B%7D;
 %0A   
@@ -1998,35 +1998,17 @@
 Loader%3E 
-contextClassLoaderA
+a
 ction = 
@@ -2069,22 +2069,44 @@
         
-return
+ClassLoader existingLoader =
  AccessC
@@ -2132,32 +2132,113 @@
 ged(
-contextClassLoaderAction
+action);%0A        setContextClassLoader(loader);%0A        return () -%3E setContextClassLoader(existingLoader
 );%0A 

Use WeakReference for ClassLoader.