| /* |
| * Copyright 1999-2004 The Apache Software Foundation. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.cocoon.util; |
| |
| import java.net.MalformedURLException; |
| import java.net.URL; |
| |
| /** |
| * A collection of class management utility methods. |
| * |
| * @version $Id$ |
| */ |
| public class ClassUtils { |
| |
| /** |
| * Create a new instance given a class name |
| * |
| * @param className A class name |
| * @return A new instance |
| * @exception Exception If an instantiation error occurs |
| */ |
| public static Object newInstance(String className) throws Exception { |
| return ClassUtils.loadClass(className).newInstance(); |
| } |
| |
| /** |
| * Load a class given its name. |
| * BL: We wan't to use a known ClassLoader--hopefully the heirarchy |
| * is set correctly. |
| * |
| * @param className A class name |
| * @return The class pointed to by <code>className</code> |
| * @exception ClassNotFoundException If a loading error occurs |
| */ |
| public static Class loadClass(String className) throws ClassNotFoundException { |
| return ClassUtils.getClassLoader().loadClass(className); |
| } |
| |
| /** |
| * Return a resource URL. |
| * BL: if this is command line operation, the classloading issues |
| * are more sane. During servlet execution, we explicitly set |
| * the ClassLoader. |
| * |
| * @return The context classloader. |
| * @exception MalformedURLException If a loading error occurs |
| */ |
| public static URL getResource(String resource) throws MalformedURLException { |
| return ClassUtils.getClassLoader().getResource(resource); |
| } |
| |
| /** |
| * Return the context classloader. |
| * BL: if this is command line operation, the classloading issues |
| * are more sane. During servlet execution, we explicitly set |
| * the ClassLoader. |
| * |
| * @return The context classloader. |
| */ |
| public static ClassLoader getClassLoader() { |
| return Thread.currentThread().getContextClassLoader(); |
| } |
| |
| } |