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 loader.
61       *
62       * @param resource
63       * @param classLoader
64       * @return
65       * @throws IOException
66       */
67  
68      public static Set<URL> getResources(String resource, ClassLoader classLoader) throws IOException {
69          // See LBCLASSIC-159
70          Set<URL> urlSet = new HashSet<URL>();
71          Enumeration<URL> urlEnum = classLoader.getResources(resource);
72          while (urlEnum.hasMoreElements()) {
73              URL url = urlEnum.nextElement();
74              urlSet.add(url);
75          }
76          return urlSet;
77      }
78  
79      /**
80       * Search for a resource using the classloader passed as parameter.
81       *
82       * @param resource    the resource name to look for
83       * @param classLoader the classloader used for the search
84       */
85      public static URL getResource(String resource, ClassLoader classLoader) {
86          try {
87              return classLoader.getResource(resource);
88          } catch (Throwable t) {
89              return null;
90          }
91      }
92  
93      /**
94       * Attempt to find a resource by using the classloader that loaded this class,
95       * namely Loader.class.
96       *
97       * @param resource
98       * @return
99       */
100     public static URL getResourceBySelfClassLoader(String resource) {
101         return getResource(resource, getClassLoaderOfClass(Loader.class));
102     }
103 
104     // private static URL getResourceByTCL(String resource) {
105     // return getResource(resource, getTCL());
106     // }
107 
108     /**
109      * Get the Thread Context Loader which is a JDK 1.2 feature. If we are running
110      * under JDK 1.1 or anything else goes wrong the method returns {@code null}.
111      */
112     public static ClassLoader getTCL() {
113         return Thread.currentThread().getContextClassLoader();
114     }
115 
116     public static Class<?> loadClass(String clazz, Context context) throws ClassNotFoundException {
117         ClassLoader cl = getClassLoaderOfObject(context);
118         return cl.loadClass(clazz);
119     }
120 
121     /**
122      * Get the class loader of the object passed as argument. Return the system
123      * class loader if appropriate.
124      *
125      * @param o
126      * @return
127      */
128     public static ClassLoader getClassLoaderOfObject(Object o) {
129         if (o == null) {
130             throw new NullPointerException("Argument cannot be null");
131         }
132         return getClassLoaderOfClass(o.getClass());
133     }
134 
135     /**
136      * Returns the class loader of clazz in an access privileged section.
137      *
138      * @param clazz
139      * @return
140      */
141     public static ClassLoader getClassLoaderAsPrivileged(final Class<?> clazz) {
142         if (!HAS_GET_CLASS_LOADER_PERMISSION)
143             return null;
144         else
145             return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
146                 public ClassLoader run() {
147                     return clazz.getClassLoader();
148                 }
149             });
150     }
151 
152     /**
153      * Return the class loader which loaded the class passed as argument. Return the
154      * system class loader if appropriate.
155      *
156      * @param clazz
157      * @return
158      */
159     public static ClassLoader getClassLoaderOfClass(final Class<?> clazz) {
160         ClassLoader cl = clazz.getClassLoader();
161         if (cl == null) {
162             return ClassLoader.getSystemClassLoader();
163         } else {
164             return cl;
165         }
166     }
167 
168     /**
169      * If running under JDK 1.2 load the specified class using the
170      * <code>Thread</code> <code>contextClassLoader</code> if that fails try
171      * Class.forname. Under JDK 1.1 only Class.forName is used.
172      */
173     public static Class<?> loadClass(String clazz) throws ClassNotFoundException {
174         // Just call Class.forName(clazz) if we are running under JDK 1.1
175         // or if we are instructed to ignore the TCL.
176         if (ignoreTCL) {
177             return Class.forName(clazz);
178         } else {
179             try {
180                 return getTCL().loadClass(clazz);
181             } catch (Throwable e) {
182                 // we reached here because tcl was null or because of a
183                 // security exception, or because clazz could not be loaded...
184                 // In any case we now try one more time
185                 return Class.forName(clazz);
186             }
187         }
188     }
189 }