]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/build/BundleModularDistribution.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / org / argeo / slc / osgi / build / 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.DefaultNameVersion;
12 import org.argeo.slc.NameVersion;
13 import org.argeo.slc.build.Distribution;
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 protected void fillDistributions(
26 SortedMap<NameVersion, Distribution> distributions)
27 throws Exception {
28 Enumeration<URL> urls = (Enumeration<URL>) getBundleContext()
29 .getBundle().findEntries(libDirectory, "*.jar", false);
30 while (urls.hasMoreElements()) {
31 URL url = urls.nextElement();
32 JarInputStream in = null;
33 try {
34 in = new JarInputStream(url.openStream());
35 Manifest mf = in.getManifest();
36 String name = mf.getMainAttributes().getValue(
37 Constants.BUNDLE_SYMBOLICNAME);
38 // Skip additional specs such as
39 // ; singleton:=true
40 if (name.indexOf(';') > -1) {
41 name = new StringTokenizer(name, " ;").nextToken();
42 }
43
44 String version = mf.getMainAttributes().getValue(
45 Constants.BUNDLE_VERSION);
46 DefaultNameVersion nameVersion = new DefaultNameVersion(name,
47 version);
48 distributions.put(nameVersion,
49 new VersionedResourceDistribution(name, version,
50 resourceLoader.getResource(url.toString())));
51 } finally {
52 IOUtils.closeQuietly(in);
53 }
54 }
55 }
56
57 public void setLibDirectory(String libDirectory) {
58 this.libDirectory = libDirectory;
59 }
60
61 public void setResourceLoader(ResourceLoader resourceLoader) {
62 this.resourceLoader = resourceLoader;
63 }
64
65 /*
66 * @SuppressWarnings(value = { "unchecked" }) protected URL
67 * findModule(String moduleName, String version) { Enumeration<URL> urls =
68 * (Enumeration<URL>) bundleContext.getBundle() .findEntries(libDirectory,
69 * moduleName + "*", false);
70 *
71 * if (!urls.hasMoreElements()) throw new SlcException("Cannot find module "
72 * + moduleName);
73 *
74 * URL url = urls.nextElement();
75 *
76 * // TODO: check version as well if (urls.hasMoreElements()) throw new
77 * SlcException("More than one module with name " + moduleName); return url;
78 * }
79 */
80
81 }