@BeforeClass public static void startServer() throws Exception { EmbeddedServerFactory.setupCleanDirectories(System.getProperties()); ServerEnvironment serverEnvironment = Main.determineEnvironment( new String[0], new Properties(System.getProperties()), System.getenv()); Assert.assertNotNull(serverEnvironment); final Bootstrap bootstrap = Bootstrap.Factory.newInstance(); final Bootstrap.Configuration configuration = new Bootstrap.Configuration(); configuration.setServerEnvironment(serverEnvironment); configuration.setModuleLoader(Module.getBootModuleLoader()); configuration.setPortOffset(0); container = bootstrap.startup(configuration, Collections.<ServiceActivator>emptyList()).get(); Assert.assertNotNull(container); }
public static StandaloneServer create( final File jbossHomeDir, final Properties systemProps, final Map<String, String> systemEnv, String... systemPackages) throws Throwable { if (jbossHomeDir == null || jbossHomeDir.isDirectory() == false) throw new IllegalStateException("Invalid jboss.home.dir: " + jbossHomeDir); if (systemProps.getProperty(ServerEnvironment.HOME_DIR) == null) systemProps.setProperty(ServerEnvironment.HOME_DIR, jbossHomeDir.getAbsolutePath()); setupCleanDirectories(jbossHomeDir, systemProps); 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("start", 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); } catch (RuntimeException rte) { throw rte; } catch (Exception ex) { ex.printStackTrace(); } } } }; return standaloneServer; }