Skip to content

Commit

Permalink
[AS7-6172] Consolidate embedded server API
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Diesler authored and n1hility committed Jan 3, 2013
1 parent 052eb54 commit 2bd88a3
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 651 deletions.
12 changes: 10 additions & 2 deletions arquillian/container-embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->

<!--
Set these VM properties in your IDE debugger
-Djava.util.logging.manager=org.jboss.logmanager.LogManager
-Djboss.home=${workspace_loc:jboss-as-build}/target/jboss-as-7.2.0.Alpha1-SNAPSHOT
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -105,11 +111,13 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Fork every test because it will launch a separate AS instance -->
<forkMode>always</forkMode>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<jboss.home>${basedir}/target/jbossas</jboss.home>
<jboss.home>${jboss.home}</jboss.home>
<module.path>${jboss.home}/modules</module.path>
<bundle.path>${jboss.home}/modules</bundle.path>
<bundle.path>${jboss.home}/bundles</bundle.path>
</systemPropertyVariables>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@
*/
package org.jboss.as.arquillian.container.embedded;

import java.io.File;
import java.util.logging.LogManager;

import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.container.spi.client.container.LifecycleException;
import org.jboss.as.arquillian.container.CommonDeployableContainer;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleClassLoader;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.modules.ModuleLoadException;
import org.jboss.modules.ModuleLoader;
import org.jboss.modules.log.JDKModuleLogger;
import org.jboss.as.embedded.EmbeddedServerFactory;
import org.jboss.as.embedded.StandaloneServer;

