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.InputStream;
020    import java.net.URL;
021    
022    import org.apache.camel.spi.ClassResolver;
023    import org.apache.camel.util.CastUtils;
024    import org.apache.camel.util.ObjectHelper;
025    
026    /**
027     * Default class resolver that uses regular class loader to load classes.
028     */
029    public class DefaultClassResolver implements ClassResolver {
030    
031        public Class<?> resolveClass(String name) {
032            return loadClass(name, DefaultClassResolver.class.getClassLoader());
033        }
034    
035        public <T> Class<T> resolveClass(String name, Class<T> type) {
036            Class<T> answer = CastUtils.cast(loadClass(name, DefaultClassResolver.class.getClassLoader()));
037            return (Class<T>) answer;
038        }
039    
040        public Class<?> resolveClass(String name, ClassLoader loader) {
041            return loadClass(name, loader);
042        }
043    
044        public <T> Class<T> resolveClass(String name, Class<T> type, ClassLoader loader) {
045            Class<T> answer = CastUtils.cast(loadClass(name, loader));
046            return answer;
047        }
048    
049        public Class<?> resolveMandatoryClass(String name) throws ClassNotFoundException {
050            Class<?> answer = resolveClass(name);
051            if (answer == null) {
052                throw new ClassNotFoundException(name);
053            }
054            return answer;
055        }
056    
057        public <T> Class<T> resolveMandatoryClass(String name, Class<T> type) throws ClassNotFoundException {
058            Class<T> answer = resolveClass(name, type);
059            if (answer == null) {
060                throw new ClassNotFoundException(name);
061            }
062            return answer;
063        }
064    
065        public Class<?> resolveMandatoryClass(String name, ClassLoader loader) throws ClassNotFoundException {
066            Class<?> answer = resolveClass(name, loader);
067            if (answer == null) {
068                throw new ClassNotFoundException(name);
069            }
070            return answer;
071        }
072    
073        public <T> Class<T> resolveMandatoryClass(String name, Class<T> type, ClassLoader loader) throws ClassNotFoundException {
074            Class<T> answer = resolveClass(name, type, loader);
075            if (answer == null) {
076                throw new ClassNotFoundException(name);
077            }
078            return answer;
079        }
080    
081        public InputStream loadResourceAsStream(String uri) {
082            ObjectHelper.notEmpty(uri, "uri");
083            return ObjectHelper.loadResourceAsStream(uri);
084        }
085    
086        public URL loadResourceAsURL(String uri) {
087            ObjectHelper.notEmpty(uri, "uri");
088            return ObjectHelper.loadResourceAsURL(uri);
089        }
090    
091        protected Class<?> loadClass(String name, ClassLoader loader) {
092            ObjectHelper.notEmpty(name, "name");
093            return ObjectHelper.loadClass(name, loader);
094        }
095    }