001: package org.apache.velocity.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.InputStream;
023:
024: /**
025: * Simple utility functions for manipulating classes and resources
026: * from the classloader.
027: *
028: * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
029: * @version $Id: ClassUtils.java 463298 2006-10-12 16:10:32Z henning $
030: */
031: public class ClassUtils {
032:
033: /**
034: * Utility class; cannot be instantiated.
035: */
036: private ClassUtils() {
037: }
038:
039: /**
040: * Return the specified class. Checks the ThreadContext classloader first,
041: * then uses the System classloader. Should replace all calls to
042: * <code>Class.forName( claz )</code> (which only calls the System class
043: * loader) when the class might be in a different classloader (e.g. in a
044: * webapp).
045: *
046: * @param clazz the name of the class to instantiate
047: * @return the requested Class object
048: * @throws ClassNotFoundException
049: */
050: public static Class getClass(String clazz)
051: throws ClassNotFoundException {
052: /**
053: * Use the Thread context classloader if possible
054: */
055: ClassLoader loader = Thread.currentThread()
056: .getContextClassLoader();
057: if (loader != null) {
058: try {
059: return Class.forName(clazz, true, loader);
060: } catch (ClassNotFoundException E) {
061: /**
062: * If not found with ThreadContext loader, fall thru to
063: * try System classloader below (works around bug in ant).
064: */
065: }
066: }
067: /**
068: * Thread context classloader isn't working out, so use system loader.
069: */
070: return Class.forName(clazz);
071: }
072:
073: /**
074: * Return a new instance of the given class. Checks the ThreadContext
075: * classloader first, then uses the System classloader. Should replace all
076: * calls to <code>Class.forName( claz ).newInstance()</code> (which only
077: * calls the System class loader) when the class might be in a different
078: * classloader (e.g. in a webapp).
079: *
080: * @param clazz the name of the class to instantiate
081: * @return an instance of the specified class
082: * @throws ClassNotFoundException
083: * @throws IllegalAccessException
084: * @throws InstantiationException
085: */
086: public static Object getNewInstance(String clazz)
087: throws ClassNotFoundException, IllegalAccessException,
088: InstantiationException {
089: return getClass(clazz).newInstance();
090: }
091:
092: /**
093: * Finds a resource with the given name. Checks the Thread Context
094: * classloader, then uses the System classloader. Should replace all
095: * calls to <code>Class.getResourceAsString</code> when the resource
096: * might come from a different classloader. (e.g. a webapp).
097: * @param claz Class to use when getting the System classloader (used if no Thread
098: * Context classloader available or fails to get resource).
099: * @param name name of the resource
100: * @return InputStream for the resource.
101: */
102: public static InputStream getResourceAsStream(Class claz,
103: String name) {
104: InputStream result = null;
105:
106: /**
107: * remove leading slash so path will work with classes in a JAR file
108: */
109: while (name.startsWith("/")) {
110: name = name.substring(1);
111: }
112:
113: ClassLoader classLoader = Thread.currentThread()
114: .getContextClassLoader();
115:
116: if (classLoader == null) {
117: classLoader = claz.getClassLoader();
118: result = classLoader.getResourceAsStream(name);
119: } else {
120: result = classLoader.getResourceAsStream(name);
121:
122: /**
123: * for compatibility with texen / ant tasks, fall back to
124: * old method when resource is not found.
125: */
126:
127: if (result == null) {
128: classLoader = claz.getClassLoader();
129: if (classLoader != null)
130: result = classLoader.getResourceAsStream(name);
131: }
132: }
133:
134: return result;
135:
136: }
137:
138: }
|