View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
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  
17  package org.springframework.osgi.io;
18  
19  import org.osgi.framework.Bundle;
20  import org.springframework.core.io.DefaultResourceLoader;
21  import org.springframework.core.io.Resource;
22  import org.springframework.util.Assert;
23  
24  /**
25   * OSGi specific {@link org.springframework.core.io.ResourceLoader}
26   * implementation.
27   * 
28   * This loader resolves paths inside an OSGi bundle using the bundle native
29   * methods. Please see {@link OsgiBundleResource} javadoc for information on
30   * what prefixes are supported.
31   * 
32   * @author Adrian Colyer
33   * @author Costin Leau
34   * 
35   * @see org.osgi.framework.Bundle
36   * @see org.springframework.osgi.io.OsgiBundleResource
37   * 
38   */
39  public class OsgiBundleResourceLoader extends DefaultResourceLoader {
40  
41  	private final Bundle bundle;
42  
43  
44  	/**
45  	 * Creates a OSGi aware <code>ResourceLoader</code> using the given
46  	 * bundle.
47  	 * 
48  	 * @param bundle OSGi <code>Bundle</code> to be used by this loader
49  	 * loader.
50  	 */
51  	public OsgiBundleResourceLoader(Bundle bundle) {
52  		this.bundle = bundle;
53  	}
54  
55  	protected Resource getResourceByPath(String path) {
56  		Assert.notNull(path, "Path is required");
57  		return new OsgiBundleResource(this.bundle, path);
58  	}
59  
60  	public Resource getResource(String location) {
61  		Assert.notNull(location, "location is required");
62  		return new OsgiBundleResource(bundle, location);
63  	}
64  
65  	/**
66  	 * Returns the bundle used by this loader.
67  	 * 
68  	 * @return OSGi <code>Bundle</code> used by this resource
69  	 */
70  	public final Bundle getBundle() {
71  		return bundle;
72  	}
73  
74  }