View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
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 it.jnrpe.osgi;
17  
18  import java.io.IOException;
19  import java.net.URL;
20  import java.util.Enumeration;
21  
22  import org.osgi.framework.Bundle;
23  
24  public class BundleDelegatingClassLoader extends ClassLoader {
25  
26  	private final Bundle bundle;
27  
28  	public BundleDelegatingClassLoader(final Bundle b) {
29  		bundle = b;
30  	}
31  
32  	@Override
33  	protected Class<?> findClass(final String className)
34  			throws ClassNotFoundException {
35  		return bundle.loadClass(className);
36  	}
37  
38  	@Override
39  	protected URL findResource(final String resName) {
40  		return bundle.getResource(resName);
41  	}
42  
43  	@SuppressWarnings("unchecked")
44  	@Override
45  	protected Enumeration<URL> findResources(final String resName)
46  			throws IOException {
47  		return bundle.getResources(resName);
48  	}
49  
50  }