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.spi;
018
019import java.io.InputStream;
020import java.net.URL;
021import java.util.Enumeration;
022
023/**
024 * A class resolver for loading classes in a loosly coupled manner to cater for different platforms such
025 * as standalone, web container, j2ee container and OSGi platforms.
026 */
027public interface ClassResolver {
028
029    /**
030     * Resolves the given class by its name
031     *
032     * @param name full qualified name of class
033     * @return the class if resolved, <tt>null</tt> if not found.
034     */
035    Class<?> resolveClass(String name);
036
037    /**
038     * Resolves the given class by its name
039     *
040     * @param name full qualified name of class
041     * @param type the expected type of the class
042     * @return the class if resolved, <tt>null</tt> if not found.
043     */
044    <T> Class<T> resolveClass(String name, Class<T> type);
045
046    /**
047     * Resolves the given class by its name
048     *
049     * @param name   full qualified name of class
050     * @param loader use the provided class loader
051     * @return the class if resolved, <tt>null</tt> if not found.
052     */
053    Class<?> resolveClass(String name, ClassLoader loader);
054
055    /**
056     * Resolves the given class by its name
057     *
058     * @param name   full qualified name of class
059     * @param type   the expected type of the class
060     * @param loader use the provided class loader
061     * @return the class if resolved, <tt>null</tt> if not found.
062     */
063    <T> Class<T> resolveClass(String name, Class<T> type, ClassLoader loader);
064
065    /**
066     * Resolves the given class by its name
067     *
068     * @param name full qualified name of class
069     * @return the class if resolved, <tt>null</tt> if not found.
070     * @throws ClassNotFoundException is thrown if class not found
071     */
072    Class<?> resolveMandatoryClass(String name) throws ClassNotFoundException;
073
074    /**
075     * Resolves the given class by its name
076     *
077     * @param name full qualified name of class
078     * @param type the expected type of the class
079     * @return the class if resolved, <tt>null</tt> if not found.
080     * @throws ClassNotFoundException is thrown if class not found
081     */
082    <T> Class<T> resolveMandatoryClass(String name, Class<T> type) throws ClassNotFoundException;
083
084    /**
085     * Resolves the given class by its name
086     *
087     * @param name   full qualified name of class
088     * @param loader use the provided class loader
089     * @return the class if resolved, <tt>null</tt> if not found.
090     * @throws ClassNotFoundException is thrown if class not found
091     */
092    Class<?> resolveMandatoryClass(String name, ClassLoader loader) throws ClassNotFoundException;
093
094    /**
095     * Resolves the given class by its name
096     *
097     * @param name   full qualified name of class
098     * @param type   the expected type of the class
099     * @param loader use the provided class loader
100     * @return the class if resolved, <tt>null</tt> if not found.
101     * @throws ClassNotFoundException is thrown if class not found
102     */
103    <T> Class<T> resolveMandatoryClass(String name, Class<T> type, ClassLoader loader) throws ClassNotFoundException;
104
105    /**
106     * Loads the given resource as a stream
107     *
108     * @param uri the uri of the resource
109     * @return as a stream
110     */
111    InputStream loadResourceAsStream(String uri);
112
113    /**
114     * Loads the given resource as a URL
115     *
116     * @param uri the uri of the resource
117     * @return as a URL
118     */
119    URL loadResourceAsURL(String uri);
120
121    /**
122     * Loads the given resources as a URL from the current bundle/classloader
123     *
124     * @param uri the uri of the resource
125     * @return the URLs found on the classpath
126     */
127    Enumeration<URL> loadResourcesAsURL(String uri);
128
129    /**
130     * Loads the given resources as a URL from all bundles/classloaders
131     *
132     * @param uri the uri of the resource
133     * @return the URLs found on the classpath
134     */
135    Enumeration<URL> loadAllResourcesAsURL(String uri);
136}