/**
* {@link DeployableContainer} implementation to bootstrap JBoss Logging (installing the LogManager if possible), use the JBoss
Expand All @@ -36,191 +29,41 @@
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
* @author <a href="mailto:mmatloka@gmail.com">Michal Matloka</a>
* @author Thomas.Diesler@jboss.com
*/
public final class EmbeddedDeployableContainer extends CommonDeployableContainer<EmbeddedContainerConfiguration> {

private static final String MODULE_ID_LOGMANAGER = "org.jboss.logmanager";
private static final String MODULE_ID_VFS = "org.jboss.vfs";
private static final String SYSPROP_KEY_CLASS_PATH = "java.class.path";
private static final String SYSPROP_KEY_MODULE_PATH = "module.path";
private static final String SYSPROP_KEY_BUNDLE_PATH = "jboss.bundles.dir";
private static final String SYSPROP_KEY_LOGMANAGER = "java.util.logging.manager";
private static final String SYSPROP_KEY_JBOSS_HOME_DIR = "jboss.home.dir";
private static final String SYSPROP_VALUE_JBOSS_LOGMANAGER = "org.jboss.logmanager.LogManager";

/**
* Hook to the server; used in start/stop, created by setup
*/
private StandaloneServerIndirection server;
private StandaloneServer server;

/**
* {@inheritDoc}
*
* @see org.jboss.as.arquillian.container.CommonDeployableContainer#setup(org.jboss.as.arquillian.container.CommonContainerConfiguration)
*/
@Override
public void setup(final EmbeddedContainerConfiguration config) {
super.setup(config);

final ModuleLoader moduleLoader = setupModuleLoader(config.getModulePath());

setupBundlePath(config.getBundlePath());

setupVfsModule(moduleLoader);

setupLoggingSystem(moduleLoader);

setupServer(moduleLoader, config.getJbossHome());
}

/**
* Creates/obtains the boot {@link ModuleLoader} for the specified, required modules path
*
* @param modulePath
* @return module loader
*/
private ModuleLoader setupModuleLoader(final String modulePath) {
if (modulePath == null || modulePath.isEmpty()) {
throw new IllegalStateException("Module path must be defined in the configuration");
}

final File modulesDir = new File(modulePath);
if (!modulesDir.isDirectory()) {
throw new IllegalStateException("Invalid modules directory: " + modulesDir);
}

final String classPath = SecurityActions.getSystemProperty(SYSPROP_KEY_CLASS_PATH);
try {
// Set up sysprop env
SecurityActions.clearSystemProperty(SYSPROP_KEY_CLASS_PATH);
SecurityActions.setSystemProperty(SYSPROP_KEY_MODULE_PATH, modulesDir.getAbsolutePath());

// Get the module loader
final ModuleLoader moduleLoader = Module.getBootModuleLoader();
return moduleLoader;
} finally {
// Return to previous state for classpath prop
SecurityActions.setSystemProperty(SYSPROP_KEY_CLASS_PATH, classPath);
}
}

/**
* Set bundle path
*
* @param bundlePath
*/
private void setupBundlePath(final String bundlePath) {
if (bundlePath == null || bundlePath.isEmpty()) {
throw new IllegalStateException("Bundle path must be defined in the configuration");
}

final File bundlesDir = new File(bundlePath);
if (!bundlesDir.isDirectory()) {
throw new IllegalStateException("Invalid bundles directory: " + bundlesDir);
}
SecurityActions.setSystemProperty(SYSPROP_KEY_BUNDLE_PATH, bundlePath);
server = EmbeddedServerFactory.create(config.getJbossHome(), config.getModulePath(), config.getBundlePath());
}

/**
* Register URL Stream Handlers for the VFS Module
*
* @param moduleLoader
*/
private void setupVfsModule(final ModuleLoader moduleLoader) {
final ModuleIdentifier vfsModuleID = ModuleIdentifier.create(MODULE_ID_VFS);
final Module vfsModule;

try {
vfsModule = moduleLoader.loadModule(vfsModuleID);
} catch (final ModuleLoadException mle) {
throw new RuntimeException("Could not load VFS module", mle);
}

Module.registerURLStreamHandlerFactoryModule(vfsModule);
}

/**
* Initialize the Logging system
*
* @param moduleLoader
*/
private void setupLoggingSystem(ModuleLoader moduleLoader) {
final ModuleIdentifier logModuleId = ModuleIdentifier.create(MODULE_ID_LOGMANAGER);
final Module logModule;
try {
logModule = moduleLoader.loadModule(logModuleId);
} catch (final ModuleLoadException mle) {
throw new RuntimeException("Could not load logging module", mle);
}

final ModuleClassLoader logModuleClassLoader = logModule.getClassLoader();
final ClassLoader tccl = SecurityActions.getContextClassLoader();
try {
SecurityActions.setContextClassLoader(logModuleClassLoader);
SecurityActions.setSystemProperty(SYSPROP_KEY_LOGMANAGER, SYSPROP_VALUE_JBOSS_LOGMANAGER);

final Class<?> actualLogManagerClass = LogManager.getLogManager().getClass();
if (actualLogManagerClass == LogManager.class) {
System.err
.println("Could not load JBoss LogManager; the LogManager or Logging subsystem has likely been accessed prior to this initialization.");
} else {
Module.setModuleLogger(new JDKModuleLogger());
}
} finally {
// Reset TCCL
SecurityActions.setContextClassLoader(tccl);
}
}

/**
* Create and set the server
*
* @param moduleLoader
* @param jbossHomePath
*/
private void setupServer(final ModuleLoader moduleLoader, final String jbossHomePath) {
if (jbossHomePath == null || jbossHomePath.isEmpty()) {
throw new IllegalStateException("JBoss home path must be defined");
}

final File jbossHome = new File(jbossHomePath);
if (!jbossHome.isDirectory()) {
throw new IllegalStateException("Invalid jboss home directory: " + jbossHome);
}

// Embedded Server wants this, too. Seems redundant, but supply it.
SecurityActions.setSystemProperty(SYSPROP_KEY_JBOSS_HOME_DIR, jbossHome.getAbsolutePath());
this.server = new StandaloneServerIndirection(moduleLoader, jbossHome);
}

/**
* {@inheritDoc}
*
* @see org.jboss.arquillian.container.spi.client.container.DeployableContainer#getConfigurationClass()
*/
@Override
public Class<EmbeddedContainerConfiguration> getConfigurationClass() {
return EmbeddedContainerConfiguration.class;
}

/**
* {@inheritDoc}
*
* @see org.jboss.as.arquillian.container.CommonDeployableContainer#startInternal()
*/
@Override
protected void startInternal() throws LifecycleException {
// The comment here in AS4/5 used to be "Go get a cup of coffee", but this is FAST now. ;)
server.start();
try {
server.start();
} catch (Throwable e) {
throw new LifecycleException("Could not invoke start on: " + server, e);
}
}

/**
* {@inheritDoc}
*
* @see org.jboss.as.arquillian.container.CommonDeployableContainer#stopInternal()
*/
@Override
protected void stopInternal() throws LifecycleException {
this.server.stop();
try {
server.stop();
} catch (Throwable e) {
throw new LifecycleException("Could not invoke stop on: " + server, e);
}
}
}
Loading

0 comments on commit 2bd88a3

Please sign in to comment.