2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.execution
;
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
;
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
;
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")
44 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
;
47 private ExecutionContext executionContext
;
48 private String prefixDatePattern
= "yyyyMMdd_HHmmss_";
49 private SimpleDateFormat sdf
= null;
51 private Boolean withExecutionSubdirectory
= true;
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");
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
);
74 protected SimpleDateFormat
sdf() {
75 // Lazy init in case prefix has been externally set
77 sdf
= new SimpleDateFormat(prefixDatePattern
);
81 public Resource
getWritableResource(String relativePath
) {
82 File file
= getFile(relativePath
);
83 File parentDir
= file
.getParentFile();
85 if (!parentDir
.exists()) {
86 // Creates if necessary
87 if (log
.isTraceEnabled())
88 log
.trace("Creating parent directory " + parentDir
);
91 Resource resource
= new FileSystemResource(file
);
93 if (log
.isTraceEnabled())
94 log
.trace("Returns writable resource " + resource
);
98 public String
getWritableOsPath(String relativePath
) {
100 return getFile(relativePath
).getCanonicalPath();
101 } catch (IOException e
) {
102 throw new SlcException("Cannot find canonical path", e
);
106 public File
getWritableOsFile(String relativePath
) {
107 return getFile(relativePath
);
110 public String
getAsOsPath(Resource resource
, Boolean overwrite
) {
111 File file
= fileFromResource(resource
);
114 if (log
.isTraceEnabled())
115 log
.debug("Directly interpret " + resource
+ " as OS file "
117 return file
.getCanonicalPath();
118 } catch (IOException e1
) {
122 if (log
.isTraceEnabled())
123 log
.trace("Resource " + resource
124 + " is not available on the file system. Retrieving it...");
126 InputStream in
= null;
127 OutputStream out
= null;
129 String path
= resource
.getURL().getPath();
130 file
= getFile(path
);
131 if (file
.exists() && !overwrite
)
132 return file
.getCanonicalPath();
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
);
145 IOUtils
.closeQuietly(in
);
146 IOUtils
.closeQuietly(out
);
151 * Extract the underlying file from the resource.
153 * @return the file or null if no files support this resource.
155 protected File
fileFromResource(Resource resource
) {
157 return resource
.getFile();
158 } catch (IOException e
) {
164 protected File
getFile(String relativePath
) {
165 File writableBaseDir
= getWritableBaseDir();
166 return new File(writableBaseDir
.getPath() + File
.separator
167 + relativePath
.replace('/', File
.separatorChar
));
170 public File
getWritableBaseDir() {
171 if (withExecutionSubdirectory
) {
172 Assert
.notNull(executionContext
, "execution context is null");
173 String path
= baseDir
.getPath()
178 .getVariable(ExecutionContext
.VAR_EXECUTION_CONTEXT_CREATION_DATE
))
179 + executionContext
.getUuid();
180 return new File(path
);
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());
195 public void setBaseDir(File baseDir
) {
196 this.baseDir
= baseDir
;
199 public void setExecutionContext(ExecutionContext executionContext
) {
200 this.executionContext
= executionContext
;
203 public void setPrefixDatePattern(String prefixDatePattern
) {
204 this.prefixDatePattern
= prefixDatePattern
;
207 public File
getBaseDir() {
211 public ExecutionContext
getExecutionContext() {
212 return executionContext
;
215 public String
getPrefixDatePattern() {
216 return prefixDatePattern
;
219 /** Default is true. */
220 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory
) {
221 this.withExecutionSubdirectory
= withExecutionSubdirectory
;