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
021 import org.apache.camel.spi.ClassResolver;
022 import org.apache.camel.util.ObjectHelper;
023
024 /**
025 * Default class resolver that uses regular class loader to load classes.
026 */
027 public class DefaultClassResolver implements ClassResolver {
028
029 public Class resolveClass(String name) {
030 return loadClass(name, DefaultClassResolver.class.getClassLoader());
031 }
032
033 @SuppressWarnings("unchecked")
034 public <T> Class<T> resolveClass(String name, Class<T> type) {
035 Class answer = loadClass(name, DefaultClassResolver.class.getClassLoader());
036 return (Class<T>) answer;
037 }
038
039 public Class resolveClass(String name, ClassLoader loader) {
040 return loadClass(name, loader);
041 }
042
043 @SuppressWarnings("unchecked")
044 public <T> Class<T> resolveClass(String name, Class<T> type, ClassLoader loader) {
045 Class answer = loadClass(name, loader);
046 return (Class<T>) 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 protected Class loadClass(String name, ClassLoader loader) {
087 ObjectHelper.notEmpty(name, "name");
088 return ObjectHelper.loadClass(name, loader);
089 }
090
091 }