View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.util;
11  
12  import java.io.IOException;
13  import java.net.URL;
14  import java.util.ArrayList;
15  import java.util.Enumeration;
16  import java.util.List;
17  
18  import ch.qos.logback.core.Context;
19  
20  /**
21   * Load resources (or images) from various sources.
22   * 
23   * @author Ceki Gülcü
24   */
25  public class Loader {
26    static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
27  
28    private static boolean ignoreTCL = false;
29    public static final String IGNORE_TCL_PROPERTY_NAME = "logback.ignoreTCL";
30  
31    static {
32  
33      String ignoreTCLProp = OptionHelper.getSystemProperty(
34          IGNORE_TCL_PROPERTY_NAME, null);
35  
36      if (ignoreTCLProp != null) {
37        ignoreTCL = OptionHelper.toBoolean(ignoreTCLProp, true);
38      }
39    }
40  
41    /**
42     * Compute the number of occurrences a resource can be found by a class
43     * loader.
44     * 
45     * @param resource
46     * @param classLoader
47     * @return
48     * @throws IOException
49     */
50    public static List<URL> getResourceOccurenceCount(String resource,
51        ClassLoader classLoader) throws IOException {
52      List<URL> urlList = new ArrayList<URL>();
53      Enumeration<URL> urlEnum = classLoader.getResources(resource);
54      while (urlEnum.hasMoreElements()) {
55        URL url = urlEnum.nextElement();
56        urlList.add(url);
57      }
58  
59      return urlList;
60    }
61  
62    /**
63     * Search for a resource using the classloader passed as parameter.
64     * 
65     * @param resource
66     *                the resource name to look for
67     * @param classLoader
68     *                the classloader used for the search
69     */
70    public static URL getResource(String resource, ClassLoader classLoader) {
71      try {
72        return classLoader.getResource(resource);
73      } catch (Throwable t) {
74        return null;
75      }
76    }
77  
78    /**
79     * Attempt to find a resource by using the classloader that loaded this class,
80     * namely Loader.class.
81     * 
82     * @param resource
83     * @return
84     */
85    public static URL getResourceBySelfClassLoader(String resource) {
86      return getResource(resource, Loader.class.getClassLoader());
87    }
88  
89    // private static URL getResourceByTCL(String resource) {
90    // return getResource(resource, getTCL());
91    // }
92  
93    /**
94     * Get the Thread Context Loader which is a JDK 1.2 feature. If we are running
95     * under JDK 1.1 or anything else goes wrong the method returns
96     * <code>null<code>.
97     *
98     */
99    public static ClassLoader getTCL() {
100     return Thread.currentThread().getContextClassLoader();
101   }
102 
103   @SuppressWarnings("unchecked")
104   public static Class loadClass(String clazz, Context context)
105       throws ClassNotFoundException {
106     ClassLoader cl = context.getClass().getClassLoader();
107     return cl.loadClass(clazz);
108   }
109 
110   /**
111    * If running under JDK 1.2 load the specified class using the
112    * <code>Thread</code> <code>contextClassLoader</code> if that fails try
113    * Class.forname. Under JDK 1.1 only Class.forName is used.
114    * 
115    */
116   @SuppressWarnings("unchecked")
117   public static Class loadClass(String clazz) throws ClassNotFoundException {
118     // Just call Class.forName(clazz) if we are running under JDK 1.1
119     // or if we are instructed to ignore the TCL.
120     if (ignoreTCL) {
121       return Class.forName(clazz);
122     } else {
123       try {
124         return getTCL().loadClass(clazz);
125       } catch (Throwable e) {
126         // we reached here because tcl was null or because of a
127         // security exception, or because clazz could not be loaded...
128         // In any case we now try one more time
129         return Class.forName(clazz);
130       }
131     }
132   }
133 }