001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.impl;
018
019 import java.io.IOException;
020 import java.net.URL;
021 import java.util.Enumeration;
022
023 /**
024 * WebSphere specific resolver to handle loading annotated resources in JAR files.
025 */
026 public class WebSpherePacakageScanClassResolver extends DefaultPackageScanClassResolver {
027
028 private String resourcePath;
029
030 /**
031 * Constructor.
032 *
033 * @param resourcePath the fixed resource path to use for fetching camel jars in WebSphere.
034 */
035 public WebSpherePacakageScanClassResolver(String resourcePath) {
036 this.resourcePath = resourcePath;
037 }
038
039 /**
040 * Is the classloader from IBM and thus the WebSphere platform?
041 *
042 * @param loader the classloader
043 * @return <tt>true</tt> if IBM classloader, <tt>false</tt> otherwise.
044 */
045 public static boolean isWebSphereClassLoader(ClassLoader loader) {
046 return loader.getClass().getName().startsWith("com.ibm");
047 }
048
049 /**
050 * Overloaded to handle specific problem with getting resources on the IBM WebSphere platform.
051 * <p/>
052 * WebSphere can <b>not</b> load resources if the resource to load is a folder name, such as a
053 * packagename, you have to explicit name a resource that is a file.
054 *
055 * @param loader the classloader
056 * @param packageName the packagename for the package to load
057 * @return URL's for the given package
058 * @throws java.io.IOException is thrown by the classloader
059 */
060 @Override
061 @SuppressWarnings("unchecked")
062 protected Enumeration<URL> getResources(ClassLoader loader, String packageName) throws IOException {
063 // try super first, just in vase
064 Enumeration<URL> enumeration = super.getResources(loader, packageName);
065 if (!enumeration.hasMoreElements()) {
066 LOG.trace("Using WebSphere workaround to load the camel jars with the annotated converters.");
067 // Special WebSphere trick to load a file that exists in the JAR and then let it go from there.
068 // The trick is that we just need the URL's for the .jars that contains the type
069 // converters that is annotated. So by searching for this resource WebSphere is able to find
070 // it and return the URL to the .jar file with the resource. Then the DefaultPackageScanClassResolver
071 // can take it from there and find the classes that are annotated.
072 enumeration = loader.getResources(resourcePath);
073 }
074
075 return enumeration;
076 }
077
078 }