View Javadoc

1   package org.apache.velocity.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.InputStream;
23  
24  
25  
26  /**
27   * Simple utility functions for manipulating classes and resources
28   * from the classloader.
29   *
30   *  @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
31   *  @version $Id: ClassUtils.java 685685 2008-08-13 21:43:27Z nbubna $
32   * @since 1.5
33   */
34  public class ClassUtils {
35  
36      /**
37       * Utility class; cannot be instantiated.
38       */
39      private ClassUtils()
40      {
41      }
42  
43      /**
44       * Return the specified class.  Checks the ThreadContext classloader first,
45       * then uses the System classloader.  Should replace all calls to
46       * <code>Class.forName( claz )</code> (which only calls the System class
47       * loader) when the class might be in a different classloader (e.g. in a
48       * webapp).
49       *
50       * @param clazz the name of the class to instantiate
51       * @return the requested Class object
52       * @throws ClassNotFoundException
53       */
54      public static Class getClass(String clazz) throws ClassNotFoundException
55      {
56          /**
57           * Use the Thread context classloader if possible
58           */
59          ClassLoader loader = Thread.currentThread().getContextClassLoader();
60          if (loader != null)
61          {
62              try
63              {
64                  return Class.forName(clazz, true, loader);
65              }
66              catch (ClassNotFoundException E)
67              {
68                  /**
69                   * If not found with ThreadContext loader, fall thru to
70                   * try System classloader below (works around bug in ant).
71                   */
72              }
73          }
74          /**
75           * Thread context classloader isn't working out, so use system loader.
76           */
77          return Class.forName(clazz);
78      }
79  
80      /**
81       * Return a new instance of the given class.  Checks the ThreadContext
82       * classloader first, then uses the System classloader.  Should replace all
83       * calls to <code>Class.forName( claz ).newInstance()</code> (which only
84       * calls the System class loader) when the class might be in a different
85       * classloader (e.g. in a webapp).
86       *
87       * @param clazz the name of the class to instantiate
88       * @return an instance of the specified class
89       * @throws ClassNotFoundException
90       * @throws IllegalAccessException
91       * @throws InstantiationException
92       */
93      public static Object getNewInstance(String clazz)
94          throws ClassNotFoundException,IllegalAccessException,InstantiationException
95      {
96          return getClass(clazz).newInstance();
97      }
98  
99      /**
100      * Finds a resource with the given name.  Checks the Thread Context
101      * classloader, then uses the System classloader.  Should replace all
102      * calls to <code>Class.getResourceAsString</code> when the resource
103      * might come from a different classloader.  (e.g. a webapp).
104      * @param claz Class to use when getting the System classloader (used if no Thread
105      * Context classloader available or fails to get resource).
106      * @param name name of the resource
107      * @return InputStream for the resource.
108      */
109     public static InputStream getResourceAsStream(Class claz, String name)
110     {
111         InputStream result = null;
112 
113         /**
114          * remove leading slash so path will work with classes in a JAR file
115          */
116         while (name.startsWith("/"))
117         {
118             name = name.substring(1);
119         }
120 
121         ClassLoader classLoader = Thread.currentThread()
122                                     .getContextClassLoader();
123 
124         if (classLoader == null)
125         {
126             classLoader = claz.getClassLoader();
127             result = classLoader.getResourceAsStream( name );
128         }
129         else
130         {
131             result= classLoader.getResourceAsStream( name );
132 
133             /**
134             * for compatibility with texen / ant tasks, fall back to
135             * old method when resource is not found.
136             */
137 
138             if (result == null)
139             {
140                 classLoader = claz.getClassLoader();
141                 if (classLoader != null)
142                     result = classLoader.getResourceAsStream( name );
143             }
144         }
145 
146         return result;
147 
148     }
149 
150 
151 }