Skip to content

Commit

Permalink
Turned Runtime.createClassLoader() into Runtime.loadModule() beca…
Browse files Browse the repository at this point in the history
…use we need the info about the module that was actually loaded before getting its class loader (#21)
  • Loading branch information
quintesse committed May 9, 2013
1 parent 6292a56 commit 1ac8ba3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Expand Up @@ -46,7 +46,7 @@ public abstract class AbstractRuntime implements ceylon.modules.spi.runtime.Runt
* @return new module instance or null if no such descriptor
* @throws Exception for any error
*/
public static Module loadModule(ClassLoader cl, String moduleName) throws Exception {
public static Module loadModuleMetaData(ClassLoader cl, String moduleName) throws Exception {
final String moduleClassName = moduleName + MODULE_INFO_CLASS;
try {
Class<?> klass = cl.loadClass(moduleClassName);
Expand All @@ -57,11 +57,11 @@ public static Module loadModule(ClassLoader cl, String moduleName) throws Except

}

protected Module readModule(String name, File moduleFile) throws Exception {
protected Module readModuleMetaData(String name, File moduleFile) throws Exception {
final URL url = moduleFile.toURI().toURL();
final ClassLoader parent = getClass().getClassLoader();
final ClassLoader cl = new URLClassLoader(new URL[]{url}, parent);
return loadModule(cl, name);
return loadModuleMetaData(cl, name);
}

protected static void invokeRun(ClassLoader cl, String runClassName, final String[] args) throws Exception {
Expand Down Expand Up @@ -102,8 +102,9 @@ public void execute(Configuration conf) throws Exception {
String name = exe.substring(0, p > 0 ? p : exe.length());
String mv = (p > 0 ? exe.substring(p + 1) : null);

ClassLoader cl = createClassLoader(name, mv, conf);
Module runtimeModule = loadModule(cl, name);
org.jboss.modules.Module m = loadModule(name, mv, conf);
ClassLoader cl = SecurityActions.getClassLoader(m);
Module runtimeModule = loadModuleMetaData(cl, name);
if (runtimeModule != null) {
final String mn = runtimeModule.name();
if (name.equals(mn) == false)
Expand Down
22 changes: 22 additions & 0 deletions api/src/main/java/ceylon/modules/api/runtime/SecurityActions.java
Expand Up @@ -19,8 +19,11 @@

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;

import org.jboss.modules.Module;

/**
* Security actions.
*
Expand Down Expand Up @@ -62,6 +65,25 @@ private static void invokeRunInternal(final Class<?> runClass, final String[] ar
}
}

/**
* Get classloader from a module.
*
* @param module the current module
* @return module's classloader
*/
static ClassLoader getClassLoader(final Module module) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return module.getClassLoader();
}
});
} else {
return module.getClassLoader();
}
}

public static ClassLoader setContextClassLoader(final ClassLoader cl) throws Exception {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
Expand Down
Expand Up @@ -50,7 +50,7 @@
* @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
*/
public abstract class AbstractJBossRuntime extends AbstractRuntime {
public ClassLoader createClassLoader(String name, String version, Configuration conf) throws Exception {
public Module loadModule(String name, String version, Configuration conf) throws Exception {
if (RepositoryManager.DEFAULT_MODULE.equals(name)) {
if (version != null) {
throw new CeylonRuntimeException("Invalid module identifier: default module should not have any version");
Expand Down Expand Up @@ -93,8 +93,7 @@ public ClassLoader createClassLoader(String name, String version, Configuration
}
try {
ModuleLoader moduleLoader = createModuleLoader(conf);
Module module = moduleLoader.loadModule(moduleIdentifier);
return SecurityActions.getClassLoader(module);
return moduleLoader.loadModule(moduleIdentifier);
} catch (ModuleNotFoundException e) {
String spec = e.getMessage().replace(':', '/');
final CeylonRuntimeException cre = new CeylonRuntimeException("Could not find module: " + spec + " (invalid version?)");
Expand Down
4 changes: 3 additions & 1 deletion spi/src/main/java/ceylon/modules/spi/runtime/Runtime.java
Expand Up @@ -17,6 +17,8 @@

package ceylon.modules.spi.runtime;

import org.jboss.modules.Module;

import ceylon.modules.Configuration;
import ceylon.modules.spi.Executable;

Expand All @@ -35,5 +37,5 @@ public interface Runtime extends Executable {
* @return module classloader instance
* @throws Exception for ay error
*/
ClassLoader createClassLoader(String name, String version, Configuration conf) throws Exception;
Module loadModule(String name, String version, Configuration conf) throws Exception;
}

0 comments on commit 1ac8ba3

Please sign in to comment.