File modulesDir = new File(jbossHomeDir + "/modules");
final ModuleLoader moduleLoader = InitialModuleLoaderFactory.getModuleLoader(modulesDir, systemPackages);
// Initialize the Logging system
ModuleIdentifier logModuleId = ModuleIdentifier.create("org.jboss.logmanager");
ModuleClassLoader logModuleClassLoader = moduleLoader.loadModule(logModuleId).getClassLoader();
ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(logModuleClassLoader);
systemProps.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
if (LogManager.getLogManager().getClass() == LogManager.class) {
System.err.println("WARNING: Failed to load the specified logmodule " + logModuleId);
} else {
Module.setModuleLogger(new JDKModuleLogger());
}
} finally {
Thread.currentThread().setContextClassLoader(ctxClassLoader);
}
// Load the server Module and get its ClassLoader
final ModuleIdentifier serverModuleId = ModuleIdentifier.create("org.jboss.as.server");
final Module serverModule = moduleLoader.loadModule(serverModuleId);
final ModuleClassLoader serverModuleClassLoader = serverModule.getClassLoader();
StandaloneServer standaloneServer = new StandaloneServer() {
private Object serviceContainer;
@Override
public void start() throws ServerStartException {
try {
// Determine the ServerEnvironment
Class<?> serverMainClass = serverModuleClassLoader.loadClass(Main.class.getName());
Method determineEnvironmentMethod = serverMainClass.getMethod("determineEnvironment", String[].class, Properties.class, Map.class);
Object serverEnvironment = determineEnvironmentMethod.invoke(null, new String[0], systemProps, systemEnv);
Class<?> bootstrapFactoryClass = serverModuleClassLoader.loadClass(Bootstrap.Factory.class.getName());
Method newInstanceMethod = bootstrapFactoryClass.getMethod("newInstance");
Object bootstrap = newInstanceMethod.invoke(null);
Class<?> configurationClass = serverModuleClassLoader.loadClass(Bootstrap.Configuration.class.getName());
Constructor<?> configurationCtor = configurationClass.getConstructor();
Object configuration = configurationCtor.newInstance();
Method setServerEnvironmentMethod = configurationClass.getMethod("setServerEnvironment", serverEnvironment.getClass());
setServerEnvironmentMethod.invoke(configuration, serverEnvironment);
Method setModuleLoaderMethod = configurationClass.getMethod("setModuleLoader", ModuleLoader.class);
setModuleLoaderMethod.invoke(configuration, moduleLoader);
Class<?> bootstrapClass = serverModuleClassLoader.loadClass(Bootstrap.class.getName());
Method bootstrapStartMethod = bootstrapClass.getMethod("startup", configurationClass, List.class);
Object future = bootstrapStartMethod.invoke(bootstrap, configuration, Collections.<ServiceActivator>emptyList());
Class<?> asyncFutureClass = serverModuleClassLoader.loadClass(AsyncFuture.class.getName());
Method getMethod = asyncFutureClass.getMethod("get");
serviceContainer = getMethod.invoke(future);
} catch (RuntimeException rte) {
throw rte;
} catch (Exception ex) {
throw new ServerStartException(ex);
}
}
@Override
public void stop() {
if (serviceContainer != null) {
try {
Class<?> serverContainerClass = serverModuleClassLoader.loadClass(ServiceContainer.class.getName());
Method shutdownMethod = serverContainerClass.getMethod("shutdown");
shutdownMethod.invoke(serviceContainer);
Method awaitTerminationMethod = serverContainerClass.getMethod("awaitTermination");
awaitTerminationMethod.invoke(serviceContainer);