Created
March 2, 2012 18:55
-
-
Save blangel/1960376 to your computer and use it in GitHub Desktop.
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 DepX { | |
// stuff which depends upon Lib-version-1.x | |
} | |
public class DepY { | |
// stuff which depends upon Lib-version-1.y | |
} | |
public class UseBothDepXAndDepY { | |
DepX depX = ClApi.get(DepX.class); | |
DepY depY = ClApi.get(DepY.class); | |
// can now call each together even though they use different versions of same Lib | |
} | |
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 final class ClApi { | |
public static <T> T get(Class<T> clazz) { | |
// make a java.lang.reflect.Proxy of clazz where the InvocationHandler impl is below | |
} | |
private static final class ClInvocationHandler implements InvocationHandler { | |
private final ClApi clApi; | |
private ClInvocationHandler(ClApi clApi) { | |
this.clApi = clApi; | |
} | |
public Object invoke(Object proxy, Method method, Object[] args) | |
throws Throwable { | |
ClassLoader existing = Thread.currentThread().getContextClassLoader(); | |
try { | |
Thread.currentThread().setContextClassLoader(this.clApi.getClassLoader(proxy, method, args)); | |
method.invoke(proxy, args); | |
} finally { | |
Thread.currentThread().setContextClassLoader(existing); | |
} | |
} | |
} | |
private final ImmutableMap<Class<?>, ClassLoader> config; | |
private ClApi() { } | |
// would be init-ed with some yaml-style mapping of which Class depends upon which Lib-version-xx | |
// then for each computed set of ClassLoaders we'd make a mapping of Class to instantiated ClassLoader | |
// take in everything from invocation even though we may only need the object's type (may not always be the case). | |
private ClassLoader getClassLoader(Object proxy, Method method, Object[] args) { | |
return config.get(proxy.getClass()); // TODO - ensure not-Null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment