]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/org/argeo/slc/osgi/OsgiExecutionResources.java
Disable trace logging
[gpl/argeo-slc.git] / org.argeo.slc.core / src / org / argeo / slc / osgi / OsgiExecutionResources.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.slc.osgi;
17
18 import java.io.File;
19 import java.io.IOException;
20
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;
29
30 /** Write access to resources in an OSGi context */
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 =
68 // System.getProperty("osgi.framework").substring(
69 // "file:".length());
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;
78 } else {
79 return null;
80 }
81
82 String path = base + '/' + osgiBundleResource.getPathWithinContext();
83 try {
84 file = new File(path).getCanonicalFile();
85 } catch (IOException e) {
86 throw new SlcException("Cannot determine canonical path for "
87 + path, e);
88 }
89
90 if (!file.exists())
91 throw new SlcException(file
92 + " was retrieved in bundle located at '" + location
93 + "' for resource " + resource + " but it does not exist");
94
95 if (log.isTraceEnabled())
96 log.debug("OSGi local resource: " + file + " from " + resource);
97 return file;
98 }
99
100 public void setBundleContext(BundleContext bundleContext) {
101 this.bundleContext = bundleContext;
102 }
103
104 }