View Javadoc

1   package liquibase.servicelocator;
2   
3   import java.io.IOException;
4   import java.net.URL;
5   import java.util.Enumeration;
6   
7   /**
8    * WebSphere specific resolver to handle loading annotated resources in JAR files.
9    */
10  public class WebSpherePackageScanClassResolver extends DefaultPackageScanClassResolver {
11  
12      private final String resourcePath;
13  
14      /**
15       * Constructor.
16       * 
17       * @param resourcePath
18       *            the fixed resource path to use for fetching camel jars in WebSphere.
19       */
20      public WebSpherePackageScanClassResolver(String resourcePath) {
21          this.resourcePath = resourcePath;
22      }
23  
24      /**
25       * Is the classloader from IBM and thus the WebSphere platform?
26       * 
27       * @param loader
28       *            the classloader
29       * @return <tt>true</tt> if IBM classloader, <tt>false</tt> otherwise.
30       */
31      public static boolean isWebSphereClassLoader(ClassLoader loader) {
32          return loader.getClass().getName().startsWith("com.ibm");
33      }
34  
35      /**
36       * Overloaded to handle specific problem with getting resources on the IBM WebSphere platform.
37       * <p/>
38       * WebSphere can <b>not</b> load resources if the resource to load is a folder name, such as a packagename, you have
39       * to explicit name a resource that is a file.
40       * 
41       * @param loader
42       *            the classloader
43       * @param packageName
44       *            the packagename for the package to load
45       * @return URL's for the given package
46       * @throws java.io.IOException
47       *             is thrown by the classloader
48       */
49      @Override
50      protected Enumeration<URL> getResources(ClassLoader loader, String packageName) throws IOException {
51          // try super first, just in vase
52          Enumeration<URL> enumeration = super.getResources(loader, packageName);
53          if (!enumeration.hasMoreElements()) {
54              log.debug("Using WebSphere workaround to load the camel jars with the annotated converters.");
55              // Special WebSphere trick to load a file that exists in the JAR and then let it go from there.
56              // The trick is that we just need the URL's for the .jars that contains the type
57              // converters that is annotated. So by searching for this resource WebSphere is able to find
58              // it and return the URL to the .jar file with the resource. Then the DefaultPackageScanClassResolver
59              // can take it from there and find the classes that are annotated.
60              enumeration = loader.getResources(resourcePath);
61          }
62  
63          return enumeration;
64      }
65  
66  }