]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/OsgiExecutionResources.java
Massive Argeo APIs refactoring
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / 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.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;
13
14 /** Write access to resources in an OSGi context */
15 public class OsgiExecutionResources extends FileExecutionResources implements
16 BundleContextAware {
17 private final static CmsLog log = CmsLog
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 =
52 // System.getProperty("osgi.framework").substring(
53 // "file:".length());
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;
62 } else {
63 return null;
64 }
65
66 String path = base + '/' + osgiBundleResource.getPathWithinContext();
67 try {
68 file = new File(path).getCanonicalFile();
69 } catch (IOException e) {
70 throw new SlcException("Cannot determine canonical path for "
71 + path, e);
72 }
73
74 if (!file.exists())
75 throw new SlcException(file
76 + " was retrieved in bundle located at '" + location
77 + "' for resource " + resource + " but it does not exist");
78
79 if (log.isTraceEnabled())
80 log.debug("OSGi local resource: " + file + " from " + resource);
81 return file;
82 }
83
84 public void setBundleContext(BundleContext bundleContext) {
85 this.bundleContext = bundleContext;
86 }
87
88 }