1 package org
.argeo
.slc
.core
.execution
;
4 import java
.text
.SimpleDateFormat
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.slc
.execution
.ExecutionContext
;
9 import org
.springframework
.core
.io
.FileSystemResource
;
10 import org
.springframework
.core
.io
.Resource
;
11 import org
.springframework
.util
.Assert
;
13 public class FileExecutionResources
implements ExecutionResources
{
14 private final static Log log
= LogFactory
15 .getLog(FileExecutionResources
.class);
16 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME
= "executionResources";
19 private ExecutionContext executionContext
;
20 private String prefixDatePattern
= "yyyyMMdd_HHmmss_";
21 private SimpleDateFormat sdf
= null;
23 public FileExecutionResources() {
24 // Default base directory
25 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
26 String osgiInstanceAreaDefault
= System
27 .getProperty("osgi.instance.area.default");
28 String tempDir
= System
.getProperty("java.io.tmpdir");
30 if (osgiInstanceArea
!= null) {
31 // within OSGi with -data specified
32 osgiInstanceArea
= removeFilePrefix(osgiInstanceArea
);
33 baseDir
= new File(osgiInstanceArea
+ File
.separator
34 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
35 } else if (osgiInstanceAreaDefault
!= null) {
36 // within OSGi without -data specified
37 osgiInstanceAreaDefault
= removeFilePrefix(osgiInstanceAreaDefault
);
38 baseDir
= new File(osgiInstanceAreaDefault
+ File
.separator
39 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
40 } else {// outside OSGi
41 baseDir
= new File(tempDir
+ File
.separator
+ "slc"
42 + File
.separator
+ DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
46 protected SimpleDateFormat
sdf() {
47 // Lazy init in case prefix has been externally set
49 sdf
= new SimpleDateFormat(prefixDatePattern
);
53 public Resource
getWritableResource(String relativePath
) {
54 File file
= getFile(relativePath
);
55 File parentDir
= file
.getParentFile();
57 if (!parentDir
.exists()) {
58 // Creates if necessary
59 if (log
.isTraceEnabled())
60 log
.trace("Creating parent directory " + parentDir
);
63 Resource resource
= new FileSystemResource(file
);
64 if (log
.isTraceEnabled())
65 log
.trace("Returns writable resource " + resource
);
69 public File
getFile(String relativePath
) {
70 Assert
.notNull(executionContext
, "execution context is null");
72 String path
= baseDir
.getPath() + File
.separator
73 + sdf().format(executionContext
.getCreationDate())
74 + executionContext
.getUuid();
75 File executionDir
= new File(path
);
77 return new File(executionDir
.getPath() + File
.separator
78 + relativePath
.replace('/', File
.separatorChar
));
81 protected String
removeFilePrefix(String url
) {
82 if (url
.startsWith("file:"))
83 return url
.substring("file:".length());
84 else if (url
.startsWith("reference:file:"))
85 return url
.substring("reference:file:".length());
90 public void setBaseDir(File baseDir
) {
91 this.baseDir
= baseDir
;
94 public void setExecutionContext(ExecutionContext executionContext
) {
95 this.executionContext
= executionContext
;
98 public void setPrefixDatePattern(String prefixDatePattern
) {
99 this.prefixDatePattern
= prefixDatePattern
;
102 public File
getBaseDir() {
106 public ExecutionContext
getExecutionContext() {
107 return executionContext
;
110 public String
getPrefixDatePattern() {
111 return prefixDatePattern
;