View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.io.IOException;
17  import java.net.URL;
18  import java.security.AccessControlException;
19  import java.security.AccessController;
20  import java.security.PrivilegedAction;
21  import java.util.ArrayList;
22  import java.util.Enumeration;
23  import java.util.List;
24  
25  import ch.qos.logback.core.Context;
26  
27  /**
28   * Load resources (or images) from various sources.
29   *
30   * @author Ceki Gülcü
31   */
32  public class Loader {
33    static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
34  
35    private static boolean ignoreTCL = false;
36    public static final String IGNORE_TCL_PROPERTY_NAME = "logback.ignoreTCL";
37    private static boolean HAS_GET_CLASS_LOADER_PERMISSION = false;
38  
39    static {
40      String ignoreTCLProp = OptionHelper.getSystemProperty(
41              IGNORE_TCL_PROPERTY_NAME, null);
42  
43      if (ignoreTCLProp != null) {
44        ignoreTCL = OptionHelper.toBoolean(ignoreTCLProp, true);
45      }
46  
47      HAS_GET_CLASS_LOADER_PERMISSION =
48              AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
49                public Boolean run() {
50                  try {
51                    AccessController.checkPermission(
52                            new RuntimePermission("getClassLoader"));
53                    return true;
54                  } catch (AccessControlException e) {
55                    return false;
56                  }
57                }
58              });
59    }
60  
61    /**
62     * Compute the number of occurrences a resource can be found by a class
63     * loader.
64     *
65     * @param resource
66     * @param classLoader
67     * @return
68     * @throws IOException
69     */
70    public static List<URL> getResourceOccurenceCount(String resource,
71                                                      ClassLoader classLoader) throws IOException {
72      List<URL> urlList = new ArrayList<URL>();
73      Enumeration<URL> urlEnum = classLoader.getResources(resource);
74      while (urlEnum.hasMoreElements()) {
75        URL url = urlEnum.nextElement();
76        urlList.add(url);
77      }
78  
79      return urlList;
80    }
81  
82    /**
83     * Search for a resource using the classloader passed as parameter.
84     *
85     * @param resource    the resource name to look for
86     * @param classLoader the classloader used for the search
87     */
88    public static URL getResource(String resource, ClassLoader classLoader) {
89      try {
90        return classLoader.getResource(resource);
91      } catch (Throwable t) {
92        return null;
93      }
94    }
95  
96    /**
97     * Attempt to find a resource by using the classloader that loaded this class,
98     * namely Loader.class.
99     *
100    * @param resource
101    * @return
102    */
103   public static URL getResourceBySelfClassLoader(String resource) {
104     return getResource(resource, getClassLoaderOfClass(Loader.class));
105   }
106 
107   // private static URL getResourceByTCL(String resource) {
108   // return getResource(resource, getTCL());
109   // }
110 
111   /**
112    * Get the Thread Context Loader which is a JDK 1.2 feature. If we are running
113    * under JDK 1.1 or anything else goes wrong the method returns
114    * <code>null<code>.
115    */
116   public static ClassLoader getTCL() {
117     return Thread.currentThread().getContextClassLoader();
118   }
119 
120   public static Class loadClass(String clazz, Context context)
121           throws ClassNotFoundException {
122     ClassLoader cl = getClassLoaderOfObject(context);
123     return cl.loadClass(clazz);
124   }
125 
126   /**
127    * Get the class loader of the object passed as argument. Return the system
128    * class loader if appropriate.
129    *
130    * @param o
131    * @return
132    */
133   public static ClassLoader getClassLoaderOfObject(Object o) {
134     if (o == null) {
135       throw new NullPointerException("Argument cannot be null");
136     }
137     return getClassLoaderOfClass(o.getClass());
138   }
139 
140   /**
141    * Returns the class loader of clazz in an access privileged section.
142    * @param clazz
143    * @return
144    */
145   public static ClassLoader getClassLoaderAsPrivileged(final Class clazz) {
146     if (!HAS_GET_CLASS_LOADER_PERMISSION)
147       return null;
148     else
149       return AccessController.doPrivileged(
150               new PrivilegedAction<ClassLoader>() {
151                 public ClassLoader run() {
152                   return clazz.getClassLoader();
153                 }
154               });
155   }
156 
157   /**
158    * Return the class loader which loaded the class passed as argument. Return
159    * the system class loader if appropriate.
160    *
161    * @param clazz
162    * @return
163    */
164   public static ClassLoader getClassLoaderOfClass(final Class clazz) {
165     ClassLoader cl = clazz.getClassLoader();
166     if (cl == null) {
167       return ClassLoader.getSystemClassLoader();
168     } else {
169       return cl;
170     }
171   }
172 
173   /**
174    * If running under JDK 1.2 load the specified class using the
175    * <code>Thread</code> <code>contextClassLoader</code> if that fails try
176    * Class.forname. Under JDK 1.1 only Class.forName is used.
177    */
178   public static Class loadClass(String clazz) throws ClassNotFoundException {
179     // Just call Class.forName(clazz) if we are running under JDK 1.1
180     // or if we are instructed to ignore the TCL.
181     if (ignoreTCL) {
182       return Class.forName(clazz);
183     } else {
184       try {
185         return getTCL().loadClass(clazz);
186       } catch (Throwable e) {
187         // we reached here because tcl was null or because of a
188         // security exception, or because clazz could not be loaded...
189         // In any case we now try one more time
190         return Class.forName(clazz);
191       }
192     }
193   }
194 }