]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/core/execution/FileExecutionResources.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / 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 import java.util.Date;
10
11 import org.apache.commons.io.IOUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.execution.ExecutionContext;
16 import org.springframework.core.io.FileSystemResource;
17 import org.springframework.core.io.Resource;
18 import org.springframework.util.Assert;
19
20 /** Implements write access to resources based on standard Java {@link File} */
21 public class FileExecutionResources implements ExecutionResources {
22 private final static Log log = LogFactory
23 .getLog(FileExecutionResources.class);
24 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME = "executionResources";
25 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH = System
26 .getProperty("java.io.tmpdir")
27 + File.separator
28 + System.getProperty("user.name")
29 + File.separator
30 + "slc"
31 + File.separator + DEFAULT_EXECUTION_RESOURCES_DIRNAME;
32
33 private File baseDir;
34 private ExecutionContext executionContext;
35 private String prefixDatePattern = "yyMMdd_HHmmss_SSS";
36 private SimpleDateFormat sdf = null;
37
38 private Boolean withExecutionSubdirectory = true;
39
40 public FileExecutionResources() {
41 // Default base directory
42 String osgiInstanceArea = System.getProperty("osgi.instance.area");
43 String osgiInstanceAreaDefault = System
44 .getProperty("osgi.instance.area.default");
45
46 if (osgiInstanceArea != null) {
47 // within OSGi with -data specified
48 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
49 baseDir = new File(osgiInstanceArea + File.separator
50 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
51 } else if (osgiInstanceAreaDefault != null) {
52 // within OSGi without -data specified
53 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
54 baseDir = new File(osgiInstanceAreaDefault + File.separator
55 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
56 } else {// outside OSGi
57 baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
58 }
59 }
60
61 protected SimpleDateFormat sdf() {
62 // Lazy init in case prefix has been externally set
63 if (sdf == null)
64 sdf = new SimpleDateFormat(prefixDatePattern);
65 return sdf;
66 }
67
68 public Resource getWritableResource(String relativePath) {
69 File file = getFile(relativePath);
70 File parentDir = file.getParentFile();
71
72 if (!parentDir.exists()) {
73 // Creates if necessary
74 if (log.isTraceEnabled())
75 log.trace("Creating parent directory " + parentDir);
76 parentDir.mkdirs();
77 }
78 Resource resource = new FileSystemResource(file);
79
80 if (log.isTraceEnabled())
81 log.trace("Returns writable resource " + resource);
82 return resource;
83 }
84
85 public String getWritableOsPath(String relativePath) {
86 try {
87 return getFile(relativePath).getCanonicalPath();
88 } catch (IOException e) {
89 throw new SlcException("Cannot find canonical path", e);
90 }
91 }
92
93 public File getWritableOsFile(String relativePath) {
94 return getFile(relativePath);
95 }
96
97 public String getAsOsPath(Resource resource, Boolean overwrite) {
98 File file = fileFromResource(resource);
99 if (file != null)
100 try {
101 if (log.isTraceEnabled())
102 log.debug("Directly interpret " + resource + " as OS file "
103 + file);
104 return file.getCanonicalPath();
105 } catch (IOException e1) {
106 // silent
107 }
108
109 if (log.isTraceEnabled())
110 log.trace("Resource " + resource
111 + " is not available on the file system. Retrieving it...");
112
113 InputStream in = null;
114 OutputStream out = null;
115 try {
116 String path = resource.getURL().getPath();
117 file = getFile(path);
118 if (file.exists() && !overwrite)
119 return file.getCanonicalPath();
120
121 file.getParentFile().mkdirs();
122 in = resource.getInputStream();
123 out = new FileOutputStream(file);
124 IOUtils.copy(in, out);
125 if (log.isDebugEnabled())
126 log.debug("Retrieved " + resource + " to OS file " + file);
127 return file.getCanonicalPath();
128 } catch (IOException e) {
129 throw new SlcException("Could not make resource " + resource
130 + " an OS file.", e);
131 } finally {
132 IOUtils.closeQuietly(in);
133 IOUtils.closeQuietly(out);
134 }
135 }
136
137 /**
138 * Extract the underlying file from the resource.
139 *
140 * @return the file or null if no files support this resource.
141 */
142 protected File fileFromResource(Resource resource) {
143 try {
144 return resource.getFile();
145 } catch (IOException e) {
146 return null;
147 }
148
149 }
150
151 protected File getFile(String relativePath) {
152 File writableBaseDir = getWritableBaseDir();
153 return new File(writableBaseDir.getPath() + File.separator
154 + relativePath.replace('/', File.separatorChar));
155 }
156
157 public File getWritableBaseDir() {
158 if (withExecutionSubdirectory) {
159 Date executionContextCreationDate = (Date) executionContext
160 .getVariable(ExecutionContext.VAR_EXECUTION_CONTEXT_CREATION_DATE);
161 Assert.notNull(executionContext, "execution context is null");
162 String path = baseDir.getPath() + File.separator
163 + sdf().format(executionContextCreationDate);
164 // TODO write execution id somewhere? like in a txt file
165 return new File(path);
166 } else {
167 return baseDir;
168 }
169 }
170
171 protected String removeFilePrefix(String url) {
172 if (url.startsWith("file:"))
173 return url.substring("file:".length());
174 else if (url.startsWith("reference:file:"))
175 return url.substring("reference:file:".length());
176 else
177 return url;
178 }
179
180 public void setBaseDir(File baseDir) {
181 this.baseDir = baseDir;
182 }
183
184 public void setExecutionContext(ExecutionContext executionContext) {
185 this.executionContext = executionContext;
186 }
187
188 public void setPrefixDatePattern(String prefixDatePattern) {
189 this.prefixDatePattern = prefixDatePattern;
190 }
191
192 public File getBaseDir() {
193 return baseDir;
194 }
195
196 public ExecutionContext getExecutionContext() {
197 return executionContext;
198 }
199
200 public String getPrefixDatePattern() {
201 return prefixDatePattern;
202 }
203
204 /** Default is true. */
205 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
206 this.withExecutionSubdirectory = withExecutionSubdirectory;
207 }
208
209 }