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
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
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
;
19 public class FileExecutionResources
implements ExecutionResources
{
20 private final static Log log
= LogFactory
21 .getLog(FileExecutionResources
.class);
22 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME
= "executionResources";
23 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH
= System
24 .getProperty("java.io.tmpdir")
28 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
;
31 private ExecutionContext executionContext
;
32 private String prefixDatePattern
= "yyyyMMdd_HHmmss_";
33 private SimpleDateFormat sdf
= null;
35 private Boolean withExecutionSubdirectory
= true;
37 public FileExecutionResources() {
38 // Default base directory
39 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
40 String osgiInstanceAreaDefault
= System
41 .getProperty("osgi.instance.area.default");
43 if (osgiInstanceArea
!= null) {
44 // within OSGi with -data specified
45 osgiInstanceArea
= removeFilePrefix(osgiInstanceArea
);
46 baseDir
= new File(osgiInstanceArea
+ File
.separator
47 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
48 } else if (osgiInstanceAreaDefault
!= null) {
49 // within OSGi without -data specified
50 osgiInstanceAreaDefault
= removeFilePrefix(osgiInstanceAreaDefault
);
51 baseDir
= new File(osgiInstanceAreaDefault
+ File
.separator
52 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
53 } else {// outside OSGi
54 baseDir
= new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH
);
58 protected SimpleDateFormat
sdf() {
59 // Lazy init in case prefix has been externally set
61 sdf
= new SimpleDateFormat(prefixDatePattern
);
65 public Resource
getWritableResource(String relativePath
) {
66 File file
= getFile(relativePath
);
67 File parentDir
= file
.getParentFile();
69 if (!parentDir
.exists()) {
70 // Creates if necessary
71 if (log
.isTraceEnabled())
72 log
.trace("Creating parent directory " + parentDir
);
75 Resource resource
= new FileSystemResource(file
);
77 if (log
.isTraceEnabled())
78 log
.trace("Returns writable resource " + resource
);
82 public String
getAsOsPath(Resource resource
, Boolean overwrite
) {
83 File file
= fileFromResource(resource
);
86 if (log
.isTraceEnabled())
87 log
.debug("Directly interpret " + resource
+ " as OS file "
89 return file
.getCanonicalPath();
90 } catch (IOException e1
) {
94 if (log
.isTraceEnabled())
95 log
.trace("Resource " + resource
96 + " is not available on the file system. Retrieving it...");
98 InputStream in
= null;
99 OutputStream out
= null;
101 String path
= resource
.getURL().getPath();
102 file
= getFile(path
);
103 if (file
.exists() && !overwrite
)
104 return file
.getCanonicalPath();
106 file
.getParentFile().mkdirs();
107 in
= resource
.getInputStream();
108 out
= new FileOutputStream(file
);
109 IOUtils
.copy(in
, out
);
110 if (log
.isDebugEnabled())
111 log
.debug("Retrieved " + resource
+ " to OS file " + file
);
112 return file
.getCanonicalPath();
113 } catch (IOException e
) {
114 throw new SlcException("Could not make resource " + resource
115 + " an OS file.", e
);
117 IOUtils
.closeQuietly(in
);
118 IOUtils
.closeQuietly(out
);
123 * Extract the underlying file from the resource.
125 * @return the file or null if no files support this resource.
127 protected File
fileFromResource(Resource resource
) {
129 return resource
.getFile();
130 } catch (IOException e
) {
136 public File
getFile(String relativePath
) {
138 if (withExecutionSubdirectory
) {
139 Assert
.notNull(executionContext
, "execution context is null");
140 String path
= baseDir
.getPath()
145 .getVariable(ExecutionContext
.VAR_EXECUTION_CONTEXT_CREATION_DATE
))
146 + executionContext
.getUuid();
147 File executionDir
= new File(path
);
149 return new File(executionDir
.getPath() + File
.separator
150 + relativePath
.replace('/', File
.separatorChar
));
152 return new File(baseDir
.getPath() + File
.separator
153 + relativePath
.replace('/', File
.separatorChar
));
157 protected String
removeFilePrefix(String url
) {
158 if (url
.startsWith("file:"))
159 return url
.substring("file:".length());
160 else if (url
.startsWith("reference:file:"))
161 return url
.substring("reference:file:".length());
166 public void setBaseDir(File baseDir
) {
167 this.baseDir
= baseDir
;
170 public void setExecutionContext(ExecutionContext executionContext
) {
171 this.executionContext
= executionContext
;
174 public void setPrefixDatePattern(String prefixDatePattern
) {
175 this.prefixDatePattern
= prefixDatePattern
;
178 public File
getBaseDir() {
182 public ExecutionContext
getExecutionContext() {
183 return executionContext
;
186 public String
getPrefixDatePattern() {
187 return prefixDatePattern
;
190 /** Default is true. */
191 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory
) {
192 this.withExecutionSubdirectory
= withExecutionSubdirectory
;