View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.river.osgi;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  import java.security.AccessController;
24  import java.security.PrivilegedAction;
25  import java.security.PrivilegedActionException;
26  import java.security.PrivilegedExceptionAction;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Enumeration;
30  
31  import org.osgi.framework.Bundle;
32  
33  /**
34   * A ClassLoader delegating to a given OSGi bundle.
35   */
36  public class BundleDelegatingClassLoader extends ClassLoader {
37      /*
38       Copied from cxf.apache.org
39       cxf/core/src/main/java/org/apache/cxf/bus/blueprint/BundleDelegatingClassLoader.java  
40      */
41      
42      private final Bundle bundle;
43      private final ClassLoader classLoader;
44  
45      public BundleDelegatingClassLoader(Bundle bundle) {
46          this(bundle, null);
47      }
48  
49      public BundleDelegatingClassLoader(Bundle bundle, ClassLoader classLoader) {
50          this.bundle = bundle;
51          this.classLoader = classLoader;
52      }
53  
54      @Override
55      protected Class<?> findClass(final String name) throws ClassNotFoundException {
56          try {
57              return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
58                  public Class<?> run() throws ClassNotFoundException {
59                      return bundle.loadClass(name);
60                  }
61              });
62          } catch (PrivilegedActionException e) {
63              Exception cause = e.getException();
64  
65              if (cause instanceof ClassNotFoundException) {
66                  throw (ClassNotFoundException)cause;
67              }
68              throw (RuntimeException)cause;
69          }
70      }
71  
72      @Override
73      protected URL findResource(final String name) {
74          URL resource = AccessController.doPrivileged(new PrivilegedAction<URL>() {
75              public URL run() {
76                  return bundle.getResource(name);
77              }
78          });
79          if (classLoader != null && resource == null) {
80              resource = classLoader.getResource(name);
81          }
82          return resource;
83      }
84  
85      @Override
86      protected Enumeration<URL> findResources(final String name) throws IOException {
87          Enumeration<URL> urls;
88          try {
89              urls = AccessController.doPrivileged(new PrivilegedExceptionAction<Enumeration<URL>>() {
90                  public Enumeration<URL> run() throws IOException {
91                      return bundle.getResources(name);
92                  }
93  
94              });
95          } catch (PrivilegedActionException e) {
96              Exception cause = e.getException();
97  
98              if (cause instanceof IOException) {
99                  throw (IOException)cause;
100             }
101             throw (RuntimeException)cause;
102         }
103 
104         if (urls == null) {
105             urls = Collections.enumeration(new ArrayList<URL>());
106         }
107 
108         return urls;
109     }
110 
111     @Override
112     protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
113         Class<?> clazz;
114         try {
115             clazz = findClass(name);
116         } catch (ClassNotFoundException cnfe) {
117             if (classLoader != null) {
118                 try {
119                     clazz = classLoader.loadClass(name);
120                 } catch (ClassNotFoundException e) {
121                     throw new ClassNotFoundException(name + " from bundle " + bundle.getBundleId()
122                                                      + " (" + bundle.getSymbolicName() + ")", cnfe);
123                 }
124             } else {
125                 throw new ClassNotFoundException(name + " from bundle " + bundle.getBundleId()
126                                                  + " (" + bundle.getSymbolicName() + ")", cnfe);
127             }
128         }
129         if (resolve) {
130             resolveClass(clazz);
131         }
132         return clazz;
133     }
134 
135     public Bundle getBundle() {
136         return bundle;
137     }
138 }