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