2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.osgi
;
19 import java
.io
.IOException
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.slc
.SlcException
;
24 import org
.argeo
.slc
.core
.execution
.FileExecutionResources
;
25 import org
.osgi
.framework
.BundleContext
;
26 import org
.springframework
.core
.io
.Resource
;
27 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
28 import org
.eclipse
.gemini
.blueprint
.io
.OsgiBundleResource
;
30 /** Write access to resources in an OSGi context */
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());
68 // System.getProperty("osgi.framework").substring(
70 // log.debug(framework);
71 String installArea
= System
.getProperty("osgi.install.area")
72 .substring("file:".length());
73 // log.debug(installArea);
74 base
= installArea
+ '/' + relPath
;
75 // int sepIndex = framework.lastIndexOf(File.separatorChar);
76 // framework = framework.substring(0, sepIndex);
77 // base = framework + '/' + relPath;
82 String path
= base
+ '/' + osgiBundleResource
.getPathWithinContext();
84 file
= new File(path
).getCanonicalFile();
85 } catch (IOException e
) {
86 throw new SlcException("Cannot determine canonical path for "
91 throw new SlcException(file
92 + " was retrieved in bundle located at '" + location
93 + "' for resource " + resource
+ " but it does not exist");
95 if (log
.isTraceEnabled())
96 log
.debug("OSGi local resource: " + file
+ " from " + resource
);
100 public void setBundleContext(BundleContext bundleContext
) {
101 this.bundleContext
= bundleContext
;