]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/build/OsgiRuntimeModularDistribution.java
Unique launch
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / build / OsgiRuntimeModularDistribution.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.build;
18
19 import java.net.URL;
20 import java.util.SortedMap;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.NameVersion;
25 import org.argeo.slc.build.Distribution;
26 import org.argeo.slc.core.build.VersionedResourceDistribution;
27 import org.argeo.slc.osgi.OsgiBundle;
28 import org.osgi.framework.Bundle;
29 import org.springframework.context.ResourceLoaderAware;
30 import org.springframework.core.io.Resource;
31 import org.springframework.core.io.ResourceLoader;
32 import org.springframework.osgi.util.OsgiBundleUtils;
33
34 public class OsgiRuntimeModularDistribution extends
35 AbstractOsgiModularDistribution implements ResourceLoaderAware {
36 private final static Log log = LogFactory
37 .getLog(OsgiRuntimeModularDistribution.class);
38
39 private ResourceLoader resourceLoader;
40
41 protected void fillDistributions(
42 SortedMap<NameVersion, Distribution> distributions)
43 throws Exception {
44
45 String frameworkUrl = System.getProperty("osgi.framework");
46 String frameworkBaseUrl = null;
47 if (frameworkUrl != null)
48 frameworkBaseUrl = frameworkUrl.substring(0, frameworkUrl
49 .lastIndexOf('/'));
50 bundles: for (Bundle bundle : getBundleContext().getBundles()) {
51 OsgiBundle osgiBundle = new OsgiBundle(bundle);
52
53 String originalLocation = bundle.getLocation();
54
55 if (OsgiBundleUtils.isSystemBundle(bundle)) {
56 continue bundles;
57 }
58
59 String location = originalLocation;
60 if (originalLocation.startsWith("reference:file:"))
61 location = originalLocation.substring("reference:".length());
62
63 if (frameworkBaseUrl != null
64 && originalLocation.startsWith("initial@reference:file:")) {
65 location = frameworkBaseUrl
66 + '/'
67 + originalLocation.substring("initial@reference:file:"
68 .length());
69 }
70
71 try {
72 URL url = new URL(location);
73 Resource res = resourceLoader.getResource(url.toString());
74 distributions.put(osgiBundle,
75 new VersionedResourceDistribution(osgiBundle, res));
76
77 if (log.isTraceEnabled())
78 log.debug("Added url " + url + " from original location "
79 + originalLocation);
80 } catch (Exception e) {
81 log.warn("Cannot interpret location " + location
82 + " of bundle " + bundle + ": " + e);
83 }
84 }
85 }
86
87 public void setResourceLoader(ResourceLoader resourceLoader) {
88 this.resourceLoader = resourceLoader;
89 }
90 }