]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
e032e6a0c3f69c0efc9426c21b98e7eabf7e4cc9
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / FileExecutionResources.java
1 package org.argeo.slc.core.execution;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.text.SimpleDateFormat;
9
10 import org.apache.commons.io.IOUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.execution.ExecutionContext;
15 import org.springframework.core.io.FileSystemResource;
16 import org.springframework.core.io.Resource;
17 import org.springframework.util.Assert;
18
19 public class FileExecutionResources implements ExecutionResources {
20 private final static Log log = LogFactory
21 .getLog(FileExecutionResources.class);
22 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME = "executionResources";
23 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH = System
24 .getProperty("java.io.tmpdir")
25 + File.separator
26 + "slc"
27 + File.separator
28 + DEFAULT_EXECUTION_RESOURCES_DIRNAME;
29
30 private File baseDir;
31 private ExecutionContext executionContext;
32 private String prefixDatePattern = "yyyyMMdd_HHmmss_";
33 private SimpleDateFormat sdf = null;
34
35 private Boolean withExecutionSubdirectory = true;
36
37 public FileExecutionResources() {
38 // Default base directory
39 String osgiInstanceArea = System.getProperty("osgi.instance.area");
40 String osgiInstanceAreaDefault = System
41 .getProperty("osgi.instance.area.default");
42 String tempDir = System.getProperty("java.io.tmpdir");
43
44 if (osgiInstanceArea != null) {
45 // within OSGi with -data specified
46 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
47 baseDir = new File(osgiInstanceArea + File.separator
48 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
49 } else if (osgiInstanceAreaDefault != null) {
50 // within OSGi without -data specified
51 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
52 baseDir = new File(osgiInstanceAreaDefault + File.separator
53 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
54 } else {// outside OSGi
55 baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
56 }
57 }
58
59 protected SimpleDateFormat sdf() {
60 // Lazy init in case prefix has been externally set
61 if (sdf == null)
62 sdf = new SimpleDateFormat(prefixDatePattern);
63 return sdf;
64 }
65
66 public Resource getWritableResource(String relativePath) {
67 File file = getFile(relativePath);
68 File parentDir = file.getParentFile();
69
70 if (!parentDir.exists()) {
71 // Creates if necessary
72 if (log.isTraceEnabled())
73 log.trace("Creating parent directory " + parentDir);
74 parentDir.mkdirs();
75 }
76 Resource resource = new FileSystemResource(file);
77
78 if (log.isTraceEnabled())
79 log.trace("Returns writable resource " + resource);
80 return resource;
81 }
82
83 public String getAsOsPath(Resource resource, Boolean overwrite) {
84 File file = null;
85 try {
86 file = resource.getFile();
87 return file.getCanonicalPath();
88 } catch (IOException e) {
89 if (log.isTraceEnabled())
90 log
91 .trace("Resource "
92 + resource
93 + " is not available on the file system. Retrieving it...");
94 }
95
96 InputStream in = null;
97 OutputStream out = null;
98 try {
99 String path = resource.getURL().getPath();
100 file = getFile(path);
101 if (file.exists() && !overwrite)
102 return file.getCanonicalPath();
103
104 file.getParentFile().mkdirs();
105 in = resource.getInputStream();
106 out = new FileOutputStream(file);
107 IOUtils.copy(in, out);
108 if (log.isDebugEnabled())
109 log.debug("Retrieved " + resource + " to OS file " + file);
110 return file.getCanonicalPath();
111 } catch (IOException e) {
112 throw new SlcException("Could not make resource " + resource
113 + " an OS file.", e);
114 } finally {
115 IOUtils.closeQuietly(in);
116 IOUtils.closeQuietly(out);
117 }
118 }
119
120 public File getFile(String relativePath) {
121 Assert.notNull(executionContext, "execution context is null");
122
123 if (withExecutionSubdirectory) {
124 String path = baseDir.getPath() + File.separator
125 + sdf().format(executionContext.getCreationDate())
126 + executionContext.getUuid();
127 File executionDir = new File(path);
128
129 return new File(executionDir.getPath() + File.separator
130 + relativePath.replace('/', File.separatorChar));
131 } else {
132 return new File(baseDir.getPath() + File.separator
133 + relativePath.replace('/', File.separatorChar));
134 }
135 }
136
137 protected String removeFilePrefix(String url) {
138 if (url.startsWith("file:"))
139 return url.substring("file:".length());
140 else if (url.startsWith("reference:file:"))
141 return url.substring("reference:file:".length());
142 else
143 return url;
144 }
145
146 public void setBaseDir(File baseDir) {
147 this.baseDir = baseDir;
148 }
149
150 public void setExecutionContext(ExecutionContext executionContext) {
151 this.executionContext = executionContext;
152 }
153
154 public void setPrefixDatePattern(String prefixDatePattern) {
155 this.prefixDatePattern = prefixDatePattern;
156 }
157
158 public File getBaseDir() {
159 return baseDir;
160 }
161
162 public ExecutionContext getExecutionContext() {
163 return executionContext;
164 }
165
166 public String getPrefixDatePattern() {
167 return prefixDatePattern;
168 }
169
170 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
171 this.withExecutionSubdirectory = withExecutionSubdirectory;
172 }
173
174 }