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
.argeo
.api
.cms
.CmsLog
;
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 /** Implements write access to resources based on standard Java {@link File} */
20 public class FileExecutionResources
implements ExecutionResources
{
21 private final static CmsLog log
= CmsLog
22 .getLog(FileExecutionResources
.class);
23 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME
= "executionResources";
24 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH
= System
25 .getProperty("java.io.tmpdir")
27 + System
.getProperty("user.name")
30 + File
.separator
+ DEFAULT_EXECUTION_RESOURCES_DIRNAME
;
33 private ExecutionContext executionContext
;
34 private String prefixDatePattern
= "yyMMdd_HHmmss_SSS";
35 private SimpleDateFormat sdf
= null;
37 private Boolean withExecutionSubdirectory
= true;
39 public FileExecutionResources() {
40 // Default base directory
41 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
42 String osgiInstanceAreaDefault
= System
43 .getProperty("osgi.instance.area.default");
45 if (osgiInstanceArea
!= null) {
46 // within OSGi with -data specified
47 osgiInstanceArea
= removeFilePrefix(osgiInstanceArea
);
48 baseDir
= new File(osgiInstanceArea
+ File
.separator
49 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
50 } else if (osgiInstanceAreaDefault
!= null) {
51 // within OSGi without -data specified
52 osgiInstanceAreaDefault
= removeFilePrefix(osgiInstanceAreaDefault
);
53 baseDir
= new File(osgiInstanceAreaDefault
+ File
.separator
54 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
55 } else {// outside OSGi
56 baseDir
= new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH
);
60 protected SimpleDateFormat
sdf() {
61 // Lazy init in case prefix has been externally set
63 sdf
= new SimpleDateFormat(prefixDatePattern
);
67 public Resource
getWritableResource(String relativePath
) {
68 File file
= getFile(relativePath
);
69 File parentDir
= file
.getParentFile();
71 if (!parentDir
.exists()) {
72 // Creates if necessary
73 if (log
.isTraceEnabled())
74 log
.trace("Creating parent directory " + parentDir
);
77 Resource resource
= new FileSystemResource(file
);
79 if (log
.isTraceEnabled())
80 log
.trace("Returns writable resource " + resource
);
84 public String
getWritableOsPath(String relativePath
) {
86 return getFile(relativePath
).getCanonicalPath();
87 } catch (IOException e
) {
88 throw new SlcException("Cannot find canonical path", e
);
92 public File
getWritableOsFile(String relativePath
) {
93 return getFile(relativePath
);
96 public String
getAsOsPath(Resource resource
, Boolean overwrite
) {
97 File file
= fileFromResource(resource
);
100 if (log
.isTraceEnabled())
101 log
.debug("Directly interpret " + resource
+ " as OS file "
103 return file
.getCanonicalPath();
104 } catch (IOException e1
) {
108 if (log
.isTraceEnabled())
109 log
.trace("Resource " + resource
110 + " is not available on the file system. Retrieving it...");
112 InputStream in
= null;
113 OutputStream out
= null;
115 String path
= resource
.getURL().getPath();
116 file
= getFile(path
);
117 if (file
.exists() && !overwrite
)
118 return file
.getCanonicalPath();
120 file
.getParentFile().mkdirs();
121 in
= resource
.getInputStream();
122 out
= new FileOutputStream(file
);
123 IOUtils
.copy(in
, out
);
124 if (log
.isDebugEnabled())
125 log
.debug("Retrieved " + resource
+ " to OS file " + file
);
126 return file
.getCanonicalPath();
127 } catch (IOException e
) {
128 throw new SlcException("Could not make resource " + resource
129 + " an OS file.", e
);
131 IOUtils
.closeQuietly(in
);
132 IOUtils
.closeQuietly(out
);
137 * Extract the underlying file from the resource.
139 * @return the file or null if no files support this resource.
141 protected File
fileFromResource(Resource resource
) {
143 return resource
.getFile();
144 } catch (IOException e
) {
150 protected File
getFile(String relativePath
) {
151 File writableBaseDir
= getWritableBaseDir();
152 return new File(writableBaseDir
.getPath() + File
.separator
153 + relativePath
.replace('/', File
.separatorChar
));
156 public File
getWritableBaseDir() {
157 if (withExecutionSubdirectory
) {
158 Date executionContextCreationDate
= (Date
) executionContext
159 .getVariable(ExecutionContext
.VAR_EXECUTION_CONTEXT_CREATION_DATE
);
160 Assert
.notNull(executionContext
, "execution context is null");
161 String path
= baseDir
.getPath() + File
.separator
162 + sdf().format(executionContextCreationDate
);
163 // TODO write execution id somewhere? like in a txt file
164 return new File(path
);
170 protected String
removeFilePrefix(String url
) {
171 if (url
.startsWith("file:"))
172 return url
.substring("file:".length());
173 else if (url
.startsWith("reference:file:"))
174 return url
.substring("reference:file:".length());
179 public void setBaseDir(File baseDir
) {
180 this.baseDir
= baseDir
;
183 public void setExecutionContext(ExecutionContext executionContext
) {
184 this.executionContext
= executionContext
;
187 public void setPrefixDatePattern(String prefixDatePattern
) {
188 this.prefixDatePattern
= prefixDatePattern
;
191 public File
getBaseDir() {
195 public ExecutionContext
getExecutionContext() {
196 return executionContext
;
199 public String
getPrefixDatePattern() {
200 return prefixDatePattern
;
203 /** Default is true. */
204 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory
) {
205 this.withExecutionSubdirectory
= withExecutionSubdirectory
;