Skip to content

Commit

Permalink
ARQ-1855 - Tomcat 6 Embedded container should support deployment of R…
Browse files Browse the repository at this point in the history
…OOT.war to the default context.
  • Loading branch information
ianbrandt committed Sep 17, 2014
1 parent 598f46c commit 191e504
Showing 1 changed file with 75 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData;
import org.jboss.arquillian.container.spi.client.protocol.metadata.Servlet;
import org.jboss.arquillian.container.spi.context.annotation.DeploymentScoped;
import org.jboss.arquillian.container.tomcat.embedded.TomcatEmbeddedConfiguration;
import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.shrinkwrap.api.Archive;
Expand All @@ -66,8 +65,8 @@
* @author Dan Allen
* @version $Revision: $
*/
public class Tomcat6EmbeddedContainer implements DeployableContainer<TomcatEmbeddedConfiguration>
{
public class Tomcat6EmbeddedContainer implements DeployableContainer<TomcatEmbeddedConfiguration> {

private static final Logger log = Logger.getLogger(Tomcat6EmbeddedContainer.class.getName());

private static final String ENV_VAR = "${env.";
Expand Down Expand Up @@ -97,79 +96,74 @@ public class Tomcat6EmbeddedContainer implements DeployableContainer<TomcatEmbed
private InstanceProducer<StandardContext> standardContextProducer;

@Override
public Class<TomcatEmbeddedConfiguration> getConfigurationClass()
{
public Class<TomcatEmbeddedConfiguration> getConfigurationClass() {

return TomcatEmbeddedConfiguration.class;
}

@Override
public ProtocolDescription getDefaultProtocol()
{
public ProtocolDescription getDefaultProtocol() {

return new ProtocolDescription("Servlet 2.5");
}

@Override
public void setup(final TomcatEmbeddedConfiguration configuration)
{
public void setup(final TomcatEmbeddedConfiguration configuration) {

final String serverName = configuration.getServerName();

if (serverName == null || "".equals(serverName))
{
if (serverName == null || "".equals(serverName)) {
configuration.setServerName("arquillian-tomcat-embedded-6");
}

this.configuration = configuration;
}

@Override
public void start() throws LifecycleException
{
try
{
public void start() throws LifecycleException {

try {
startTomcatEmbedded();
} catch (final Exception e)
{
} catch (final Exception e) {
throw new LifecycleException("Failed to start embedded Tomcat", e);
}
}

@Override
public void stop() throws LifecycleException
{
try
{
public void stop() throws LifecycleException {

try {
removeFailedUnDeployments();
} catch (final Exception e)
{
} catch (final Exception e) {
throw new LifecycleException("Could not clean up", e);
}
if (wasStarted)
{
try
{
if (wasStarted) {
try {
stopTomcatEmbedded();
} catch (final org.apache.catalina.LifecycleException e)
{
} catch (final org.apache.catalina.LifecycleException e) {
throw new LifecycleException("Failed to stop Tomcat", e);
}
}
}

@Override
public ProtocolMetaData deploy(final Archive<?> archive) throws DeploymentException
{
try
{
public ProtocolMetaData deploy(final Archive<?> archive) throws DeploymentException {

try {
final StandardContext standardContext = archive.as(ShrinkWrapStandardContext.class);

if (archive.getName().startsWith("ROOT")) {
standardContext.setPath("");
}

standardContext.addLifecycleListener(new EmbeddedContextConfig());
standardContext.setUnpackWAR(configuration.isUnpackArchive());
standardContext.setJ2EEServer("Arquillian-" + UUID.randomUUID().toString());

// Need to tell TomCat to use TCCL as parent, else the WebContextClassloader will be looking in AppCL
standardContext.setParentClassLoader(Thread.currentThread().getContextClassLoader());

if (standardContext.getUnpackWAR())
{
if (standardContext.getUnpackWAR()) {
deleteUnpackedWAR(standardContext);
}

Expand All @@ -185,83 +179,70 @@ public ProtocolMetaData deploy(final Archive<?> archive) throws DeploymentExcept
standardContextProducer.set(standardContext);

final String contextPath = standardContext.getPath();
final HTTPContext httpContext = new HTTPContext(configuration.getBindAddress(),
configuration.getBindHttpPort());
final HTTPContext httpContext = new HTTPContext(configuration.getBindAddress(), configuration.getBindHttpPort());

for (final String mapping : standardContext.findServletMappings())
{
for (final String mapping : standardContext.findServletMappings()) {
httpContext.add(new Servlet(standardContext.findServletMapping(mapping), contextPath));
}

return new ProtocolMetaData().addContext(httpContext);
} catch (final Exception e)
{
} catch (final Exception e) {
throw new DeploymentException("Failed to deploy " + archive.getName(), e);
}
}

@Override
public void undeploy(final Archive<?> archive) throws DeploymentException
{
public void undeploy(final Archive<?> archive) throws DeploymentException {

final StandardContext standardContext = standardContextProducer.get();
if (standardContext != null)
{
if (standardContext != null) {
host.removeChild(standardContext);
try
{
try {
standardContext.stop();
standardContext.destroy();
} catch (final Exception e)
{
} catch (final Exception e) {
log.log(Level.WARNING, "Error on undeployment of " + standardContext.getName(), e);
}
if (standardContext.getUnpackWAR())
{
if (standardContext.getUnpackWAR()) {
deleteUnpackedWAR(standardContext);
}
}
}

@Override
public void deploy(final Descriptor descriptor) throws DeploymentException
{
public void deploy(final Descriptor descriptor) throws DeploymentException {

throw new UnsupportedOperationException("Not implemented");
}

@Override
public void undeploy(final Descriptor descriptor) throws DeploymentException
{
public void undeploy(final Descriptor descriptor) throws DeploymentException {

throw new UnsupportedOperationException("Not implemented");
}

protected void startTomcatEmbedded() throws UnknownHostException, org.apache.catalina.LifecycleException
{
protected void startTomcatEmbedded() throws UnknownHostException, org.apache.catalina.LifecycleException {

// creating the tomcat embedded == service tag in server.xml
tomcat = new Embedded();
tomcat.setName(configuration.getServerName());
// TODO this needs to be a lot more robust
String tomcatHome = configuration.getTomcatHome();
File tomcatHomeFile = null;
if (tomcatHome != null)
{
if (tomcatHome.startsWith(ENV_VAR))
{
if (tomcatHome != null) {
if (tomcatHome.startsWith(ENV_VAR)) {
final String sysVar = tomcatHome.substring(ENV_VAR.length(), tomcatHome.length() - 1);
tomcatHome = System.getProperty(sysVar);
if (tomcatHome != null && tomcatHome.length() > 0 && new File(tomcatHome).isAbsolute())
{
if (tomcatHome != null && tomcatHome.length() > 0 && new File(tomcatHome).isAbsolute()) {
tomcatHomeFile = new File(tomcatHome);
log.info("Using tomcat home from environment variable: " + tomcatHome);
}
}
else
{
} else {
tomcatHomeFile = new File(tomcatHome);
}
}

if (tomcatHomeFile == null)
{
if (tomcatHomeFile == null) {
tomcatHomeFile = new File(System.getProperty(TMPDIR_SYS_PROP), "tomcat-embedded-6");
}

Expand All @@ -281,16 +262,16 @@ protected void startTomcatEmbedded() throws UnknownHostException, org.apache.cat
final File appBaseFile = new File(tomcatHomeFile, configuration.getAppBase());
appBaseFile.mkdirs();
host = tomcat.createHost(configuration.getBindAddress(), appBaseFile.getAbsolutePath());
if (configuration.getTomcatWorkDir() != null)
{
if (configuration.getTomcatWorkDir() != null) {
((StandardHost) host).setWorkDir(configuration.getTomcatWorkDir());
}
((StandardHost) host).setUnpackWARs(configuration.isUnpackArchive());
engine.addChild(host);

// creates an http connector, i.e., <connector> element in server.xml
final Connector connector = tomcat.createConnector(InetAddress.getByName(configuration.getBindAddress()),
configuration.getBindHttpPort(), false);
final Connector connector =
tomcat.createConnector(InetAddress.getByName(configuration.getBindAddress()), configuration.getBindHttpPort(),
false);
tomcat.addConnector(connector);
connector.setContainer(engine);

Expand All @@ -300,50 +281,51 @@ protected void startTomcatEmbedded() throws UnknownHostException, org.apache.cat
wasStarted = true;
}

protected void stopTomcatEmbedded() throws org.apache.catalina.LifecycleException
{
protected void stopTomcatEmbedded() throws org.apache.catalina.LifecycleException {

tomcat.stop();
tomcat.destroy();
}

/**
* Make sure an the unpacked WAR is not left behind you would think Tomcat would cleanup an unpacked WAR, but it doesn't
*/
protected void deleteUnpackedWAR(final StandardContext standardContext)
{
final File unpackDir = new File(host.getAppBase(), standardContext.getPath().substring(1));
if (unpackDir.exists())
{
protected void deleteUnpackedWAR(final StandardContext standardContext) {

String path = standardContext.getPath();

if ("".equals(path)) {
path = "/ROOT";
}

final File unpackDir = new File(host.getAppBase(), path.substring(1));

if (unpackDir.exists()) {
ExpandWar.deleteDir(unpackDir);
}
}

private void undeploy(final String name) throws DeploymentException
{
private void undeploy(final String name) throws DeploymentException {

final Container child = host.findChild(name);
if (child != null)
{
if (child != null) {
host.removeChild(child);
}
}

private void removeFailedUnDeployments() throws IOException
{
private void removeFailedUnDeployments() throws IOException {

final List<String> remainingDeployments = new ArrayList<String>();
for (final String name : failedUndeployments)
{
try
{
for (final String name : failedUndeployments) {
try {
undeploy(name);
} catch (final Exception e)
{
} catch (final Exception e) {
final IOException ioe = new IOException();
ioe.initCause(e);
throw ioe;
}
}
if (remainingDeployments.size() > 0)
{
if (remainingDeployments.size() > 0) {
log.severe("Failed to undeploy these artifacts: " + remainingDeployments);
}
failedUndeployments.clear();
Expand Down

0 comments on commit 191e504

Please sign in to comment.