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 protected void fillDistributions(
41 SortedMap
<NameVersion
, Distribution
> distributions
)
43 Enumeration
<URL
> urls
= (Enumeration
<URL
>) getBundleContext()
44 .getBundle().findEntries(libDirectory
, "*.jar", false);
45 while (urls
.hasMoreElements()) {
46 URL url
= urls
.nextElement();
47 JarInputStream in
= null;
49 in
= new JarInputStream(url
.openStream());
50 Manifest mf
= in
.getManifest();
51 String name
= mf
.getMainAttributes().getValue(
52 Constants
.BUNDLE_SYMBOLICNAME
);
53 // Skip additional specs such as
55 if (name
.indexOf(';') > -1) {
56 name
= new StringTokenizer(name
, " ;").nextToken();
59 String version
= mf
.getMainAttributes().getValue(
60 Constants
.BUNDLE_VERSION
);
61 DefaultNameVersion nameVersion
= new DefaultNameVersion(name
,
63 distributions
.put(nameVersion
,
64 new VersionedResourceDistribution(name
, version
,
65 resourceLoader
.getResource(url
.toString())));
67 IOUtils
.closeQuietly(in
);
72 public void setLibDirectory(String libDirectory
) {
73 this.libDirectory
= libDirectory
;
76 public void setResourceLoader(ResourceLoader resourceLoader
) {
77 this.resourceLoader
= resourceLoader
;
81 * @SuppressWarnings(value = { "unchecked" }) protected URL
82 * findModule(String moduleName, String version) { Enumeration<URL> urls =
83 * (Enumeration<URL>) bundleContext.getBundle() .findEntries(libDirectory,
84 * moduleName + "*", false);
86 * if (!urls.hasMoreElements()) throw new SlcException("Cannot find module "
89 * URL url = urls.nextElement();
91 * // TODO: check version as well if (urls.hasMoreElements()) throw new
92 * SlcException("More than one module with name " + moduleName); return url;