2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.osgi
.build
;
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
;
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
;
35 public class BundleModularDistribution
extends AbstractOsgiModularDistribution
36 implements ResourceLoaderAware
{
37 private ResourceLoader resourceLoader
;
39 private String libDirectory
= "/lib";
41 @SuppressWarnings(value
= { "unchecked" })
42 protected void fillDistributions(
43 SortedMap
<NameVersion
, Distribution
> distributions
)
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;
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
57 if (name
.indexOf(';') > -1) {
58 name
= new StringTokenizer(name
, " ;").nextToken();
61 String version
= mf
.getMainAttributes().getValue(
62 Constants
.BUNDLE_VERSION
);
63 BasicNameVersion nameVersion
= new BasicNameVersion(name
,
65 distributions
.put(nameVersion
,
66 new VersionedResourceDistribution(name
, version
,
67 resourceLoader
.getResource(url
.toString())));
69 IOUtils
.closeQuietly(in
);
74 public void setLibDirectory(String libDirectory
) {
75 this.libDirectory
= libDirectory
;
78 public void setResourceLoader(ResourceLoader resourceLoader
) {
79 this.resourceLoader
= resourceLoader
;
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);
88 * if (!urls.hasMoreElements()) throw new SlcException("Cannot find module "
91 * URL url = urls.nextElement();
93 * // TODO: check version as well if (urls.hasMoreElements()) throw new
94 * SlcException("More than one module with name " + moduleName); return url;