2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.core
.execution
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
22 import java
.io
.OutputStream
;
23 import java
.text
.SimpleDateFormat
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.execution
.ExecutionContext
;
30 import org
.springframework
.core
.io
.FileSystemResource
;
31 import org
.springframework
.core
.io
.Resource
;
32 import org
.springframework
.util
.Assert
;
34 /** Implements write access to resources based on standard Java {@link File} */
35 public class FileExecutionResources
implements ExecutionResources
{
36 private final static Log log
= LogFactory
37 .getLog(FileExecutionResources
.class);
38 protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME
= "executionResources";
39 public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH
= System
40 .getProperty("java.io.tmpdir")
42 + System
.getProperty("user.name")
45 + File
.separator
+ DEFAULT_EXECUTION_RESOURCES_DIRNAME
;
48 private ExecutionContext executionContext
;
49 private String prefixDatePattern
= "yyyyMMdd_HHmmss_";
50 private SimpleDateFormat sdf
= null;
52 private Boolean withExecutionSubdirectory
= true;
54 public FileExecutionResources() {
55 // Default base directory
56 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
57 String osgiInstanceAreaDefault
= System
58 .getProperty("osgi.instance.area.default");
60 if (osgiInstanceArea
!= null) {
61 // within OSGi with -data specified
62 osgiInstanceArea
= removeFilePrefix(osgiInstanceArea
);
63 baseDir
= new File(osgiInstanceArea
+ File
.separator
64 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
65 } else if (osgiInstanceAreaDefault
!= null) {
66 // within OSGi without -data specified
67 osgiInstanceAreaDefault
= removeFilePrefix(osgiInstanceAreaDefault
);
68 baseDir
= new File(osgiInstanceAreaDefault
+ File
.separator
69 + DEFAULT_EXECUTION_RESOURCES_DIRNAME
);
70 } else {// outside OSGi
71 baseDir
= new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH
);
75 protected SimpleDateFormat
sdf() {
76 // Lazy init in case prefix has been externally set
78 sdf
= new SimpleDateFormat(prefixDatePattern
);
82 public Resource
getWritableResource(String relativePath
) {
83 File file
= getFile(relativePath
);
84 File parentDir
= file
.getParentFile();
86 if (!parentDir
.exists()) {
87 // Creates if necessary
88 if (log
.isTraceEnabled())
89 log
.trace("Creating parent directory " + parentDir
);
92 Resource resource
= new FileSystemResource(file
);
94 if (log
.isTraceEnabled())
95 log
.trace("Returns writable resource " + resource
);
99 public String
getWritableOsPath(String relativePath
) {
101 return getFile(relativePath
).getCanonicalPath();
102 } catch (IOException e
) {
103 throw new SlcException("Cannot find canonical path", e
);
107 public File
getWritableOsFile(String relativePath
) {
108 return getFile(relativePath
);
111 public String
getAsOsPath(Resource resource
, Boolean overwrite
) {
112 File file
= fileFromResource(resource
);
115 if (log
.isTraceEnabled())
116 log
.debug("Directly interpret " + resource
+ " as OS file "
118 return file
.getCanonicalPath();
119 } catch (IOException e1
) {
123 if (log
.isTraceEnabled())
124 log
.trace("Resource " + resource
125 + " is not available on the file system. Retrieving it...");
127 InputStream in
= null;
128 OutputStream out
= null;
130 String path
= resource
.getURL().getPath();
131 file
= getFile(path
);
132 if (file
.exists() && !overwrite
)
133 return file
.getCanonicalPath();
135 file
.getParentFile().mkdirs();
136 in
= resource
.getInputStream();
137 out
= new FileOutputStream(file
);
138 IOUtils
.copy(in
, out
);
139 if (log
.isDebugEnabled())
140 log
.debug("Retrieved " + resource
+ " to OS file " + file
);
141 return file
.getCanonicalPath();
142 } catch (IOException e
) {
143 throw new SlcException("Could not make resource " + resource
144 + " an OS file.", e
);
146 IOUtils
.closeQuietly(in
);
147 IOUtils
.closeQuietly(out
);
152 * Extract the underlying file from the resource.
154 * @return the file or null if no files support this resource.
156 protected File
fileFromResource(Resource resource
) {
158 return resource
.getFile();
159 } catch (IOException e
) {
165 protected File
getFile(String relativePath
) {
166 File writableBaseDir
= getWritableBaseDir();
167 return new File(writableBaseDir
.getPath() + File
.separator
168 + relativePath
.replace('/', File
.separatorChar
));
171 public File
getWritableBaseDir() {
172 if (withExecutionSubdirectory
) {
173 Assert
.notNull(executionContext
, "execution context is null");
174 String path
= baseDir
.getPath()
178 .getVariable(ExecutionContext
.VAR_EXECUTION_CONTEXT_CREATION_DATE
))
179 + executionContext
.getUuid();
180 return new File(path
);
186 protected String
removeFilePrefix(String url
) {
187 if (url
.startsWith("file:"))
188 return url
.substring("file:".length());
189 else if (url
.startsWith("reference:file:"))
190 return url
.substring("reference:file:".length());
195 public void setBaseDir(File baseDir
) {
196 this.baseDir
= baseDir
;
199 public void setExecutionContext(ExecutionContext executionContext
) {
200 this.executionContext
= executionContext
;
203 public void setPrefixDatePattern(String prefixDatePattern
) {
204 this.prefixDatePattern
= prefixDatePattern
;
207 public File
getBaseDir() {
211 public ExecutionContext
getExecutionContext() {
212 return executionContext
;
215 public String
getPrefixDatePattern() {
216 return prefixDatePattern
;
219 /** Default is true. */
220 public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory
) {
221 this.withExecutionSubdirectory
= withExecutionSubdirectory
;