]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionResources.java
5582d9ca02f3df556f26e953e43ba4ed663352da
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionResources.java
1 package org.argeo.slc.osgi;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.SlcException;
9 import org.argeo.slc.core.execution.FileExecutionResources;
10 import org.osgi.framework.BundleContext;
11 import org.springframework.core.io.Resource;
12 import org.springframework.osgi.context.BundleContextAware;
13 import org.springframework.osgi.io.OsgiBundleResource;
14
15 public class OsgiExecutionResources extends FileExecutionResources implements
16 BundleContextAware {
17 private final static Log log = LogFactory
18 .getLog(OsgiExecutionResources.class);
19
20 private BundleContext bundleContext;
21
22 @Override
23 protected File fileFromResource(Resource resource) {
24 File file = super.fileFromResource(resource);
25 if (file != null)
26 return file;
27
28 if (!(resource instanceof OsgiBundleResource))
29 return null;
30
31 OsgiBundleResource osgiBundleResource = (OsgiBundleResource) resource;
32 try {
33 return osgiBundleResource.getFile();
34 } catch (IOException e) {
35 if (log.isTraceEnabled())
36 log.trace("Resource " + resource
37 + " is not available on the file system: " + e);
38 }
39
40 // TODO: ability to access resources in other bundles
41 String location = bundleContext.getBundle().getLocation();
42 String base = null;
43 if (location.startsWith("reference:file:"))
44 base = location.substring("reference:file:".length());
45 else if (location.startsWith("initial@reference:file:")) {
46 // TODO: Equinox specific?
47 String relPath = location.substring("initial@reference:file:"
48 .length());
49 if (relPath.startsWith("../"))// relative to the framework jar
50 relPath = relPath.substring("../".length());
51 String framework = System.getProperty("osgi.framework").substring(
52 "file:".length());
53 int sepIndex = framework.lastIndexOf(File.separatorChar);
54 framework = framework.substring(0, sepIndex);
55 base = framework + '/' + relPath;
56 } else {
57 return null;
58 }
59
60 String path = base + '/' + osgiBundleResource.getPathWithinContext();
61 try {
62 file = new File(path).getCanonicalFile();
63 } catch (IOException e) {
64 throw new SlcException("Cannot determine canonical path for "
65 + path, e);
66 }
67 if (log.isTraceEnabled())
68 log.debug("OSGi local resource: " + file + " from " + resource);
69 return file;
70 }
71
72 public void setBundleContext(BundleContext bundleContext) {
73 this.bundleContext = bundleContext;
74 }
75
76 }