1 package org
.argeo
.slc
.osgi
;
4 import java
.io
.IOException
;
6 import org
.argeo
.api
.cms
.CmsLog
;
7 import org
.argeo
.slc
.SlcException
;
8 import org
.argeo
.slc
.core
.execution
.FileExecutionResources
;
9 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
10 import org
.eclipse
.gemini
.blueprint
.io
.OsgiBundleResource
;
11 import org
.osgi
.framework
.BundleContext
;
12 import org
.springframework
.core
.io
.Resource
;
14 /** Write access to resources in an OSGi context */
15 public class OsgiExecutionResources
extends FileExecutionResources
implements
17 private final static CmsLog log
= CmsLog
18 .getLog(OsgiExecutionResources
.class);
20 private BundleContext bundleContext
;
23 protected File
fileFromResource(Resource resource
) {
24 File file
= super.fileFromResource(resource
);
28 if (!(resource
instanceof OsgiBundleResource
))
31 OsgiBundleResource osgiBundleResource
= (OsgiBundleResource
) resource
;
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
);
40 // TODO: ability to access resources in other bundles
41 String location
= bundleContext
.getBundle().getLocation();
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:"
49 // if (relPath.startsWith("../"))// relative to the framework jar
50 // relPath = relPath.substring("../".length());
52 // System.getProperty("osgi.framework").substring(
54 // log.debug(framework);
55 String installArea
= System
.getProperty("osgi.install.area")
56 .substring("file:".length());
57 // log.debug(installArea);
58 base
= installArea
+ '/' + relPath
;
59 // int sepIndex = framework.lastIndexOf(File.separatorChar);
60 // framework = framework.substring(0, sepIndex);
61 // base = framework + '/' + relPath;
66 String path
= base
+ '/' + osgiBundleResource
.getPathWithinContext();
68 file
= new File(path
).getCanonicalFile();
69 } catch (IOException e
) {
70 throw new SlcException("Cannot determine canonical path for "
75 throw new SlcException(file
76 + " was retrieved in bundle located at '" + location
77 + "' for resource " + resource
+ " but it does not exist");
79 if (log
.isTraceEnabled())
80 log
.debug("OSGi local resource: " + file
+ " from " + resource
);
84 public void setBundleContext(BundleContext bundleContext
) {
85 this.bundleContext
= bundleContext
;