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");
42 String tempDir
= System
.getProperty("java.io.tmpdir");
44 if (osgiInstanceArea
!= null) {
45 // within OSGi with -data specified
46 osgiInstanceArea
= removeFilePrefix(osgiInstanceArea
);
47 baseDir
= new File(osgiInstanceArea
+ File
.separator
48 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
49 } else if (osgiInstanceAreaDefault
!= null) {
50 // within OSGi without -data specified
51 osgiInstanceAreaDefault
= removeFilePrefix(osgiInstanceAreaDefault
);
52 baseDir
= new File(osgiInstanceAreaDefault
+ File
.separator
53 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
54 } else {// outside OSGi
55 baseDir
= new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH
);
59 protected SimpleDateFormat
sdf() {
60 // Lazy init in case prefix has been externally set
62 sdf
= new SimpleDateFormat(prefixDatePattern
);
66 public Resource
getWritableResource(String relativePath
) {
67 File file
= getFile(relativePath
);
68 File parentDir
= file
.getParentFile();
70 if (!parentDir
.exists()) {
71 // Creates if necessary
72 if (log
.isTraceEnabled())
73 log
.trace("Creating parent directory " + parentDir
);
76 Resource resource
= new FileSystemResource(file
);
78 if (log
.isTraceEnabled())
79 log
.trace("Returns writable resource " + resource
);
83 public String
getAsOsPath(Resource resource
, Boolean overwrite
) {
86 file
= resource
.getFile();
87 return file
.getCanonicalPath();
88 } catch (IOException e
) {
89 if (log
.isTraceEnabled())
93 + " is not available on the file system. Retrieving it...");
96 InputStream in
= null;
97 OutputStream out
= null;
99 String path
= resource
.getURL().getPath();
100 file
= getFile(path
);
101 if (file
.exists() && !overwrite
)
102 return file
.getCanonicalPath();
104 file
.getParentFile().mkdirs();
105 in
= resource
.getInputStream();
106 out
= new FileOutputStream(file
);
107 IOUtils
.copy(in
, out
);
108 if (log
.isDebugEnabled())
109 log
.debug("Retrieved " + resource
+ " to OS file " + file
);
110 return file
.getCanonicalPath();
111 } catch (IOException e
) {
112 throw new SlcException("Could not make resource " + resource
113 + " an OS file.", e
);
115 IOUtils
.closeQuietly(in
);
116 IOUtils
.closeQuietly(out
);
120 public File
getFile(String relativePath
) {
121 Assert
.notNull(executionContext
, "execution context is null");
123 if (withExecutionSubdirectory
) {
124 String path
= baseDir
.getPath() + File
.separator
125 + sdf().format(executionContext
.getCreationDate())
126 + executionContext
.getUuid();
127 File executionDir
= new File(path
);
129 return new File(executionDir
.getPath() + File
.separator
130 + relativePath
.replace('/', File
.separatorChar
));
132 return new File(baseDir
.getPath() + File
.separator
133 + relativePath
.replace('/', File
.separatorChar
));
137 protected String
removeFilePrefix(String url
) {
138 if (url
.startsWith("file:"))
139 return url
.substring("file:".length());
140 else if (url
.startsWith("reference:file:"))
141 return url
.substring("reference:file:".length());
146 public void setBaseDir(File baseDir
) {
147 this.baseDir
= baseDir
;
150 public void setExecutionContext(ExecutionContext executionContext
) {
151 this.executionContext
= executionContext
;
154 public void setPrefixDatePattern(String prefixDatePattern
) {
155 this.prefixDatePattern
= prefixDatePattern
;
158 public File
getBaseDir() {
162 public ExecutionContext
getExecutionContext() {
163 return executionContext
;
166 public String
getPrefixDatePattern() {
167 return prefixDatePattern
;
170 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory
) {
171 this.withExecutionSubdirectory
= withExecutionSubdirectory
;