1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.osgi;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.slc.SlcException;
24 import org.argeo.slc.core.execution.FileExecutionResources;
25 import org.eclipse.gemini.blueprint.context.BundleContextAware;
26 import org.eclipse.gemini.blueprint.io.OsgiBundleResource;
27 import org.osgi.framework.BundleContext;
28 import org.springframework.core.io.Resource;
29
30
31 public class OsgiExecutionResources extends FileExecutionResources implements
32 BundleContextAware {
33 private final static Log log = LogFactory
34 .getLog(OsgiExecutionResources.class);
35
36 private BundleContext bundleContext;
37
38 @Override
39 protected File fileFromResource(Resource resource) {
40 File file = super.fileFromResource(resource);
41 if (file != null)
42 return file;
43
44 if (!(resource instanceof OsgiBundleResource))
45 return null;
46
47 OsgiBundleResource osgiBundleResource = (OsgiBundleResource) resource;
48 try {
49 return osgiBundleResource.getFile();
50 } catch (IOException e) {
51 if (log.isTraceEnabled())
52 log.trace("Resource " + resource
53 + " is not available on the file system: " + e);
54 }
55
56
57 String location = bundleContext.getBundle().getLocation();
58 String base = null;
59 if (location.startsWith("reference:file:"))
60 base = location.substring("reference:file:".length());
61 else if (location.startsWith("initial@reference:file:")) {
62
63 String relPath = location.substring("initial@reference:file:"
64 .length());
65
66
67
68
69
70
71 String installArea = System.getProperty("osgi.install.area")
72 .substring("file:".length());
73
74 base = installArea + '/' + relPath;
75
76
77
78 } else {
79 return null;
80 }
81
82 String path = base + '/' + osgiBundleResource.getPathWithinContext();
83 try {
84 file = new File(path).getCanonicalFile();
85 } catch (IOException e) {
86 throw new SlcException("Cannot determine canonical path for "
87 + path, e);
88 }
89
90 if (!file.exists())
91 throw new SlcException(file
92 + " was retrieved in bundle located at '" + location
93 + "' for resource " + resource + " but it does not exist");
94
95 if (log.isTraceEnabled())
96 log.debug("OSGi local resource: " + file + " from " + resource);
97 return file;
98 }
99
100 public void setBundleContext(BundleContext bundleContext) {
101 this.bundleContext = bundleContext;
102 }
103
104 }