]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/build/BundleModularDistribution.java
Introduce bundle register
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / build / BundleModularDistribution.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.Enumeration;
21 import java.util.SortedMap;
22 import java.util.StringTokenizer;
23 import java.util.jar.JarInputStream;
24 import java.util.jar.Manifest;
25
26 import org.apache.commons.io.IOUtils;
27 import org.argeo.slc.build.BasicNameVersion;
28 import org.argeo.slc.build.Distribution;
29 import org.argeo.slc.build.NameVersion;
30 import org.argeo.slc.core.build.VersionedResourceDistribution;
31 import org.osgi.framework.Constants;
32 import org.springframework.context.ResourceLoaderAware;
33 import org.springframework.core.io.ResourceLoader;
34
35 public class BundleModularDistribution extends AbstractOsgiModularDistribution
36 implements ResourceLoaderAware {
37 private ResourceLoader resourceLoader;
38
39 private String libDirectory = "/lib";
40
41 @SuppressWarnings(value = { "unchecked" })
42 protected void fillDistributions(
43 SortedMap<NameVersion, Distribution> distributions)
44 throws Exception {
45 Enumeration<URL> urls = (Enumeration<URL>) getBundleContext()
46 .getBundle().findEntries(libDirectory, "*.jar", false);
47 while (urls.hasMoreElements()) {
48 URL url = urls.nextElement();
49 JarInputStream in = null;
50 try {
51 in = new JarInputStream(url.openStream());
52 Manifest mf = in.getManifest();
53 String name = mf.getMainAttributes().getValue(
54 Constants.BUNDLE_SYMBOLICNAME);
55 // Skip additional specs such as
56 // ; singleton:=true
57 if (name.indexOf(';') > -1) {
58 name = new StringTokenizer(name, " ;").nextToken();
59 }
60
61 String version = mf.getMainAttributes().getValue(
62 Constants.BUNDLE_VERSION);
63 BasicNameVersion nameVersion = new BasicNameVersion(name,
64 version);
65 distributions.put(nameVersion,
66 new VersionedResourceDistribution(name, version,
67 resourceLoader.getResource(url.toString())));
68 } finally {
69 IOUtils.closeQuietly(in);
70 }
71 }
72 }
73
74 public void setLibDirectory(String libDirectory) {
75 this.libDirectory = libDirectory;
76 }
77
78 public void setResourceLoader(ResourceLoader resourceLoader) {
79 this.resourceLoader = resourceLoader;
80 }
81
82 /*
83 * @SuppressWarnings(value = { "unchecked" }) protected URL
84 * findModule(String moduleName, String version) { Enumeration<URL> urls =
85 * (Enumeration<URL>) bundleContext.getBundle() .findEntries(libDirectory,
86 * moduleName + "*", false);
87 *
88 * if (!urls.hasMoreElements()) throw new SlcException("Cannot find module "
89 * + moduleName);
90 *
91 * URL url = urls.nextElement();
92 *
93 * // TODO: check version as well if (urls.hasMoreElements()) throw new
94 * SlcException("More than one module with name " + moduleName); return url;
95 * }
96 */
97
98 }