]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / FileExecutionResources.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.core.execution;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.text.SimpleDateFormat;
25
26 import org.apache.commons.io.IOUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.argeo.slc.SlcException;
30 import org.argeo.slc.execution.ExecutionContext;
31 import org.springframework.core.io.FileSystemResource;
32 import org.springframework.core.io.Resource;
33 import org.springframework.util.Assert;
34
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 + "slc"
43 + File.separator
44 + DEFAULT_EXECUTION_RESOURCES_DIRNAME;
45
46 private File baseDir;
47 private ExecutionContext executionContext;
48 private String prefixDatePattern = "yyyyMMdd_HHmmss_";
49 private SimpleDateFormat sdf = null;
50
51 private Boolean withExecutionSubdirectory = true;
52
53 public FileExecutionResources() {
54 // Default base directory
55 String osgiInstanceArea = System.getProperty("osgi.instance.area");
56 String osgiInstanceAreaDefault = System
57 .getProperty("osgi.instance.area.default");
58
59 if (osgiInstanceArea != null) {
60 // within OSGi with -data specified
61 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
62 baseDir = new File(osgiInstanceArea + File.separator
63 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
64 } else if (osgiInstanceAreaDefault != null) {
65 // within OSGi without -data specified
66 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
67 baseDir = new File(osgiInstanceAreaDefault + File.separator
68 + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
69 } else {// outside OSGi
70 baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
71 }
72 }
73
74 protected SimpleDateFormat sdf() {
75 // Lazy init in case prefix has been externally set
76 if (sdf == null)
77 sdf = new SimpleDateFormat(prefixDatePattern);
78 return sdf;
79 }
80
81 public Resource getWritableResource(String relativePath) {
82 File file = getFile(relativePath);
83 File parentDir = file.getParentFile();
84
85 if (!parentDir.exists()) {
86 // Creates if necessary
87 if (log.isTraceEnabled())
88 log.trace("Creating parent directory " + parentDir);
89 parentDir.mkdirs();
90 }
91 Resource resource = new FileSystemResource(file);
92
93 if (log.isTraceEnabled())
94 log.trace("Returns writable resource " + resource);
95 return resource;
96 }
97
98 public String getWritableOsPath(String relativePath) {
99 try {
100 return getFile(relativePath).getCanonicalPath();
101 } catch (IOException e) {
102 throw new SlcException("Cannot find canonical path", e);
103 }
104 }
105
106 public File getWritableOsFile(String relativePath) {
107 return getFile(relativePath);
108 }
109
110 public String getAsOsPath(Resource resource, Boolean overwrite) {
111 File file = fileFromResource(resource);
112 if (file != null)
113 try {
114 if (log.isTraceEnabled())
115 log.debug("Directly interpret " + resource + " as OS file "
116 + file);
117 return file.getCanonicalPath();
118 } catch (IOException e1) {
119 // silent
120 }
121
122 if (log.isTraceEnabled())
123 log.trace("Resource " + resource
124 + " is not available on the file system. Retrieving it...");
125
126 InputStream in = null;
127 OutputStream out = null;
128 try {
129 String path = resource.getURL().getPath();
130 file = getFile(path);
131 if (file.exists() && !overwrite)
132 return file.getCanonicalPath();
133
134 file.getParentFile().mkdirs();
135 in = resource.getInputStream();
136 out = new FileOutputStream(file);
137 IOUtils.copy(in, out);
138 if (log.isDebugEnabled())
139 log.debug("Retrieved " + resource + " to OS file " + file);
140 return file.getCanonicalPath();
141 } catch (IOException e) {
142 throw new SlcException("Could not make resource " + resource
143 + " an OS file.", e);
144 } finally {
145 IOUtils.closeQuietly(in);
146 IOUtils.closeQuietly(out);
147 }
148 }
149
150 /**
151 * Extract the underlying file from the resource.
152 *
153 * @return the file or null if no files support this resource.
154 */
155 protected File fileFromResource(Resource resource) {
156 try {
157 return resource.getFile();
158 } catch (IOException e) {
159 return null;
160 }
161
162 }
163
164 protected File getFile(String relativePath) {
165 File writableBaseDir = getWritableBaseDir();
166 return new File(writableBaseDir.getPath() + File.separator
167 + relativePath.replace('/', File.separatorChar));
168 }
169
170 public File getWritableBaseDir() {
171 if (withExecutionSubdirectory) {
172 Assert.notNull(executionContext, "execution context is null");
173 String path = baseDir.getPath()
174 + File.separator
175 + sdf()
176 .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 }