Skip to content

Commit

Permalink
Implementing deletion of files in sample application inside application.
Browse files Browse the repository at this point in the history
  • Loading branch information
klakegg committed Aug 31, 2015
1 parent b7a775c commit fd59316
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
7 changes: 7 additions & 0 deletions validator-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<version>${tiles.version}</version>
</dependency>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -117,6 +118,12 @@
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.2</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
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;
import javax.xml.bind.JAXBContext;
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;
Expand All @@ -30,6 +30,8 @@ public class WorkspaceService {

@Value("${workspace}")
private String dirWorkspace;
@Value("${workspace.expire}")
private int workspaceExpire;

private Marshaller reportMarshaller;
private Unmarshaller reportUnmarshaller;
Expand Down Expand Up @@ -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);
}
}
}
}
}
}
7 changes: 6 additions & 1 deletion validator-web/src/main/resources/context-application.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
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">

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
Expand All @@ -26,4 +29,6 @@
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<task:annotation-driven />

</beans>
3 changes: 3 additions & 0 deletions validator-web/src/main/resources/validator.default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.".

0 comments on commit fd59316

Please sign in to comment.