ARTEMIS-2596 Move tmp web dir for a safe clean up
This commit is contained in:
parent
d7f5e92e77
commit
819f1834f5
|
@ -19,6 +19,7 @@ package org.apache.activemq.artemis.component;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -48,7 +49,6 @@ import org.eclipse.jetty.server.handler.ResourceHandler;
|
||||||
import org.eclipse.jetty.servlet.FilterHolder;
|
import org.eclipse.jetty.servlet.FilterHolder;
|
||||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
import org.eclipse.jetty.webapp.WebAppContext;
|
import org.eclipse.jetty.webapp.WebAppContext;
|
||||||
import org.eclipse.jetty.webapp.WebInfConfiguration;
|
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
import javax.servlet.DispatcherType;
|
import javax.servlet.DispatcherType;
|
||||||
|
@ -65,6 +65,7 @@ public class WebServerComponent implements ExternalComponent {
|
||||||
private List<WebAppContext> webContexts;
|
private List<WebAppContext> webContexts;
|
||||||
private ServerConnector connector;
|
private ServerConnector connector;
|
||||||
private Path artemisHomePath;
|
private Path artemisHomePath;
|
||||||
|
private Path temporaryWarDir;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception {
|
public void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception {
|
||||||
|
@ -133,6 +134,11 @@ public class WebServerComponent implements ExternalComponent {
|
||||||
Path homeWarDir = artemisHomePath.resolve(webServerConfig.path).toAbsolutePath();
|
Path homeWarDir = artemisHomePath.resolve(webServerConfig.path).toAbsolutePath();
|
||||||
Path instanceWarDir = Paths.get(artemisInstance != null ? artemisInstance : ".").resolve(webServerConfig.path).toAbsolutePath();
|
Path instanceWarDir = Paths.get(artemisInstance != null ? artemisInstance : ".").resolve(webServerConfig.path).toAbsolutePath();
|
||||||
|
|
||||||
|
temporaryWarDir = Paths.get(artemisInstance != null ? artemisInstance : ".").resolve("tmp").resolve("webapps").toAbsolutePath();
|
||||||
|
if (!Files.exists(temporaryWarDir)) {
|
||||||
|
Files.createDirectories(temporaryWarDir);
|
||||||
|
}
|
||||||
|
|
||||||
if (webServerConfig.apps != null && webServerConfig.apps.size() > 0) {
|
if (webServerConfig.apps != null && webServerConfig.apps.size() > 0) {
|
||||||
webContexts = new ArrayList<>();
|
webContexts = new ArrayList<>();
|
||||||
for (AppDTO app : webServerConfig.apps) {
|
for (AppDTO app : webServerConfig.apps) {
|
||||||
|
@ -283,27 +289,16 @@ public class WebServerComponent implements ExternalComponent {
|
||||||
//there is no webapp to be deployed (as in some tests)
|
//there is no webapp to be deployed (as in some tests)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
List<File> temporaryFiles = new ArrayList<>();
|
|
||||||
|
|
||||||
for (WebAppContext context : webContexts) {
|
try {
|
||||||
WebInfConfiguration config = new WebInfConfiguration();
|
List<File> temporaryFiles = new ArrayList<>();
|
||||||
try {
|
Files.newDirectoryStream(temporaryWarDir).forEach(path -> temporaryFiles.add(path.toFile()));
|
||||||
config.resolveTempDirectory(context);
|
|
||||||
File webTmpBase = context.getTempDirectory().getParentFile();
|
if (temporaryFiles.size() > 0) {
|
||||||
if (webTmpBase.exists()) {
|
WebTmpCleaner.cleanupTmpFiles(getLibFolder(), temporaryFiles, true);
|
||||||
webTmpBase.listFiles((f) -> {
|
|
||||||
temporaryFiles.add(f);
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (temporaryFiles.size() > 0) {
|
|
||||||
WebTmpCleaner.cleanupTmpFiles(getLibFolder(), temporaryFiles, true);
|
|
||||||
}
|
|
||||||
//all web contexts share a same base dir. So we only do it once.
|
|
||||||
break;
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.warn("Failed to get base dir for tmp web files", e);
|
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("Failed to get base dir for tmp web files", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,6 +338,9 @@ public class WebServerComponent implements ExternalComponent {
|
||||||
webapp.addFilter(new FilterHolder(AuthenticationFilter.class), "/auth/login/*", EnumSet.of(DispatcherType.REQUEST));
|
webapp.addFilter(new FilterHolder(AuthenticationFilter.class), "/auth/login/*", EnumSet.of(DispatcherType.REQUEST));
|
||||||
|
|
||||||
webapp.setWar(warDirectory.resolve(warFile).toString());
|
webapp.setWar(warDirectory.resolve(warFile).toString());
|
||||||
|
|
||||||
|
webapp.setAttribute("org.eclipse.jetty.webapp.basetempdir", temporaryWarDir.toFile().getAbsolutePath());
|
||||||
|
|
||||||
handlers.addHandler(webapp);
|
handlers.addHandler(webapp);
|
||||||
return webapp;
|
return webapp;
|
||||||
}
|
}
|
||||||
|
|
|
@ -408,20 +408,17 @@ public class WebServerComponentTest extends Assert {
|
||||||
//create some garbage
|
//create some garbage
|
||||||
List<WebAppContext> contexts = webServerComponent.getWebContexts();
|
List<WebAppContext> contexts = webServerComponent.getWebContexts();
|
||||||
|
|
||||||
File targetDir = new File("./target");
|
|
||||||
File workDir = new File(targetDir, "web-work");
|
|
||||||
workDir.mkdir();
|
|
||||||
|
|
||||||
WebInfConfiguration cfg = new WebInfConfiguration();
|
WebInfConfiguration cfg = new WebInfConfiguration();
|
||||||
assertEquals(1, contexts.size());
|
assertEquals(1, contexts.size());
|
||||||
WebAppContext ctxt = contexts.get(0);
|
WebAppContext ctxt = contexts.get(0);
|
||||||
List<File> garbage = new ArrayList<>();
|
List<File> garbage = new ArrayList<>();
|
||||||
|
|
||||||
ctxt.setAttribute("javax.servlet.context.tempdir", new File(workDir, "jetty-context0"));
|
|
||||||
|
|
||||||
cfg.resolveTempDirectory(ctxt);
|
cfg.resolveTempDirectory(ctxt);
|
||||||
|
|
||||||
File tmpdir = ctxt.getTempDirectory();
|
File tmpdir = ctxt.getTempDirectory();
|
||||||
File testDir = tmpdir.getParentFile();
|
File testDir = tmpdir.getParentFile();
|
||||||
|
|
||||||
createGarbagesInDir(testDir, garbage);
|
createGarbagesInDir(testDir, garbage);
|
||||||
|
|
||||||
assertTrue(garbage.size() > 0);
|
assertTrue(garbage.size() > 0);
|
||||||
|
|
Loading…
Reference in New Issue