]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
Move ReailizedFlow to execution package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / FileExecutionResources.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.core.execution;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.text.SimpleDateFormat;
24
25 import org.apache.commons.io.IOUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.SlcException;
29 import org.argeo.slc.execution.ExecutionContext;
30 import org.springframework.core.io.FileSystemResource;
31 import org.springframework.core.io.Resource;
32 import org.springframework.util.Assert;
33
34 /** Implements write access to resources based on standard Java {@link File} */
35 public class FileExecutionResources implements ExecutionResources {
36 private final static Log log = LogFactory
37 .getLog(FileExecutionResources.class);
38 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME = "executionResources";
39 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH = System
40 .getProperty("java.io.tmpdir")
41 + File.separator
42 + System.getProperty("user.name")
43 + File.separator
44 + "slc"
45 + File.separator + DEFAULT_EXECUTION_RESOURCES_DIRNAME;
46
47 private File baseDir;
48 private ExecutionContext executionContext;
49 private String prefixDatePattern = "yyyyMMdd_HHmmss_";
50 private SimpleDateFormat sdf = null;
51
52 private Boolean withExecutionSubdirectory = true;
53
54 public FileExecutionResources() {
55 // Default base directory
56 String osgiInstanceArea = System.getProperty("osgi.instance.area");
57 String osgiInstanceAreaDefault = System
58 .getProperty("osgi.instance.area.default");
59
60 if (osgiInstanceArea != null) {
61 // within OSGi with -data specified
62 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
63 baseDir = new File(osgiInstanceArea + File.separator
64 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
65 } else if (osgiInstanceAreaDefault != null) {
66 // within OSGi without -data specified
67 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
68 baseDir = new File(osgiInstanceAreaDefault + File.separator
69 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
70 } else {// outside OSGi
71 baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
72 }
73 }
74
75 protected SimpleDateFormat sdf() {
76 // Lazy init in case prefix has been externally set
77 if (sdf == null)
78 sdf = new SimpleDateFormat(prefixDatePattern);
79 return sdf;
80 }
81
82 public Resource getWritableResource(String relativePath) {
83 File file = getFile(relativePath);
84 File parentDir = file.getParentFile();
85
86 if (!parentDir.exists()) {
87 // Creates if necessary
88 if (log.isTraceEnabled())
89 log.trace("Creating parent directory " + parentDir);
90 parentDir.mkdirs();
91 }
92 Resource resource = new FileSystemResource(file);
93
94 if (log.isTraceEnabled())
95 log.trace("Returns writable resource " + resource);
96 return resource;
97 }
98
99 public String getWritableOsPath(String relativePath) {
100 try {
101 return getFile(relativePath).getCanonicalPath();
102 } catch (IOException e) {
103 throw new SlcException("Cannot find canonical path", e);
104 }
105 }
106
107 public File getWritableOsFile(String relativePath) {
108 return getFile(relativePath);
109 }
110
111 public String getAsOsPath(Resource resource, Boolean overwrite) {
112 File file = fileFromResource(resource);
113 if (file != null)
114 try {
115 if (log.isTraceEnabled())
116 log.debug("Directly interpret " + resource + " as OS file "
117 + file);
118 return file.getCanonicalPath();
119 } catch (IOException e1) {
120 // silent
121 }
122
123 if (log.isTraceEnabled())
124 log.trace("Resource " + resource
125 + " is not available on the file system. Retrieving it...");
126
127 InputStream in = null;
128 OutputStream out = null;
129 try {
130 String path = resource.getURL().getPath();
131 file = getFile(path);
132 if (file.exists() && !overwrite)
133 return file.getCanonicalPath();
134
135 file.getParentFile().mkdirs();
136 in = resource.getInputStream();
137 out = new FileOutputStream(file);
138 IOUtils.copy(in, out);
139 if (log.isDebugEnabled())
140 log.debug("Retrieved " + resource + " to OS file " + file);
141 return file.getCanonicalPath();
142 } catch (IOException e) {
143 throw new SlcException("Could not make resource " + resource
144 + " an OS file.", e);
145 } finally {
146 IOUtils.closeQuietly(in);
147 IOUtils.closeQuietly(out);
148 }
149 }
150
151 /**
152 * Extract the underlying file from the resource.
153 *
154 * @return the file or null if no files support this resource.
155 */
156 protected File fileFromResource(Resource resource) {
157 try {
158 return resource.getFile();
159 } catch (IOException e) {
160 return null;
161 }
162
163 }
164
165 protected File getFile(String relativePath) {
166 File writableBaseDir = getWritableBaseDir();
167 return new File(writableBaseDir.getPath() + File.separator
168 + relativePath.replace('/', File.separatorChar));
169 }
170
171 public File getWritableBaseDir() {
172 if (withExecutionSubdirectory) {
173 Assert.notNull(executionContext, "execution context is null");
174 String path = baseDir.getPath()
175 + File.separator
176 + sdf().format(
177 executionContext
178 .getVariable(ExecutionContext.VAR_EXECUTION_CONTEXT_CREATION_DATE))
179 + executionContext.getUuid();
180 return new File(path);
181 } else {
182 return baseDir;
183 }
184 }
185
186 protected String removeFilePrefix(String url) {
187 if (url.startsWith("file:"))
188 return url.substring("file:".length());
189 else if (url.startsWith("reference:file:"))
190 return url.substring("reference:file:".length());
191 else
192 return url;
193 }
194
195 public void setBaseDir(File baseDir) {
196 this.baseDir = baseDir;
197 }
198
199 public void setExecutionContext(ExecutionContext executionContext) {
200 this.executionContext = executionContext;
201 }
202
203 public void setPrefixDatePattern(String prefixDatePattern) {
204 this.prefixDatePattern = prefixDatePattern;
205 }
206
207 public File getBaseDir() {
208 return baseDir;
209 }
210
211 public ExecutionContext getExecutionContext() {
212 return executionContext;
213 }
214
215 public String getPrefixDatePattern() {
216 return prefixDatePattern;
217 }
218
219 /** Default is true. */
220 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
221 this.withExecutionSubdirectory = withExecutionSubdirectory;
222 }
223
224 }