From fd5931675a049964cd4de8114d9649e7f7914f08 Mon Sep 17 00:00:00 2001 From: Erlend Klakegg Bergheim Date: Mon, 31 Aug 2015 13:38:47 +0200 Subject: [PATCH] Implementing deletion of files in sample application inside application. --- validator-web/pom.xml | 7 +++++ .../validator/service/WorkspaceService.java | 31 ++++++++++++++++--- .../main/resources/context-application.xml | 7 ++++- .../resources/validator.default.properties | 3 ++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/validator-web/pom.xml b/validator-web/pom.xml index 745fe3b5..0fb61c59 100644 --- a/validator-web/pom.xml +++ b/validator-web/pom.xml @@ -106,6 +106,7 @@ ${tiles.version} + com.fasterxml.jackson.core jackson-databind @@ -117,6 +118,12 @@ ${jackson.version} + + joda-time + joda-time + 2.8.2 + + commons-fileupload commons-fileupload diff --git a/validator-web/src/main/java/no/difi/vefa/validator/service/WorkspaceService.java b/validator-web/src/main/java/no/difi/vefa/validator/service/WorkspaceService.java index 76f6ee48..c6f2a8bf 100644 --- a/validator-web/src/main/java/no/difi/vefa/validator/service/WorkspaceService.java +++ b/validator-web/src/main/java/no/difi/vefa/validator/service/WorkspaceService.java @@ -4,10 +4,13 @@ import no.difi.vefa.validator.Validation; import no.difi.vefa.validator.api.ValidatorException; import no.difi.xsd.vefa.validator._1.Report; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @@ -15,10 +18,7 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStream; +import java.io.*; import java.util.UUID; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -30,6 +30,8 @@ public class WorkspaceService { @Value("${workspace}") private String dirWorkspace; + @Value("${workspace.expire}") + private int workspaceExpire; private Marshaller reportMarshaller; private Unmarshaller reportUnmarshaller; @@ -121,4 +123,25 @@ public File getSource(String identifier) { public File getView(String identifier) { return new File(getFolder(identifier), "view.html"); } + + @Scheduled(fixedDelay = 60 * 60 * 1000, initialDelay = 1000) + public void cleanWorkspace() { + logger.info("Cleaning workspace."); + + if (workspaceExpire < 0) + return; + + for (File f : new File(dirWorkspace).listFiles()) { + if (f.isDirectory() && !new File(f, ".keep").exists()) { + if (new DateTime(f.lastModified()).isBefore(DateTime.now().minusDays(workspaceExpire))) { + try { + logger.info(String.format("Delete validation '%s'.", f.getName())); + FileUtils.deleteDirectory(f); + } catch (IOException e) { + logger.warn(e.getMessage(), e); + } + } + } + } + } } diff --git a/validator-web/src/main/resources/context-application.xml b/validator-web/src/main/resources/context-application.xml index f12194bc..1a4ef961 100644 --- a/validator-web/src/main/resources/context-application.xml +++ b/validator-web/src/main/resources/context-application.xml @@ -1,11 +1,14 @@ + http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/task + http://www.springframework.org/schema/task/spring-task-3.0.xsd"> @@ -26,4 +29,6 @@ + + \ No newline at end of file diff --git a/validator-web/src/main/resources/validator.default.properties b/validator-web/src/main/resources/validator.default.properties index a1853a17..d12be951 100644 --- a/validator-web/src/main/resources/validator.default.properties +++ b/validator-web/src/main/resources/validator.default.properties @@ -13,4 +13,7 @@ repository=https://test-vefa.difi.no/validator/repo/ # Directory for upload and report. workspace=workspace +# Defines the maximum age of content in workspace in days. +workspace.expire=14 + # Add any validator properties prefixed with "validator.". \ No newline at end of file