View Javadoc

1   package com.atlassian.plugin.osgi.factory;
2   
3   import org.osgi.framework.Bundle;
4   import org.apache.log4j.Logger;
5   import org.apache.commons.collections.iterators.IteratorEnumeration;
6   import org.apache.commons.lang.Validate;
7   
8   import java.net.URL;
9   import java.util.Enumeration;
10  import java.util.Arrays;
11  import java.io.IOException;
12  import java.io.InputStream;
13  
14  /**
15   * Utility methods for accessing a bundle as if it was a classloader.
16   */
17  class BundleClassLoaderAccessor
18  {
19      private static final Logger log = Logger.getLogger(BundleClassLoaderAccessor.class);
20  
21      static ClassLoader getClassLoader(Bundle bundle) {
22          return new BundleClassLoader(bundle);
23      }
24  
25      static Class loadClass(Bundle bundle, String name, Class callingClass) throws ClassNotFoundException
26      {
27          Validate.notNull(bundle, "The bundle is required");
28          return bundle.loadClass(name);
29      }
30  
31      static URL getResource(Bundle bundle, String name)
32      {
33          Validate.notNull(bundle, "The bundle is required");
34          return bundle.getResource(name);
35      }
36  
37      static InputStream getResourceAsStream(Bundle bundle, String name)
38      {
39          Validate.notNull(bundle, "The bundle is required");
40          URL url = getResource(bundle, name);
41          if (url != null) {
42              try
43              {
44                  return url.openStream();
45              }
46              catch (IOException e)
47              {
48                  log.debug("Unable to load resource from bundle: "+bundle.getSymbolicName(), e);
49              }
50          }
51  
52          return null;
53      }
54  
55      ///CLOVER:OFF
56      /**
57       * Fake classloader that delegates to a bundle
58       */
59      private static class BundleClassLoader extends ClassLoader
60      {
61          private Bundle bundle;
62  
63          public BundleClassLoader(Bundle bundle)
64          {
65              Validate.notNull(bundle, "The bundle must not be null");
66              this.bundle = bundle;
67          }
68  
69          @Override
70          public Class findClass(String name) throws ClassNotFoundException
71          {
72              return bundle.loadClass(name);
73          }
74  
75          @Override
76          public Enumeration<URL> findResources(String name) throws IOException
77          {
78              Enumeration<URL> e = bundle.getResources(name);
79  
80              // For some reason, getResources() sometimes returns nothing, yet getResource() will return one.  This code
81              // handles that strange case
82              if (!e.hasMoreElements())
83              {
84                  URL resource = findResource(name);
85                  if (resource != null)
86                      e = new IteratorEnumeration(Arrays.asList(resource).iterator());
87              }
88              return e;
89          }
90  
91          @Override
92          public URL findResource(String name)
93          {
94              return bundle.getResource(name);
95          }
96      }
97  
98  
99  }