]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
ExcecutionScopeDecorator: change default to proxy interfaces
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / 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
43 if (osgiInstanceArea != null) {
44 // within OSGi with -data specified
45 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
46 baseDir = new File(osgiInstanceArea + File.separator
47 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
48 } else if (osgiInstanceAreaDefault != null) {
49 // within OSGi without -data specified
50 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
51 baseDir = new File(osgiInstanceAreaDefault + File.separator
52 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
53 } else {// outside OSGi
54 baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
55 }
56 }
57
58 protected SimpleDateFormat sdf() {
59 // Lazy init in case prefix has been externally set
60 if (sdf == null)
61 sdf = new SimpleDateFormat(prefixDatePattern);
62 return sdf;
63 }
64
65 public Resource getWritableResource(String relativePath) {
66 File file = getFile(relativePath);
67 File parentDir = file.getParentFile();
68
69 if (!parentDir.exists()) {
70 // Creates if necessary
71 if (log.isTraceEnabled())
72 log.trace("Creating parent directory " + parentDir);
73 parentDir.mkdirs();
74 }
75 Resource resource = new FileSystemResource(file);
76
77 if (log.isTraceEnabled())
78 log.trace("Returns writable resource " + resource);
79 return resource;
80 }
81
82 public String getWritableOsPath(String relativePath) {
83 try {
84 return getFile(relativePath).getCanonicalPath();
85 } catch (IOException e) {
86 throw new SlcException("Cannot find canonical path", e);
87 }
88 }
89
90 public File getWritableOsFile(String relativePath) {
91 return getFile(relativePath);
92 }
93
94 public String getAsOsPath(Resource resource, Boolean overwrite) {
95 File file = fileFromResource(resource);
96 if (file != null)
97 try {
98 if (log.isTraceEnabled())
99 log.debug("Directly interpret " + resource + " as OS file "
100 + file);
101 return file.getCanonicalPath();
102 } catch (IOException e1) {
103 // silent
104 }
105
106 if (log.isTraceEnabled())
107 log.trace("Resource " + resource
108 + " is not available on the file system. Retrieving it...");
109
110 InputStream in = null;
111 OutputStream out = null;
112 try {
113 String path = resource.getURL().getPath();
114 file = getFile(path);
115 if (file.exists() && !overwrite)
116 return file.getCanonicalPath();
117
118 file.getParentFile().mkdirs();
119 in = resource.getInputStream();
120 out = new FileOutputStream(file);
121 IOUtils.copy(in, out);
122 if (log.isDebugEnabled())
123 log.debug("Retrieved " + resource + " to OS file " + file);
124 return file.getCanonicalPath();
125 } catch (IOException e) {
126 throw new SlcException("Could not make resource " + resource
127 + " an OS file.", e);
128 } finally {
129 IOUtils.closeQuietly(in);
130 IOUtils.closeQuietly(out);
131 }
132 }
133
134 /**
135 * Extract the underlying file from the resource.
136 *
137 * @return the file or null if no files support this resource.
138 */
139 protected File fileFromResource(Resource resource) {
140 try {
141 return resource.getFile();
142 } catch (IOException e) {
143 return null;
144 }
145
146 }
147
148 protected File getFile(String relativePath) {
149 File writableBaseDir = getWritableBaseDir();
150 return new File(writableBaseDir.getPath() + File.separator
151 + relativePath.replace('/', File.separatorChar));
152 }
153
154 public File getWritableBaseDir() {
155 if (withExecutionSubdirectory) {
156 Assert.notNull(executionContext, "execution context is null");
157 String path = baseDir.getPath()
158 + File.separator
159 + sdf()
160 .format(
161 executionContext
162 .getVariable(ExecutionContext.VAR_EXECUTION_CONTEXT_CREATION_DATE))
163 + executionContext.getUuid();
164 return new File(path);
165 } else {
166 return baseDir;
167 }
168 }
169
170 protected String removeFilePrefix(String url) {
171 if (url.startsWith("file:"))
172 return url.substring("file:".length());
173 else if (url.startsWith("reference:file:"))
174 return url.substring("reference:file:".length());
175 else
176 return url;
177 }
178
179 public void setBaseDir(File baseDir) {
180 this.baseDir = baseDir;
181 }
182
183 public void setExecutionContext(ExecutionContext executionContext) {
184 this.executionContext = executionContext;
185 }
186
187 public void setPrefixDatePattern(String prefixDatePattern) {
188 this.prefixDatePattern = prefixDatePattern;
189 }
190
191 public File getBaseDir() {
192 return baseDir;
193 }
194
195 public ExecutionContext getExecutionContext() {
196 return executionContext;
197 }
198
199 public String getPrefixDatePattern() {
200 return prefixDatePattern;
201 }
202
203 /** Default is true. */
204 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
205 this.withExecutionSubdirectory = withExecutionSubdirectory;
206 }
207
208 }