1 package org
.argeo
.slc
.core
.execution
;
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
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.execution
.ExecutionContext
;
16 import org
.springframework
.core
.io
.FileSystemResource
;
17 import org
.springframework
.core
.io
.Resource
;
18 import org
.springframework
.util
.Assert
;
20 /** Implements write access to resources based on standard Java {@link File} */
21 public class FileExecutionResources
implements ExecutionResources
{
22 private final static Log log
= LogFactory
23 .getLog(FileExecutionResources
.class);
24 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME
= "executionResources";
25 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH
= System
26 .getProperty("java.io.tmpdir")
28 + System
.getProperty("user.name")
31 + File
.separator
+ DEFAULT_EXECUTION_RESOURCES_DIRNAME
;
34 private ExecutionContext executionContext
;
35 private String prefixDatePattern
= "yyMMdd_HHmmss_SSS";
36 private SimpleDateFormat sdf
= null;
38 private Boolean withExecutionSubdirectory
= true;
40 public FileExecutionResources() {
41 // Default base directory
42 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
43 String osgiInstanceAreaDefault
= System
44 .getProperty("osgi.instance.area.default");
46 if (osgiInstanceArea
!= null) {
47 // within OSGi with -data specified
48 osgiInstanceArea
= removeFilePrefix(osgiInstanceArea
);
49 baseDir
= new File(osgiInstanceArea
+ File
.separator
50 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
51 } else if (osgiInstanceAreaDefault
!= null) {
52 // within OSGi without -data specified
53 osgiInstanceAreaDefault
= removeFilePrefix(osgiInstanceAreaDefault
);
54 baseDir
= new File(osgiInstanceAreaDefault
+ File
.separator
55 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
56 } else {// outside OSGi
57 baseDir
= new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH
);
61 protected SimpleDateFormat
sdf() {
62 // Lazy init in case prefix has been externally set
64 sdf
= new SimpleDateFormat(prefixDatePattern
);
68 public Resource
getWritableResource(String relativePath
) {
69 File file
= getFile(relativePath
);
70 File parentDir
= file
.getParentFile();
72 if (!parentDir
.exists()) {
73 // Creates if necessary
74 if (log
.isTraceEnabled())
75 log
.trace("Creating parent directory " + parentDir
);
78 Resource resource
= new FileSystemResource(file
);
80 if (log
.isTraceEnabled())
81 log
.trace("Returns writable resource " + resource
);
85 public String
getWritableOsPath(String relativePath
) {
87 return getFile(relativePath
).getCanonicalPath();
88 } catch (IOException e
) {
89 throw new SlcException("Cannot find canonical path", e
);
93 public File
getWritableOsFile(String relativePath
) {
94 return getFile(relativePath
);
97 public String
getAsOsPath(Resource resource
, Boolean overwrite
) {
98 File file
= fileFromResource(resource
);
101 if (log
.isTraceEnabled())
102 log
.debug("Directly interpret " + resource
+ " as OS file "
104 return file
.getCanonicalPath();
105 } catch (IOException e1
) {
109 if (log
.isTraceEnabled())
110 log
.trace("Resource " + resource
111 + " is not available on the file system. Retrieving it...");
113 InputStream in
= null;
114 OutputStream out
= null;
116 String path
= resource
.getURL().getPath();
117 file
= getFile(path
);
118 if (file
.exists() && !overwrite
)
119 return file
.getCanonicalPath();
121 file
.getParentFile().mkdirs();
122 in
= resource
.getInputStream();
123 out
= new FileOutputStream(file
);
124 IOUtils
.copy(in
, out
);
125 if (log
.isDebugEnabled())
126 log
.debug("Retrieved " + resource
+ " to OS file " + file
);
127 return file
.getCanonicalPath();
128 } catch (IOException e
) {
129 throw new SlcException("Could not make resource " + resource
130 + " an OS file.", e
);
132 IOUtils
.closeQuietly(in
);
133 IOUtils
.closeQuietly(out
);
138 * Extract the underlying file from the resource.
140 * @return the file or null if no files support this resource.
142 protected File
fileFromResource(Resource resource
) {
144 return resource
.getFile();
145 } catch (IOException e
) {
151 protected File
getFile(String relativePath
) {
152 File writableBaseDir
= getWritableBaseDir();
153 return new File(writableBaseDir
.getPath() + File
.separator
154 + relativePath
.replace('/', File
.separatorChar
));
157 public File
getWritableBaseDir() {
158 if (withExecutionSubdirectory
) {
159 Date executionContextCreationDate
= (Date
) executionContext
160 .getVariable(ExecutionContext
.VAR_EXECUTION_CONTEXT_CREATION_DATE
);
161 Assert
.notNull(executionContext
, "execution context is null");
162 String path
= baseDir
.getPath() + File
.separator
163 + sdf().format(executionContextCreationDate
);
164 // TODO write execution id somewhere? like in a txt file
165 return new File(path
);
171 protected String
removeFilePrefix(String url
) {
172 if (url
.startsWith("file:"))
173 return url
.substring("file:".length());
174 else if (url
.startsWith("reference:file:"))
175 return url
.substring("reference:file:".length());
180 public void setBaseDir(File baseDir
) {
181 this.baseDir
= baseDir
;
184 public void setExecutionContext(ExecutionContext executionContext
) {
185 this.executionContext
= executionContext
;
188 public void setPrefixDatePattern(String prefixDatePattern
) {
189 this.prefixDatePattern
= prefixDatePattern
;
192 public File
getBaseDir() {
196 public ExecutionContext
getExecutionContext() {
197 return executionContext
;
200 public String
getPrefixDatePattern() {
201 return prefixDatePattern
;
204 /** Default is true. */
205 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory
) {
206 this.withExecutionSubdirectory
= withExecutionSubdirectory
;