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