* @return
* @throws BootException
*/
public ModuleStartup findStartupService(ModulesRegistry registry, Habitat habitat, String mainModuleName, StartupContext context) throws BootException {
ModuleStartup startupCode=null;
final Module mainModule;
if(mainModuleName!=null) {
// instantiate the main module, this is the entry point of the application
// code. it is supposed to have 1 ModuleStartup implementation.
Collection<Module> modules = registry.getModules(mainModuleName);
if (modules.size() != 1) {
if(registry.getModules().isEmpty())
throw new BootException("Registry has no module at all");
else
throw new BootException("Cannot find main module " + mainModuleName+" : no such module");
}
mainModule = modules.iterator().next();
String targetClassName = findModuleStartupClassName(mainModule,context.getPlatformMainServiceName(), HABITAT_NAME);
if (targetClassName==null) {
throw new BootException("Cannot find a ModuleStartup implementation in the META-INF/services/com.sun.enterprise.v3.ModuleStartup file, aborting");
}
Class<? extends ModuleStartup> targetClass=null;
final ClassLoader currentCL =
AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(mainModule.getClassLoader());
return cl;
}
});
try {
ClassLoader moduleClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return mainModule.getClassLoader();
}
});
targetClass = moduleClassLoader.loadClass(targetClassName).asSubclass(ModuleStartup.class);
startupCode = habitat.getComponent(targetClass);
} catch (ClassNotFoundException e) {
throw new BootException("Unable to load component of type " + targetClassName,e);
} catch (ComponentException e) {
throw new BootException("Unable to load component of type " + targetClassName,e);
} finally {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
Thread.currentThread().setContextClassLoader(currentCL);
return null;
}
});
}
} else {
Collection<Inhabitant<? extends ModuleStartup>> startups = habitat.getInhabitants(ModuleStartup.class);
if(startups.isEmpty())
throw new BootException("No module has a ModuleStartup implementation");
if(startups.size()>1) {
// maybe the user specified a main
String mainServiceName = context.getPlatformMainServiceName();
for (Inhabitant<? extends ModuleStartup> startup : startups) {
Collection<String> regNames = Inhabitants.getNamesFor(startup, ModuleStartup.class.getName());
if (regNames.isEmpty() && mainServiceName==null) {
startupCode = startup.get();
} else {
for (String regName : regNames) {
if (regName.equals(mainServiceName)) {
startupCode = startup.get();
}
}
}
}
if (startupCode==null) {
if (mainServiceName==null) {
Iterator<Inhabitant<? extends ModuleStartup>> itr = startups.iterator();
ModuleStartup a = itr.next().get();
ModuleStartup b = itr.next().get();
Module am = registry.find(a.getClass());
Module bm = registry.find(b.getClass());
throw new BootException(String.format("Multiple ModuleStartup found: %s from %s and %s from %s",a,am,b,bm));
} else {
throw new BootException(String.format("Cannot find %s ModuleStartup", mainServiceName));
}
}