]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/process/FileSlcExecutionNotifier.java
git-svn-id: https://svn.argeo.org/slc/trunk@1286 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / process / FileSlcExecutionNotifier.java
1 package org.argeo.slc.core.process;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.xml.transform.stream.StreamResult;
12
13 import org.springframework.oxm.Marshaller;
14
15 import org.apache.commons.io.IOUtils;
16 import org.apache.commons.logging.LogFactory;
17
18 import org.argeo.slc.core.SlcException;
19
20 public class FileSlcExecutionNotifier implements SlcExecutionNotifier {
21 private final static SimpleDateFormat sdf = new SimpleDateFormat(
22 "yyyyMMdd-HHmmss");
23
24 private String basePath;
25 private Marshaller marshaller;
26
27 private Map<String, String> uuidToDir = new HashMap<String, String>();
28
29 public void addSteps(SlcExecution slcExecution,
30 List<SlcExecutionStep> additionalSteps) {
31 writeSlcExecution(slcExecution);
32 }
33
34 public void newExecution(SlcExecution slcExecution) {
35 String dirPath = basePath + File.separator + sdf.format(new Date())
36 + '-' + slcExecution.getUuid();
37 File dir = new File(dirPath);
38 dir.mkdirs();
39
40 uuidToDir.put(slcExecution.getUuid(), dirPath);
41
42 writeSlcExecution(slcExecution);
43 }
44
45 public void updateExecution(SlcExecution slcExecution) {
46 writeSlcExecution(slcExecution);
47 }
48
49 public void updateStatus(SlcExecution slcExecution, String oldStatus,
50 String newStatus) {
51 writeSlcExecution(slcExecution);
52 }
53
54 protected void writeSlcExecution(SlcExecution slcExecution) {
55 FileWriter out = null;
56 try {
57 out = new FileWriter(getFilePath(slcExecution));
58 marshaller.marshal(slcExecution, new StreamResult(out));
59 } catch (Exception e) {
60 throw new SlcException("Cannot marshall SlcExecution to "
61 + getFilePath(slcExecution), e);
62 } finally {
63 IOUtils.closeQuietly(out);
64 }
65 }
66
67 protected String getFileName(SlcExecution slcExecution) {
68 return "SlcExecution-" + slcExecution.getUuid() + ".xml";
69 }
70
71 protected String getFilePath(SlcExecution slcExecution) {
72 String dirPath = uuidToDir.get(slcExecution.getUuid());
73 return dirPath + File.separator + "SlcExecution-"
74 + slcExecution.getUuid() + ".xml";
75 }
76
77 public void setBasePath(String basePath) {
78 this.basePath = basePath;
79 }
80
81 public void setMarshaller(Marshaller marshaller) {
82 this.marshaller = marshaller;
83 }
84
85 }