2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.osgi
.build
;
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
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.argeo
.slc
.DefaultNameVersion
;
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
;
34 public class BundleModularDistribution
extends AbstractOsgiModularDistribution
35 implements ResourceLoaderAware
{
36 private ResourceLoader resourceLoader
;
38 private String libDirectory
= "/lib";
40 @SuppressWarnings(value
= { "unchecked" })
41 protected void fillDistributions(
42 SortedMap
<NameVersion
, Distribution
> distributions
)
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;
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
56 if (name
.indexOf(';') > -1) {
57 name
= new StringTokenizer(name
, " ;").nextToken();
60 String version
= mf
.getMainAttributes().getValue(
61 Constants
.BUNDLE_VERSION
);
62 DefaultNameVersion nameVersion
= new DefaultNameVersion(name
,
64 distributions
.put(nameVersion
,
65 new VersionedResourceDistribution(name
, version
,
66 resourceLoader
.getResource(url
.toString())));
68 IOUtils
.closeQuietly(in
);
73 public void setLibDirectory(String libDirectory
) {
74 this.libDirectory
= libDirectory
;
77 public void setResourceLoader(ResourceLoader resourceLoader
) {
78 this.resourceLoader
= resourceLoader
;
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);
87 * if (!urls.hasMoreElements()) throw new SlcException("Cannot find module "
90 * URL url = urls.nextElement();
92 * // TODO: check version as well if (urls.hasMoreElements()) throw new
93 * SlcException("More than one module with name " + moduleName); return url;