View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.slc.osgi.build;
17  
18  import java.net.URL;
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;
24  
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;
33  
34  public class BundleModularDistribution extends AbstractOsgiModularDistribution
35  		implements ResourceLoaderAware {
36  	private ResourceLoader resourceLoader;
37  
38  	private String libDirectory = "/lib";
39  
40  	protected void fillDistributions(
41  			SortedMap<NameVersion, Distribution> distributions)
42  			throws Exception {
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;
48  			try {
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
54  				// ; singleton:=true
55  				if (name.indexOf(';') > -1) {
56  					name = new StringTokenizer(name, " ;").nextToken();
57  				}
58  
59  				String version = mf.getMainAttributes().getValue(
60  						Constants.BUNDLE_VERSION);
61  				DefaultNameVersion nameVersion = new DefaultNameVersion(name,
62  						version);
63  				distributions.put(nameVersion,
64  						new VersionedResourceDistribution(name, version,
65  								resourceLoader.getResource(url.toString())));
66  			} finally {
67  				IOUtils.closeQuietly(in);
68  			}
69  		}
70  	}
71  
72  	public void setLibDirectory(String libDirectory) {
73  		this.libDirectory = libDirectory;
74  	}
75  
76  	public void setResourceLoader(ResourceLoader resourceLoader) {
77  		this.resourceLoader = resourceLoader;
78  	}
79  
80  	/*
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);
85  	 * 
86  	 * if (!urls.hasMoreElements()) throw new SlcException("Cannot find module "
87  	 * + moduleName);
88  	 * 
89  	 * URL url = urls.nextElement();
90  	 * 
91  	 * // TODO: check version as well if (urls.hasMoreElements()) throw new
92  	 * SlcException("More than one module with name " + moduleName); return url;
93  	 * }
94  	 */
95  
96  }