]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/core/execution/FileExecutionResources.java
Prepare next development cacle
[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.argeo.api.cms.CmsLog;
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 /** Implements write access to resources based on standard Java {@link File} */
20 public class FileExecutionResources implements ExecutionResources {
21 private final static CmsLog log = CmsLog
22 .getLog(FileExecutionResources.class);
23 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME = "executionResources";
24 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH = System
25 .getProperty("java.io.tmpdir")
26 + File.separator
27 + System.getProperty("user.name")
28 + File.separator
29 + "slc"
30 + File.separator + DEFAULT_EXECUTION_RESOURCES_DIRNAME;
31
32 private File baseDir;
33 private ExecutionContext executionContext;
34 private String prefixDatePattern = "yyMMdd_HHmmss_SSS";
35 private SimpleDateFormat sdf = null;
36
37 private Boolean withExecutionSubdirectory = true;
38
39 public FileExecutionResources() {
40 // Default base directory
41 String osgiInstanceArea = System.getProperty("osgi.instance.area");
42 String osgiInstanceAreaDefault = System
43 .getProperty("osgi.instance.area.default");
44
45 if (osgiInstanceArea != null) {
46 // within OSGi with -data specified
47 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
48 baseDir = new File(osgiInstanceArea + File.separator
49 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
50 } else if (osgiInstanceAreaDefault != null) {
51 // within OSGi without -data specified
52 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
53 baseDir = new File(osgiInstanceAreaDefault + File.separator
54 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
55 } else {// outside OSGi
56 baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
57 }
58 }
59
60 protected SimpleDateFormat sdf() {
61 // Lazy init in case prefix has been externally set
62 if (sdf == null)
63 sdf = new SimpleDateFormat(prefixDatePattern);
64 return sdf;
65 }
66
67 public Resource getWritableResource(String relativePath) {
68 File file = getFile(relativePath);
69 File parentDir = file.getParentFile();
70
71 if (!parentDir.exists()) {
72 // Creates if necessary
73 if (log.isTraceEnabled())
74 log.trace("Creating parent directory " + parentDir);
75 parentDir.mkdirs();
76 }
77 Resource resource = new FileSystemResource(file);
78
79 if (log.isTraceEnabled())
80 log.trace("Returns writable resource " + resource);
81 return resource;
82 }
83
84 public String getWritableOsPath(String relativePath) {
85 try {
86 return getFile(relativePath).getCanonicalPath();
87 } catch (IOException e) {
88 throw new SlcException("Cannot find canonical path", e);
89 }
90 }
91
92 public File getWritableOsFile(String relativePath) {
93 return getFile(relativePath);
94 }
95
96 public String getAsOsPath(Resource resource, Boolean overwrite) {
97 File file = fileFromResource(resource);
98 if (file != null)
99 try {
100 if (log.isTraceEnabled())
101 log.debug("Directly interpret " + resource + " as OS file "
102 + file);
103 return file.getCanonicalPath();
104 } catch (IOException e1) {
105 // silent
106 }
107
108 if (log.isTraceEnabled())
109 log.trace("Resource " + resource
110 + " is not available on the file system. Retrieving it...");
111
112 InputStream in = null;
113 OutputStream out = null;
114 try {
115 String path = resource.getURL().getPath();
116 file = getFile(path);
117 if (file.exists() && !overwrite)
118 return file.getCanonicalPath();
119
120 file.getParentFile().mkdirs();
121 in = resource.getInputStream();
122 out = new FileOutputStream(file);
123 IOUtils.copy(in, out);
124 if (log.isDebugEnabled())
125 log.debug("Retrieved " + resource + " to OS file " + file);
126 return file.getCanonicalPath();
127 } catch (IOException e) {
128 throw new SlcException("Could not make resource " + resource
129 + " an OS file.", e);
130 } finally {
131 IOUtils.closeQuietly(in);
132 IOUtils.closeQuietly(out);
133 }
134 }
135
136 /**
137 * Extract the underlying file from the resource.
138 *
139 * @return the file or null if no files support this resource.
140 */
141 protected File fileFromResource(Resource resource) {
142 try {
143 return resource.getFile();
144 } catch (IOException e) {
145 return null;
146 }
147
148 }
149
150 protected File getFile(String relativePath) {
151 File writableBaseDir = getWritableBaseDir();
152 return new File(writableBaseDir.getPath() + File.separator
153 + relativePath.replace('/', File.separatorChar));
154 }
155
156 public File getWritableBaseDir() {
157 if (withExecutionSubdirectory) {
158 Date executionContextCreationDate = (Date) executionContext
159 .getVariable(ExecutionContext.VAR_EXECUTION_CONTEXT_CREATION_DATE);
160 Assert.notNull(executionContext, "execution context is null");
161 String path = baseDir.getPath() + File.separator
162 + sdf().format(executionContextCreationDate);
163 // TODO write execution id somewhere? like in a txt file
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 }