ARTEMIS-2596 Move tmp web dir for a safe clean up

This commit is contained in:
brusdev 2020-06-16 19:20:07 +02:00 committed by Clebert Suconic
parent d7f5e92e77
commit 819f1834f5
2 changed files with 20 additions and 25 deletions

View File

@ -19,6 +19,7 @@ package org.apache.activemq.artemis.component;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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.util.ssl.SslContextFactory;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.jboss.logging.Logger;
import javax.servlet.DispatcherType;
@ -65,6 +65,7 @@ public class WebServerComponent implements ExternalComponent {
private List<WebAppContext> webContexts;
private ServerConnector connector;
private Path artemisHomePath;
private Path temporaryWarDir;
@Override
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 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) {
webContexts = new ArrayList<>();
for (AppDTO app : webServerConfig.apps) {
@ -283,29 +289,18 @@ public class WebServerComponent implements ExternalComponent {
//there is no webapp to be deployed (as in some tests)
return;
}
List<File> temporaryFiles = new ArrayList<>();
for (WebAppContext context : webContexts) {
WebInfConfiguration config = new WebInfConfiguration();
try {
config.resolveTempDirectory(context);
File webTmpBase = context.getTempDirectory().getParentFile();
if (webTmpBase.exists()) {
webTmpBase.listFiles((f) -> {
temporaryFiles.add(f);
return false;
});
}
List<File> temporaryFiles = new ArrayList<>();
Files.newDirectoryStream(temporaryWarDir).forEach(path -> temporaryFiles.add(path.toFile()));
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);
}
}
}
public void cleanupWebTemporaryFiles(List<WebAppContext> webContexts) throws Exception {
List<File> temporaryFiles = new ArrayList<>();
@ -343,6 +338,9 @@ public class WebServerComponent implements ExternalComponent {
webapp.addFilter(new FilterHolder(AuthenticationFilter.class), "/auth/login/*", EnumSet.of(DispatcherType.REQUEST));
webapp.setWar(warDirectory.resolve(warFile).toString());
webapp.setAttribute("org.eclipse.jetty.webapp.basetempdir", temporaryWarDir.toFile().getAbsolutePath());
handlers.addHandler(webapp);
return webapp;
}

View File

@ -408,20 +408,17 @@ public class WebServerComponentTest extends Assert {
//create some garbage
List<WebAppContext> contexts = webServerComponent.getWebContexts();
File targetDir = new File("./target");
File workDir = new File(targetDir, "web-work");
workDir.mkdir();
WebInfConfiguration cfg = new WebInfConfiguration();
assertEquals(1, contexts.size());
WebAppContext ctxt = contexts.get(0);
List<File> garbage = new ArrayList<>();
ctxt.setAttribute("javax.servlet.context.tempdir", new File(workDir, "jetty-context0"));
cfg.resolveTempDirectory(ctxt);
File tmpdir = ctxt.getTempDirectory();
File testDir = tmpdir.getParentFile();
createGarbagesInDir(testDir, garbage);
assertTrue(garbage.size() > 0);