Skip to content

Commit

Permalink
cleanup temporary instance root on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed May 13, 2012
1 parent 8c66eed commit cb3a0c3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Expand Up @@ -43,6 +43,7 @@ public class GlassFishConfiguration implements ContainerConfiguration
private boolean configurationReadOnly = true;
private String configurationXml;
private String resourcesXml;
private boolean cleanup = true;

/* (non-Javadoc)
* @see org.jboss.arquillian.spi.client.container.ContainerConfiguration#validate()
Expand Down Expand Up @@ -108,6 +109,16 @@ public void setConfigurationXml(String configurationXml)
this.configurationXml = configurationXml;
}

public boolean getCleanup()
{
return cleanup;
}

public void setCleanup(boolean cleanup)
{
this.cleanup = cleanup;
}

public List<String> getResourcesXml()
{
if(resourcesXml == null){
Expand Down
Expand Up @@ -22,8 +22,9 @@
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.naming.NamingException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletRegistration;

import org.apache.catalina.Container;
Expand Down Expand Up @@ -52,8 +53,8 @@
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptor;

import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.web.WebModule;
import javax.naming.InitialContext;

/**
* GlassfishContainer
Expand Down Expand Up @@ -110,15 +111,16 @@ public void setup(GlassFishConfiguration configuration)
throw new RuntimeException("Could not setup GlassFish Embedded Bootstrap", e);
}

boolean cleanup = configuration.getCleanup();
GlassFishProperties serverProps = new GlassFishProperties();

boolean shouldSetPort = true;
if(configuration.getInstanceRoot() != null)
{
File instanceRoot = new File(configuration.getInstanceRoot());
if(!instanceRoot.exists())
if (instanceRoot.exists())
{
instanceRoot.mkdirs();
cleanup = false;
}
serverProps.setInstanceRoot(configuration.getInstanceRoot());
shouldSetPort = false;
Expand All @@ -133,6 +135,7 @@ public void setup(GlassFishConfiguration configuration)
{
serverProps.setPort("http-listener", configuration.getBindHttpPort());
}

try
{
glassfish = glassfishRuntime.newGlassFish(serverProps);
Expand All @@ -141,6 +144,18 @@ public void setup(GlassFishConfiguration configuration)
{
throw new RuntimeException("Could not setup GlassFish Embedded Runtime", e);
}

if (cleanup)
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
deleteRecursive(new File(System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY)));
}
});
}
}

/* (non-Javadoc)
Expand All @@ -158,7 +173,7 @@ public void start() throws LifecycleException
throw new LifecycleException("Could not start GlassFish Embedded", e);
}
// Server needs to be started before we can deploy resources
for(String resource : configuration.getSunResourcesXml())
for(String resource : configuration.getResourcesXml())
{
try
{
Expand Down Expand Up @@ -356,4 +371,24 @@ private void bindCommandRunner() throws NamingException, GlassFishException {
private void unbindCommandRunner() throws NamingException {
new InitialContext().unbind("org.glassfish.embeddable.CommandRunner");
}

private void deleteRecursive(File dir) {
if (dir == null) {
throw new IllegalArgumentException("directory cannot be null");
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException("directory must be a directory");
}
if (dir.exists()) {
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
deleteRecursive(file);
file.delete();
} else {
file.delete();
}
}
dir.delete();
}
}
}

0 comments on commit cb3a0c3

Please sign in to comment.