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