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 */
017package org.apache.camel.impl;
018
019import java.io.IOException;
020import java.net.URL;
021import java.util.Enumeration;
022
023/**
024 * WebSphere specific resolver to handle loading annotated resources in JAR files.
025 */
026public class WebSpherePackageScanClassResolver extends DefaultPackageScanClassResolver {
027
028    private final 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 WebSpherePackageScanClassResolver(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 != null ? loader.getClass().getName().startsWith("com.ibm.") : false;
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    protected Enumeration<URL> getResources(ClassLoader loader, String packageName) throws IOException {
062        // try super first, just in vase
063        Enumeration<URL> enumeration = super.getResources(loader, packageName);
064        if (!enumeration.hasMoreElements()) {
065            log.trace("Using WebSphere workaround to load the camel jars with the annotated converters.");
066            // Special WebSphere trick to load a file that exists in the JAR and then let it go from there.
067            // The trick is that we just need the URL's for the .jars that contains the type
068            // converters that is annotated. So by searching for this resource WebSphere is able to find
069            // it and return the URL to the .jar file with the resource. Then the DefaultPackageScanClassResolver
070            // can take it from there and find the classes that are annotated.
071            enumeration = loader.getResources(resourcePath);
072        }
073
074        return enumeration;
075    }
076
077}