]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionResources.java
Improve Javadocs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionResources.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.osgi;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.SlcException;
25 import org.argeo.slc.core.execution.FileExecutionResources;
26 import org.osgi.framework.BundleContext;
27 import org.springframework.core.io.Resource;
28 import org.springframework.osgi.context.BundleContextAware;
29 import org.springframework.osgi.io.OsgiBundleResource;
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 // TODO: ability to access resources in other bundles
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 // TODO: Equinox specific?
63 String relPath = location.substring("initial@reference:file:"
64 .length());
65 // if (relPath.startsWith("../"))// relative to the framework jar
66 // relPath = relPath.substring("../".length());
67 // String framework = System.getProperty("osgi.framework").substring(
68 // "file:".length());
69 // log.debug(framework);
70 String installArea = System.getProperty("osgi.install.area")
71 .substring("file:".length());
72 // log.debug(installArea);
73 base = installArea + '/' + relPath;
74 // int sepIndex = framework.lastIndexOf(File.separatorChar);
75 // framework = framework.substring(0, sepIndex);
76 // base = framework + '/' + relPath;
77 } else {
78 return null;
79 }
80
81 String path = base + '/' + osgiBundleResource.getPathWithinContext();
82 try {
83 file = new File(path).getCanonicalFile();
84 } catch (IOException e) {
85 throw new SlcException("Cannot determine canonical path for "
86 + path, e);
87 }
88
89 if (!file.exists())
90 throw new SlcException(file
91 + " was retrieved in bundle located at '" + location
92 + "' for resource " + resource + " but it does not exist");
93
94 if (log.isTraceEnabled())
95 log.debug("OSGi local resource: " + file + " from " + resource);
96 return file;
97 }
98
99 public void setBundleContext(BundleContext bundleContext) {
100 this.bundleContext = bundleContext;
101 }
102
103 }