2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.osgi
;
20 import java
.io
.IOException
;
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
;
31 public class OsgiExecutionResources
extends FileExecutionResources
implements
33 private final static Log log
= LogFactory
34 .getLog(OsgiExecutionResources
.class);
36 private BundleContext bundleContext
;
39 protected File
fileFromResource(Resource resource
) {
40 File file
= super.fileFromResource(resource
);
44 if (!(resource
instanceof OsgiBundleResource
))
47 OsgiBundleResource osgiBundleResource
= (OsgiBundleResource
) resource
;
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
);
56 // TODO: ability to access resources in other bundles
57 String location
= bundleContext
.getBundle().getLocation();
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:"
65 // if (relPath.startsWith("../"))// relative to the framework jar
66 // relPath = relPath.substring("../".length());
67 // String framework = System.getProperty("osgi.framework").substring(
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;
81 String path
= base
+ '/' + osgiBundleResource
.getPathWithinContext();
83 file
= new File(path
).getCanonicalFile();
84 } catch (IOException e
) {
85 throw new SlcException("Cannot determine canonical path for "
90 throw new SlcException(file
91 + " was retrieved in bundle located at '" + location
92 + "' for resource " + resource
+ " but it does not exist");
94 if (log
.isTraceEnabled())
95 log
.debug("OSGi local resource: " + file
+ " from " + resource
);
99 public void setBundleContext(BundleContext bundleContext
) {
100 this.bundleContext
= bundleContext
;