/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; /** * A collection of class management utility methods. * * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a> * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a> * @version CVS $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 hierarchy * 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(); } /** * Tests if a class implements a given interface * * @return true if class implements given interface. * @deprecated Will be removed 2 versions after 2.1.5 */ public static boolean implementsInterface(String className, String iface) throws Exception { Class class1 = ClassUtils.loadClass (className); Class class2 = ClassUtils.loadClass (iface); return ClassUtils.implementsInterface(class1, class2); } /** * Tests if a class implements a given interface * * @return true if class implements given interface. * @deprecated Will be removed 2 versions after 2.1.5 */ public static boolean implementsInterface(Class class1, Class iface) { return iface.isAssignableFrom (class1); } /** * Determine the last modification date for this * class file or its enclosing library * * @param aClass A class whose last modification date is queried * @return The time the given class was last modified * @exception IOException IOError * @exception IllegalArgumentException The class was not loaded from a file * or directory * @deprecated Will be removed 2 versions after 2.1.5 */ public static long lastModified(Class aClass) throws IOException, IllegalArgumentException { URL url = aClass.getProtectionDomain().getCodeSource().getLocation(); if (!url.getProtocol().equals("file")) { throw new IllegalArgumentException("Class was not loaded from a file url"); } File directory = new File(url.getFile()); if (!directory.isDirectory()) { throw new IllegalArgumentException("Class was not loaded from a directory"); } String className = aClass.getName(); String basename = className.substring(className.lastIndexOf(".") + 1); File file = new File(directory, basename + ".class"); return file.lastModified(); } /** * Gets the absolute pathname of the class file * containing the specified class name, as prescribed * by the current classpath. * * @param aClass Name of the class. * @deprecated Will be removed 2 versions after 2.1.5 */ public static String which(Class aClass) { String path = null; try { path = aClass.getProtectionDomain().getCodeSource().getLocation().toString(); } catch (Throwable t){ } return path; } }