Created
August 23, 2012 01:07
-
-
Save martinlau/3430990 to your computer and use it in GitHub Desktop.
Classloader swapping in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ClassLoaderSwapper { | |
public void doWithClassLoader(ClassLoader classLoader) { | |
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); | |
Thread.currentThread().setContextClassLoader(classLoader); | |
try { | |
doInternal(); | |
} | |
finally { | |
Thread.currentThread().setContextClassLoader(oldClassLoader); | |
} | |
} | |
protected abstract void doInternal(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassLoaderSwapperClient { | |
public void doSomething() { | |
UsageClassLoaderSwapper swapper = new UsageClassLoaderSwapper(); | |
ClassLoader portalClassLoader = PortalClassLoaderUtil.getPortalClassLoader(); | |
swapper.doWithClassLoader(portalClassLoader); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsageClassLoaderSwapper extends ClassLoaderSwapper { | |
@Override | |
protected void doInternal() { | |
System.out.println("hi"); | |
System.out.println("bye"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment