Permalink
Browse files

Fix for #31: set the context class loader to the ceylon module class …

…loader
  • Loading branch information...
1 parent 0f074e5 commit 6cbae6b0ff58e3801ef91b4a7b9b4f4167d64002 @FroMage FroMage committed Mar 7, 2013
@@ -66,20 +66,25 @@ protected Module readModule(String name, File moduleFile) throws Exception {
protected static void invokeRun(ClassLoader cl, String runClassName, final String[] args) throws Exception {
final Class<?> runClass;
- try {
- char firstChar = runClassName.charAt(0);
- int lastDot = runClassName.lastIndexOf('.');
- if (lastDot > 0) {
- firstChar = runClassName.charAt(lastDot + 1);
+ ClassLoader oldClassLoader = SecurityActions.setContextClassLoader(cl);
+ try{
+ try {
+ char firstChar = runClassName.charAt(0);
+ int lastDot = runClassName.lastIndexOf('.');
+ if (lastDot > 0) {
+ firstChar = runClassName.charAt(lastDot + 1);
+ }
+ // we add _ to run class
+ runClass = cl.loadClass(Character.isLowerCase(firstChar) ? runClassName + "_" : runClassName);
+ } catch (ClassNotFoundException ignored) {
+ Logger.getLogger("ceylon.runtime").severe("Could not find class or method '" + runClassName + "'");
+ return; // looks like no such run class is available
}
- // we add _ to run class
- runClass = cl.loadClass(Character.isLowerCase(firstChar) ? runClassName + "_" : runClassName);
- } catch (ClassNotFoundException ignored) {
- Logger.getLogger("ceylon.runtime").severe("Could not find class or method '" + runClassName + "'");
- return; // looks like no such run class is available
- }
- SecurityActions.invokeRun(runClass, args);
+ SecurityActions.invokeRun(runClass, args);
+ }finally{
+ SecurityActions.setContextClassLoader(oldClassLoader);
+ }
}
public void execute(Configuration conf) throws Exception {
@@ -61,4 +61,21 @@ private static void invokeRunInternal(final Class<?> runClass, final String[] ar
throw new NoSuchMethodException(runClass.getName() + "(Void)");
}
}
+
+ public static ClassLoader setContextClassLoader(final ClassLoader cl) throws Exception {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ return (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ public Object run() throws Exception {
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(cl);
+ return oldClassLoader;
+ }
+ });
+ } else {
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(cl);
+ return oldClassLoader;
+ }
+ }
}

0 comments on commit 6cbae6b

Please sign in to comment.