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.SortedMap;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.argeo.slc.NameVersion;
24  import org.argeo.slc.build.Distribution;
25  import org.argeo.slc.core.build.VersionedResourceDistribution;
26  import org.argeo.slc.osgi.OsgiBundle;
27  import org.eclipse.gemini.blueprint.util.OsgiBundleUtils;
28  import org.osgi.framework.Bundle;
29  import org.springframework.context.ResourceLoaderAware;
30  import org.springframework.core.io.Resource;
31  import org.springframework.core.io.ResourceLoader;
32  
33  public class OsgiRuntimeModularDistribution extends
34  		AbstractOsgiModularDistribution implements ResourceLoaderAware {
35  	private final static Log log = LogFactory
36  			.getLog(OsgiRuntimeModularDistribution.class);
37  
38  	private ResourceLoader resourceLoader;
39  
40  	protected void fillDistributions(
41  			SortedMap<NameVersion, Distribution> distributions)
42  			throws Exception {
43  
44  		String frameworkUrl = System.getProperty("osgi.framework");
45  		String frameworkBaseUrl = null;
46  		if (frameworkUrl != null)
47  			frameworkBaseUrl = frameworkUrl.substring(0, frameworkUrl
48  					.lastIndexOf('/'));
49  		bundles: for (Bundle bundle : getBundleContext().getBundles()) {
50  			OsgiBundle osgiBundle = new OsgiBundle(bundle);
51  
52  			String originalLocation = bundle.getLocation();
53  
54  			if (OsgiBundleUtils.isSystemBundle(bundle)) {
55  				continue bundles;
56  			}
57  
58  			String location = originalLocation;
59  			if (originalLocation.startsWith("reference:file:"))
60  				location = originalLocation.substring("reference:".length());
61  
62  			if (frameworkBaseUrl != null
63  					&& originalLocation.startsWith("initial@reference:file:")) {
64  				location = frameworkBaseUrl
65  						+ '/'
66  						+ originalLocation.substring("initial@reference:file:"
67  								.length());
68  			}
69  
70  			try {
71  				URL url = new URL(location);
72  				Resource res = resourceLoader.getResource(url.toString());
73  				distributions.put(osgiBundle,
74  						new VersionedResourceDistribution(osgiBundle, res));
75  
76  				if (log.isTraceEnabled())
77  					log.debug("Added url " + url + " from original location "
78  							+ originalLocation);
79  			} catch (Exception e) {
80  				log.warn("Cannot interpret location " + location
81  						+ " of bundle " + bundle + ": " + e);
82  			}
83  		}
84  	}
85  
86  	public void setResourceLoader(ResourceLoader resourceLoader) {
87  		this.resourceLoader = resourceLoader;
88  	}
89  }