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