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.deploy;
17  
18  import org.argeo.slc.core.deploy.DefaultResourceSet;
19  import org.eclipse.gemini.blueprint.context.BundleContextAware;
20  import org.eclipse.gemini.blueprint.io.OsgiBundleResourceLoader;
21  import org.eclipse.gemini.blueprint.io.OsgiBundleResourcePatternResolver;
22  import org.eclipse.gemini.blueprint.util.OsgiBundleUtils;
23  import org.osgi.framework.Bundle;
24  import org.osgi.framework.BundleContext;
25  import org.springframework.core.io.ResourceLoader;
26  
27  /**
28   * Retrieves ressources from an OSGi bundle either the active one or another one
29   * referenced by its symbolic name.
30   */
31  public class OsgiResourceSet extends DefaultResourceSet implements
32  		BundleContextAware {
33  	private BundleContext bundleContext;
34  	private Bundle bundle = null;
35  	private String bundleSymbolicName = null;
36  
37  	private OsgiBundleResourceLoader osgiBundleResourceLoader = null;
38  
39  	@Override
40  	public void afterPropertiesSet() throws Exception {
41  		osgiBundleResourceLoader = new OsgiBundleResourceLoader(getBundle());
42  		if (getResourcePatternResolver() == null)
43  			setResourcePatternResolver(new OsgiBundleResourcePatternResolver(
44  					osgiBundleResourceLoader));
45  		super.afterPropertiesSet();
46  	}
47  
48  	public Bundle getBundle() {
49  		if (bundle != null)
50  			return bundle;
51  		else if (bundleSymbolicName != null)// do not cache
52  			return OsgiBundleUtils.findBundleBySymbolicName(bundleContext,
53  					bundleSymbolicName);
54  		else
55  			// containing bundle
56  			return bundleContext.getBundle();
57  	}
58  
59  	public void setBundleContext(BundleContext bundleContext) {
60  		this.bundleContext = bundleContext;
61  	}
62  
63  	@Override
64  	public ResourceLoader getResourceLoaderToUse() {
65  		return osgiBundleResourceLoader;
66  	}
67  
68  	public void setBundle(Bundle bundle) {
69  		this.bundle = bundle;
70  	}
71  
72  	public void setBundleSymbolicName(String bundleSymbolicName) {
73  		this.bundleSymbolicName = bundleSymbolicName;
74  	}
75  
76  }