Permalink
Browse files

Initial import of working code.

  • Loading branch information...
buckett committed Dec 4, 2009
0 parents commit 83c3e50ad2facde938a09c1e2c5da1ed583b86c5
@@ -0,0 +1,3 @@
+.classpath
+.project
+.settings
@@ -0,0 +1,40 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>content-cleanup</artifactId>
+ <groupId>uk.ac.ox.oucs.vle</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <groupId>uk.ac.ox.oucs.vle</groupId>
+ <artifactId>content-cleanup-impl</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>content-cleanup-impl</name>
+ <url>http://maven.apache.org</url>
+ <dependencies>
+ <dependency>
+ <groupId>org.sakaiproject.kernel</groupId>
+ <artifactId>sakai-kernel-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.sakaiproject.kernel</groupId>
+ <artifactId>sakai-component-manager</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.sakaiproject</groupId>
+ <artifactId>sakai-scheduler-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>quartz</groupId>
+ <artifactId>quartz</artifactId>
+ <version>1.5.2</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
@@ -0,0 +1,120 @@
+package uk.ac.ox.oucs.vle.content;
+
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.sakaiproject.component.api.ServerConfigurationService;
+import org.sakaiproject.content.api.ContentHostingService;
+import org.sakaiproject.content.api.ContentResource;
+import org.sakaiproject.entity.api.ResourceProperties;
+import org.sakaiproject.exception.IdUnusedException;
+import org.sakaiproject.exception.InUseException;
+import org.sakaiproject.exception.PermissionException;
+import org.sakaiproject.exception.TypeException;
+import org.sakaiproject.time.api.Time;
+import org.sakaiproject.time.api.TimeService;
+import org.sakaiproject.tool.api.Session;
+import org.sakaiproject.tool.api.SessionManager;
+
+/**
+ * Job to remove old delete content from content hosting. This is in a seperate
+ * project as the kernel (content hosting) can't bind to stuff outside it.
+ *
+ * @author buckett
+ *
+ */
+public class CleanupDeletedContent implements Job {
+
+ private static final Log log = LogFactory
+ .getLog(CleanupDeletedContent.class);
+
+ private ContentHostingService chs;
+ private ServerConfigurationService scs;
+ private TimeService ts;
+ private SessionManager sm;
+
+ public void setContentHostingService(ContentHostingService chs) {
+ this.chs = chs;
+ }
+
+ public void setServerConfigurationService(ServerConfigurationService scs) {
+ this.scs = scs;
+ }
+
+ public void setTimeService(TimeService ts) {
+ this.ts = ts;
+ }
+
+ public void setSessionManager(SessionManager sm) {
+ this.sm = sm;
+ }
+
+ public void execute(JobExecutionContext context)
+ throws JobExecutionException {
+ Session sakaiSession = sm.getCurrentSession();
+ sakaiSession.setUserId("admin");
+ sakaiSession.setUserEid("admin");
+
+ List<ContentResource> deleted = (List<ContentResource>) chs
+ .getAllDeletedResources("/");
+ long daysToKeep = scs.getInt("keep.deleted.files.days", 30);
+ Time oldest = ts.newTime(System.currentTimeMillis()
+ - (daysToKeep * 1000 * 60 * 60 * 24));
+ log.info("Looking at " + deleted.size()
+ + " resources, and removing anything older than: " + oldest);
+ int removed = 0, attempted = 0;
+ long totalSize = 0, removedSize = 0;
+ for (ContentResource resource : deleted) {
+ // We can't get at the deleted field in the DB but the modification
+ // time is updated when it's deleted.
+ Object property = resource.getProperties().get(ResourceProperties.PROP_MODIFIED_DATE);
+ long size = resource.getContentLength();
+ totalSize += size;
+ if (property != null && property instanceof String) {
+ Time time = ts.newTimeGmt((String)property);
+ if (oldest.after(time)) {
+ try {
+ attempted++;
+ chs.removeDeletedResource(resource.getId());
+ removed++;
+ removedSize += size;
+ } catch (PermissionException e) {
+ log.warn("Failed to remove due to lack of permission: "
+ + resource.getId());
+ } catch (IdUnusedException e) {
+ log
+ .warn("Failed to remove due to not beging able to find: "
+ + resource.getId());
+ } catch (TypeException e) {
+ log.warn("Failed to remove due to type exception: "
+ + resource.getId());
+ } catch (InUseException e) {
+ log.warn("Failed to remove due resource being in use: "
+ + resource.getId());
+ }
+ }
+ }
+ }
+ int failed = attempted - removed;
+ log.info("Out of " + deleted.size() + "(~"+ formatSize(totalSize)+ ") "
+ + "deleted resources, successfully removed " + removed + "(~"+ formatSize(removedSize)+ ")"
+ + ((failed > 0) ? ", failed on " + failed + " resources": ""));
+ }
+
+ static String formatSize(long size) {
+ if (size >= 1L<<30) {
+ return ""+ (size / 1L>>30)+ "Gb";
+ } else if (size >= 1L<<20) {
+ return ""+ (size / 1L>>20)+ "Mb";
+ } else if (size >= 1L<<10) {
+ return ""+ (size / 1L>>10)+ "kb";
+ } else {
+ return ""+ size+ "b";
+ }
+ }
+
+}
@@ -0,0 +1,20 @@
+package uk.ac.ox.oucs.vle.content;
+
+import junit.framework.TestCase;
+
+public class CleanupDeletedContentTest extends TestCase {
+
+ public void testFormatSize() {
+ assertEquals("100Gb", CleanupDeletedContent.formatSize(1024L * 1024 * 1024 * 100));
+
+ assertEquals("2Gb", CleanupDeletedContent.formatSize(1L+ Integer.MAX_VALUE));
+ assertEquals("1Gb", CleanupDeletedContent.formatSize(Integer.MAX_VALUE));
+
+ assertEquals("1Mb", CleanupDeletedContent.formatSize(1024 * 1024));
+
+ assertEquals("1023b", CleanupDeletedContent.formatSize(1023));
+ assertEquals("1kb", CleanupDeletedContent.formatSize(1024));
+ assertEquals("1kb", CleanupDeletedContent.formatSize(1025));
+ }
+
+}
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>content-cleanup</artifactId>
+ <groupId>uk.ac.ox.oucs.vle</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <groupId>uk.ac.ox.oucs.vle</groupId>
+ <artifactId>content-cleanup-pack</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>content-cleanup-pack</name>
+ <packaging>sakai-component</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>uk.ac.ox.oucs.vle</groupId>
+ <artifactId>content-cleanup-impl</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+</project>
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans>
+ <bean id="uk.ac.ox.oucs.vle.content.CleanupDeletedContent"
+ class="uk.ac.ox.oucs.vle.content.CleanupDeletedContent">
+ <property name="contentHostingService">
+ <ref bean="org.sakaiproject.content.api.ContentHostingService" />
+ </property>
+ <property name="serverConfigurationService">
+ <ref bean="org.sakaiproject.component.api.ServerConfigurationService"/>
+ </property>
+ <property name="timeService">
+ <ref bean="org.sakaiproject.time.api.TimeService"/>
+ </property>
+ <property name="sessionManager">
+ <ref bean="org.sakaiproject.tool.api.SessionManager"/>
+ </property>
+ </bean>
+
+ <bean
+ id="org.sakaiproject.api.app.scheduler.JobBeanWrapper.cleanupDeletedContent"
+ class="org.sakaiproject.component.app.scheduler.jobs.SpringStatefulJobBeanWrapper"
+ singleton="true" init-method="init">
+ <property name="beanId">
+ <value>uk.ac.ox.oucs.vle.content.CleanupDeletedContent</value>
+ </property>
+ <property name="jobName">
+ <value>Remove old deleted content</value>
+ </property>
+ <property name="schedulerManager">
+ <ref bean="org.sakaiproject.api.app.scheduler.SchedulerManager" />
+ </property>
+ </bean>
+</beans>
73 pom.xml
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>uk.ac.ox.oucs.vle</groupId>
+ <artifactId>content-cleanup</artifactId>
+ <packaging>pom</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>content-cleanup</name>
+ <url>http://maven.apache.org</url>
+ <repositories>
+ <repository>
+ <id>sakai-maven</id>
+ <name>Sakai Maven Repo</name>
+ <layout>default</layout>
+ <url>http://source.sakaiproject.org/maven2</url>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+ <pluginRepositories>
+ <pluginRepository>
+ <id>maven-repo.oucs</id>
+ <name>OUCS Maven Repository</name>
+ <layout>default</layout>
+ <url>http://maven-repo.oucs.ox.ac.uk/content/repositories/releases</url>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </pluginRepository>
+ </pluginRepositories>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.sakaiproject</groupId>
+ <artifactId>master</artifactId>
+ <version>2.6-SNAPSHOT</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+ <modules>
+ <module>content-cleanup-impl</module>
+ <module>content-cleanup-pack</module>
+ </modules>
+ <build>
+ <plugins>
+ <plugin>
+ <inherited>true</inherited>
+ <groupId>org.sakaiproject.maven.plugins</groupId>
+ <artifactId>sakai</artifactId>
+ <version>1.6.ox1</version>
+ <extensions>true</extensions>
+ <configuration>
+ <deployDirectory>${maven.tomcat.home}</deployDirectory>
+ <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <inherited>true</inherited>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>

0 comments on commit 83c3e50

Please sign in to comment.