View Javadoc

1   /*
2    * $Id: ClassLocator.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.util;
6   
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.net.MalformedURLException;
10  import java.net.URL;
11  
12  import javax.faces.context.FacesContext;
13  import javax.servlet.ServletContext;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /**
19   * 
20   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
21   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
22   */
23  public class ClassLocator {
24      private static final Log LOG = LogFactory.getLog(ClassLocator.class);
25  
26      public static final Class load(String className, Object fallback,
27              Object context) throws ClassNotFoundException {
28  
29          ClassNotFoundException thOrigin = null;
30          RuntimeException rtOrigin = null;
31  
32          ClassLoader cl = Thread.currentThread().getContextClassLoader();
33          if (cl != null) {
34              try {
35                  return cl.loadClass(className);
36  
37              } catch (ClassNotFoundException ex) {
38                  if (thOrigin == null) {
39                      thOrigin = ex;
40                  }
41  
42                  if (LOG.isDebugEnabled()) {
43                      LOG.debug("Find class '" + className + "'", ex);
44                  }
45  
46              } catch (RuntimeException ex) {
47                  if (rtOrigin == null) {
48                      rtOrigin = ex;
49                  }
50  
51                  if (LOG.isDebugEnabled()) {
52                      LOG.debug("Find class '" + className + "'", ex);
53                  }
54              }
55          }
56  
57          if (fallback != null) {
58              Class cls;
59              if (fallback instanceof Class) {
60                  cls = (Class) fallback;
61              } else {
62                  cls = fallback.getClass();
63              }
64  
65              try {
66                  return cls.getClassLoader().loadClass(className);
67  
68              } catch (ClassNotFoundException ex) {
69                  if (thOrigin == null) {
70                      thOrigin = ex;
71                  }
72  
73                  if (LOG.isDebugEnabled()) {
74                      LOG.debug("Find class '" + className + "' fallbackClass="
75                              + cls, ex);
76                  }
77  
78              } catch (RuntimeException ex) {
79                  if (rtOrigin == null) {
80                      rtOrigin = ex;
81                  }
82  
83                  if (LOG.isDebugEnabled()) {
84                      LOG.debug("Find class '" + className + "' fallbackClass="
85                              + cls, ex);
86                  }
87              }
88          }
89  
90          try {
91              return ClassLocator.class.getClassLoader().loadClass(className);
92  
93          } catch (ClassNotFoundException ex) {
94              if (thOrigin == null) {
95                  thOrigin = ex;
96              }
97  
98              if (LOG.isDebugEnabled()) {
99                  LOG.debug("Find class '" + className + "' (classLocator)", ex);
100             }
101 
102         } catch (RuntimeException ex) {
103             if (rtOrigin == null) {
104                 rtOrigin = ex;
105             }
106 
107             if (LOG.isDebugEnabled()) {
108                 LOG.debug("Find class '" + className + "' (classLocator)", ex);
109             }
110         }
111 
112         if (context instanceof FacesContext) {
113             context = ((FacesContext) context).getExternalContext()
114                     .getContext();
115         }
116 
117         if (context instanceof ServletContext) {
118             try {
119                 context = ((ServletContext) context).getClass()
120                         .getClassLoader();
121 
122             } catch (RuntimeException ex) {
123                 LOG.debug("Can not get classLoader of servletContext.", ex);
124             }
125         }
126 
127         if (context instanceof ClassLoader) {
128             try {
129                 return ((ClassLoader) context).loadClass(className);
130 
131             } catch (ClassNotFoundException ex) {
132                 if (thOrigin == null) {
133                     thOrigin = ex;
134                 }
135 
136                 if (LOG.isDebugEnabled()) {
137                     LOG.debug("Find class '" + className + "' (context="
138                             + context + ")", ex);
139                 }
140 
141             } catch (RuntimeException ex) {
142                 if (rtOrigin == null) {
143                     rtOrigin = ex;
144                 }
145 
146                 if (LOG.isDebugEnabled()) {
147                     LOG.debug("Find class '" + className + "' (context="
148                             + context + ")", ex);
149                 }
150             }
151         }
152 
153         if (thOrigin == null) {
154             if (LOG.isDebugEnabled()) {
155                 LOG.debug("Can not find class '" + className + "'.");
156             }
157 
158             thOrigin = new ClassNotFoundException("Can not find class '"
159                     + className + "'.", rtOrigin);
160         }
161 
162         throw thOrigin;
163     }
164 
165     public static final URL getResource(String resourceLocation,
166             Object fallback, Object context) {
167 
168         IOException thOrigin[] = new IOException[1];
169 
170         ClassLoader cl = Thread.currentThread().getContextClassLoader();
171         if (cl != null) {
172             URL url = null;
173             try {
174                 url = cl.getResource(resourceLocation);
175 
176             } catch (RuntimeException ex) {
177                 LOG.debug("Resource not found '" + resourceLocation + "'.", ex);
178             }
179 
180             if (url != null && testURL(url, thOrigin)) {
181                 return url;
182             }
183         }
184 
185         if (fallback != null) {
186             try {
187                 URL url = fallback.getClass().getClassLoader()
188                         .getResource(resourceLocation);
189                 if (url != null && testURL(url, thOrigin)) {
190                     return url;
191                 }
192 
193             } catch (RuntimeException ex) {
194                 LOG.debug("Resource not found '" + resourceLocation + "'.", ex);
195             }
196         }
197 
198         try {
199             URL url = ClassLocator.class.getClassLoader().getResource(
200                     resourceLocation);
201 
202             if (url != null && testURL(url, thOrigin)) {
203                 return url;
204             }
205 
206         } catch (RuntimeException ex) {
207             LOG.debug("Resource not found '" + resourceLocation + "'.", ex);
208         }
209 
210         if (context instanceof FacesContext) {
211             context = ((FacesContext) context).getExternalContext()
212                     .getContext();
213         }
214 
215         if (context instanceof ServletContext) {
216             try {
217                 URL url = ((ServletContext) context)
218                         .getResource(resourceLocation);
219                 if (url != null && testURL(url, thOrigin)) {
220                     return url;
221                 }
222 
223             } catch (MalformedURLException ex) {
224                 LOG.debug("Malformed URL for '" + resourceLocation + "'.", ex);
225 
226             } catch (RuntimeException ex) {
227                 LOG.debug("Resource not found '" + resourceLocation + "'.", ex);
228             }
229 
230             try {
231                 context = ((ServletContext) context).getClass()
232                         .getClassLoader();
233 
234             } catch (RuntimeException ex) {
235                 LOG.debug("Can not get classLoader of ServletContext", ex);
236             }
237         }
238 
239         if (context instanceof ClassLoader) {
240             try {
241                 URL url = ((ClassLoader) context).getResource(resourceLocation);
242 
243                 if (url != null && testURL(url, thOrigin)) {
244                     return url;
245                 }
246 
247             } catch (RuntimeException ex) {
248                 LOG.debug("Resource not found '" + resourceLocation + "'.", ex);
249             }
250         }
251 
252         if (thOrigin[0] == null) {
253             thOrigin[0] = new IOException("Can not find resource '"
254                     + resourceLocation + "'.");
255         }
256 
257         return null;
258     }
259 
260     protected static boolean testURL(URL url, IOException exs[]) {
261         if (url == null) {
262             return false;
263         }
264         try {
265             InputStream ins = url.openStream();
266 
267             ins.close();
268 
269             return true;
270 
271         } catch (IOException ex) {
272             if (exs != null && exs[0] == null) {
273                 exs[0] = ex;
274             }
275         }
276 
277         return false;
278     }
279 }