Permalink
Browse files

Working on tasks (yeuch)

  • Loading branch information...
1 parent e53d9a9 commit 68581b2c57610438ec46527c56fffc45f519b9ce @shellac shellac committed Apr 13, 2012
@@ -24,7 +24,6 @@ public FileRepository(String base) {
public Path create(String id, Path source) throws IOException {
Path target = Files.createDirectory(root.resolve(id));
- Util.copyDirectory(source, target);
return target;
}
}
@@ -4,6 +4,8 @@
*/
package uk.ac.bristol.dundry.dao;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
@@ -14,13 +16,15 @@
import com.hp.hpl.jena.vocabulary.RDFS;
import java.io.IOException;
import java.nio.file.Path;
-import java.util.Calendar;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
+import java.util.logging.Level;
+import org.quartz.Job;
+import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
import uk.ac.bristol.dundry.model.ResourceCollection;
+import uk.ac.bristol.dundry.tasks.CopyTask;
/**
*
@@ -33,12 +37,27 @@
// play it safe. radix of 36 is ideal
static final int RADIX = Math.min(Character.MAX_RADIX, 36);
+ @Autowired private TaskManager taskManager;
private final FileRepository fileRepo;
private final MetadataStore mdStore;
+ private final List<Class<? extends Job>> jobs;
- public Repository(FileRepository fileRepo, MetadataStore mdStore) {
+ public Repository(FileRepository fileRepo, MetadataStore mdStore, List<String> jobsClasses) {
this.fileRepo = fileRepo;
this.mdStore = mdStore;
+
+ // Load up job classes
+ jobs = new ArrayList<>();
+ for (String jobClassName: jobsClasses) {
+ // Try to load the class. Check it is a Job.
+ try {
+ Class<?> job = Repository.class.getClassLoader().loadClass(jobClassName);
+ if (Job.class.isAssignableFrom(job)) jobs.add((Class<? extends Job>) job);
+ else log.error("Class <{}> is not a Job. Ignoring.", jobClassName);
+ } catch (ClassNotFoundException ex) {
+ log.error("Job class <{}> not found. Ignoring.", jobClassName);
+ }
+ }
}
public ResourceCollection getIds() {
@@ -73,7 +92,7 @@ public ResourceCollection getIds() {
* @return
* @throws IOException
*/
- public String create(Path source, String creator, Resource subject) throws IOException {
+ public String create(Path source, String creator, Resource subject) throws IOException, SchedulerException {
// Create a random id!
UUID randId = UUID.randomUUID();
String baseEncoded =
@@ -86,15 +105,38 @@ public String create(Path source, String creator, Resource subject) throws IOExc
Path repoDir = fileRepo.create(id, source);
- subject.addLiteral(DCTerms.dateSubmitted, subject.getModel().createTypedLiteral(Calendar.getInstance()));
+ subject.addLiteral(DCTerms.dateSubmitted, Calendar.getInstance());
subject.addLiteral(DCTerms.source, source.toAbsolutePath().toString());
subject.addProperty(DCTerms.creator, creator);
mdStore.create(toInternalId(id), subject.getModel());
+ startTasks(id, CopyTask.class, CopyTask.FROM, source, CopyTask.TO, repoDir);
+
return id;
}
+ /**
+ * Begin the post-creation tasks.
+ *
+ * @param loadJob First task to get get data into the repository
+ * @param settings
+ */
+ private void startTasks(String id, Class<? extends Job> loadJob, Object... settings) throws SchedulerException {
+ Map<String, Object> context = new HashMap<>();
+
+ for (int i = 0; i < settings.length; i++) {
+ context.put((String) settings[i], settings[i+1]);
+ }
+
+ List<Class<? extends Job>> allJobs = new ArrayList<>();
+
+ allJobs.add(loadJob);
+ allJobs.addAll(jobs);
+
+ taskManager.startJobs(id, allJobs, context);
+ }
+
public Resource getMetadata(String id) {
return mdStore.getDataAbout(toInternalId(id)).createResource(toInternalId(id));
}
@@ -2,6 +2,7 @@
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;
@@ -62,6 +63,28 @@ public void startJob(String id) throws SchedulerException {
startJobsInOrder(id, jobs);
}
+ public void startJobs(String id, List<Class<? extends Job>> jobs, Map<String, Object> context)
+ throws SchedulerException {
+ List<JobDetail> jobDetails = new ArrayList<>();
+
+ // Create context for these jobs
+ JobDataMap jobData = new JobDataMap();
+ jobData.putAll(context);
+
+ // Make the job details
+ int count = 0;
+ for (Class<? extends Job> job: jobs) {
+ JobDetail jobDetail = newJob(job)
+ .withIdentity(job.getName() + count++, id)
+ .usingJobData(jobData)
+ .build();
+
+ jobDetails.add(jobDetail);
+ }
+
+ startJobsInOrder(id, jobDetails);
+ }
+
public void startJobsInOrder(String id, List<JobDetail> jobs) throws SchedulerException {
if (jobs.isEmpty()) {
return;
@@ -0,0 +1,87 @@
+package uk.ac.bristol.dundry.tasks;
+
+import java.io.IOException;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.logging.Level;
+import org.quartz.JobDataMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import uk.ac.bristol.dundry.Util;
+import uk.ac.bristol.dundry.dao.MetadataStore;
+
+/**
+ *
+ * @author pldms
+ */
+public class CopyTask extends JobBase {
+
+ final static Logger log = LoggerFactory.getLogger(CopyTask.class);
+
+
+ public final static String FROM = "copy-task-from";
+ public final static String TO = "copy-task-to";
+
+ @Override
+ public void execute(MetadataStore store, String id, Path root, JobDataMap jobData) {
+ Path from = (Path) jobData.get(FROM);
+ Path to = (Path) jobData.get(TO);
+
+ try {
+ copyDirectory(from, to);
+ } catch (IOException ex) {
+ throw new RuntimeException("Error copying", ex);
+ }
+ }
+
+ @Override
+ public String getName() {
+ return "Copy durectory";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Recursively copy a directory.";
+ }
+
+ /**
+ * Recursively copy a directory into another directory.
+ * Copying a to b will result in b/a...
+ * @param from Source
+ * @param to Destination
+ * @return
+ * @throws IOException
+ */
+ public static Path copyDirectory(final Path from, final Path to) throws IOException {
+
+ log.info("Copy {} to {}", from, to);
+
+ // We relativise paths to the parent of from
+ // So /ex/a/b becomes /ex/a and we copy b directory
+ final Path parent = from.getParent();
+
+ try {
+ return Files.walkFileTree(from, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Path rel = parent.relativize(file);
+ log.debug("Visit {}", file);
+ Files.copy(file, to.resolve(rel), StandardCopyOption.COPY_ATTRIBUTES);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+ Path rel = parent.relativize(dir);
+ log.trace("Visit dir {}", dir);
+ log.trace("Create dir {}", to.resolve(rel));
+ Files.createDirectory(to.resolve(rel));
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ } catch (IOException ex) {
+ // Clean up?
+ throw ex;
+ }
+ }
+}

0 comments on commit 68581b2

Please sign in to comment.