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 463298 2006-10-12 16:10:32Z henning $
32   */
33  public class ClassUtils {
34  
35      /**
36       * Utility class; cannot be instantiated.
37       */
38      private ClassUtils()
39      {
40      }
41  
42      /**
43       * Return the specified class.  Checks the ThreadContext classloader first,
44       * then uses the System classloader.  Should replace all calls to
45       * <code>Class.forName( claz )</code> (which only calls the System class
46       * loader) when the class might be in a different classloader (e.g. in a
47       * webapp).
48       *
49       * @param clazz the name of the class to instantiate
50       * @return the requested Class object
51       * @throws ClassNotFoundException
52       */
53      public static Class getClass(String clazz) throws ClassNotFoundException
54      {
55          /**
56           * Use the Thread context classloader if possible
57           */
58          ClassLoader loader = Thread.currentThread().getContextClassLoader();
59          if (loader != null)
60          {
61              try
62              {
63                  return Class.forName(clazz, true, loader);
64              }
65              catch (ClassNotFoundException E)
66              {
67                  /**
68                   * If not found with ThreadContext loader, fall thru to
69                   * try System classloader below (works around bug in ant).
70                   */
71              }
72          }
73          /**
74           * Thread context classloader isn't working out, so use system loader.
75           */
76          return Class.forName(clazz);
77      }
78  
79      /**
80       * Return a new instance of the given class.  Checks the ThreadContext
81       * classloader first, then uses the System classloader.  Should replace all
82       * calls to <code>Class.forName( claz ).newInstance()</code> (which only
83       * calls the System class loader) when the class might be in a different
84       * classloader (e.g. in a webapp).
85       *
86       * @param clazz the name of the class to instantiate
87       * @return an instance of the specified class
88       * @throws ClassNotFoundException
89       * @throws IllegalAccessException
90       * @throws InstantiationException
91       */
92      public static Object getNewInstance(String clazz)
93          throws ClassNotFoundException,IllegalAccessException,InstantiationException
94      {
95          return getClass(clazz).newInstance();
96      }
97  
98      /**
99       * Finds a resource with the given name.  Checks the Thread Context
100      * classloader, then uses the System classloader.  Should replace all
101      * calls to <code>Class.getResourceAsString</code> when the resource
102      * might come from a different classloader.  (e.g. a webapp).
103      * @param claz Class to use when getting the System classloader (used if no Thread
104      * Context classloader available or fails to get resource).
105      * @param name name of the resource
106      * @return InputStream for the resource.
107      */
108     public static InputStream getResourceAsStream(Class claz, String name)
109     {
110         InputStream result = null;
111 
112         /**
113          * remove leading slash so path will work with classes in a JAR file
114          */
115         while (name.startsWith("/"))
116         {
117             name = name.substring(1);
118         }
119 
120         ClassLoader classLoader = Thread.currentThread()
121                                     .getContextClassLoader();
122 
123         if (classLoader == null)
124         {
125             classLoader = claz.getClassLoader();
126             result = classLoader.getResourceAsStream( name );
127         }
128         else
129         {
130             result= classLoader.getResourceAsStream( name );
131 
132             /**
133             * for compatibility with texen / ant tasks, fall back to
134             * old method when resource is not found.
135             */
136 
137             if (result == null)
138             {
139                 classLoader = claz.getClassLoader();
140                 if (classLoader != null)
141                     result = classLoader.getResourceAsStream( name );
142             }
143         }
144 
145         return result;
146 
147     }
148 
149 
150 }