KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRResourcesUtil


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLStreamHandler JavaDoc;
33 import java.net.URLStreamHandlerFactory JavaDoc;
34
35
36 /**
37  * Provides methods for resource resolution via class loaders or URL stream handlers.
38  *
39  * @author Lucian Chirita (lucianc@users.sourceforge.net)
40  * @version $Id: JRResourcesUtil.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
41  */

42 public class JRResourcesUtil
43 {
44     private static URLStreamHandlerFactory JavaDoc globalURLHandlerFactory;
45     private static ThreadLocalStack localURLHandlerFactoryStack = new ThreadLocalStack();
46
47     private static ClassLoader JavaDoc globalClassLoader;
48     private static ThreadLocalStack localClassLoaderStack = new ThreadLocalStack();
49
50     
51     /**
52      * Tries to parse a <code>String</code> as an URL.
53      *
54      * @param spec the <code>String</code> to parse
55      * @param urlHandlerFactory an URL stream handler factory to use
56      * @return an URL if the parsing is successful
57      * @see #getURLHandler(String, URLStreamHandlerFactory)
58      * @see #getURLHandlerFactory(URLStreamHandlerFactory)
59      */

60     public static URL JavaDoc createURL(String JavaDoc spec, URLStreamHandlerFactory JavaDoc urlHandlerFactory)
61     {
62         URLStreamHandler JavaDoc handler = getURLHandler(spec, urlHandlerFactory);
63         URL JavaDoc url;
64         try
65         {
66             if (handler == null)
67             {
68                 url = new URL JavaDoc(spec);
69             }
70             else
71             {
72                 url = new URL JavaDoc(null, spec, handler);
73             }
74         }
75         catch (MalformedURLException JavaDoc e)
76         {
77             url = null;
78         }
79         return url;
80     }
81     
82     
83     /**
84      * Returns an URL stream handler for an URL specified as a <code>String</code>.
85      *
86      * @param spec the <code>String</code> to parse as an URL
87      * @param urlHandlerFactory an URL stream handler factory
88      * @return an URL stream handler if one was found for the protocol of the URL
89      * @see #getURLHandlerFactory(URLStreamHandlerFactory)
90      */

91     public static URLStreamHandler JavaDoc getURLHandler(String JavaDoc spec, URLStreamHandlerFactory JavaDoc urlHandlerFactory)
92     {
93         urlHandlerFactory = getURLHandlerFactory(urlHandlerFactory);
94
95         URLStreamHandler JavaDoc handler = null;
96         if (urlHandlerFactory != null)
97         {
98             String JavaDoc protocol = getURLProtocol(spec);
99             if (protocol != null)
100             {
101                 handler = urlHandlerFactory.createURLStreamHandler(protocol);
102             }
103         }
104         return handler;
105     }
106
107     
108     private static String JavaDoc getURLProtocol(String JavaDoc spec)
109     {
110         String JavaDoc protocol = null;
111         
112         spec = spec.trim();
113         int colon = spec.indexOf(':');
114         if (colon > 0)
115         {
116             String JavaDoc proto = spec.substring(0, colon);
117             if (protocolValid(proto))
118             {
119                 protocol = proto;
120             }
121         }
122         
123         return protocol;
124     }
125
126     private static boolean protocolValid(String JavaDoc protocol)
127     {
128         int length = protocol.length();
129         if (length < 1)
130         {
131             return false;
132         }
133         
134         if (!Character.isLetter(protocol.charAt(0)))
135         {
136             return false;
137         }
138         
139         for (int i = 1; i < length; ++i)
140         {
141             char c = protocol.charAt(i);
142             if (!(Character.isLetterOrDigit(c) || c == '+' || c == '-' || c == '.'))
143             {
144                 return false;
145             }
146         }
147         
148         return true;
149     }
150
151     
152     /**
153      * Returns an URL steam handler factory.
154      * <p/>
155      * The first not null value from the following is returned:
156      * <ul>
157      * <li>the value of the parameter</li>
158      * <li>the thread local URL stream handler factory</li>
159      * <li>the global URL stream handler factory</li>
160      * </ul>
161      *
162      * @param urlHandlerFactory an URL steam handler factory that will be returned if not null
163      * @return an URL steam handler factory
164      * @see #setGlobalURLHandlerFactory(URLStreamHandlerFactory)
165      * @see #setThreadURLHandlerFactory(URLStreamHandlerFactory)
166      */

167     public static URLStreamHandlerFactory JavaDoc getURLHandlerFactory(URLStreamHandlerFactory JavaDoc urlHandlerFactory)
168     {
169         if (urlHandlerFactory == null)
170         {
171             urlHandlerFactory = getThreadURLStreamHandlerFactory();
172             if (urlHandlerFactory == null)
173             {
174                 urlHandlerFactory = globalURLHandlerFactory;
175             }
176         }
177         return urlHandlerFactory;
178     }
179
180     
181     /**
182      * Returns the global URL stream handler factory.
183      *
184      * @return the global URL stream handler factory
185      * @see #setGlobalURLHandlerFactory(URLStreamHandlerFactory)
186      */

187     public static URLStreamHandlerFactory JavaDoc getGlobalURLStreamHandlerFactory()
188     {
189         return globalURLHandlerFactory;
190     }
191
192     
193     /**
194      * Returns the thread local URL stream handler factory.
195      *
196      * @return the thread local URL stream handler factory.
197      * @see #setThreadURLHandlerFactory(URLStreamHandlerFactory)
198      */

199     public static URLStreamHandlerFactory JavaDoc getThreadURLStreamHandlerFactory()
200     {
201         return (URLStreamHandlerFactory JavaDoc) localURLHandlerFactoryStack.top();
202     }
203
204     
205     /**
206      * Sets the thread local URL stream handler factory.
207      *
208      * @param urlHandlerFactory an URL stream handler factory.
209      * @see #getURLHandlerFactory(URLStreamHandlerFactory)
210      * @see #resetThreadURLHandlerFactory()
211      */

212     public static void setThreadURLHandlerFactory(URLStreamHandlerFactory JavaDoc urlHandlerFactory)
213     {
214         localURLHandlerFactoryStack.push(urlHandlerFactory);
215     }
216     
217     
218     /**
219      * Resets the the thread local URL stream handler factory to its previous value.
220      */

221     public static void resetThreadURLHandlerFactory()
222     {
223         localURLHandlerFactoryStack.pop();
224     }
225
226     /**
227      * Sets a global URL stream handler facotry to be used for resource resolution.
228      *
229      * @param urlHandlerFactory the URL stream handler factory
230      * @see #getURLHandlerFactory(URLStreamHandlerFactory)
231      */

232     public static void setGlobalURLHandlerFactory(URLStreamHandlerFactory JavaDoc urlHandlerFactory)
233     {
234         globalURLHandlerFactory = urlHandlerFactory;
235     }
236
237     
238     /**
239      * Returns a class loader.
240      * <p/>
241      * The first not null value from the following is returned:
242      * <ul>
243      * <li>the value of the parameter</li>
244      * <li>the thread local class loader</li>
245      * <li>the global class loader</li>
246      * </ul>
247      *
248      * @param classLoader a class loader that will be returned if not null
249      * @return a class loader.
250      * @see #setGlobalClassLoader(ClassLoader)
251      * @see #setThreadClassLoader(ClassLoader)
252      */

253     public static ClassLoader JavaDoc getClassLoader(ClassLoader JavaDoc classLoader)
254     {
255         if (classLoader == null)
256         {
257             classLoader = getThreadClassLoader();
258             if (classLoader == null)
259             {
260                 classLoader = globalClassLoader;
261             }
262         }
263         return classLoader;
264     }
265
266     
267     /**
268      * Returns the global class loader.
269      *
270      * @return the global class loader.
271      * @see #setGlobalClassLoader(ClassLoader)
272      */

273     public static ClassLoader JavaDoc getGlobalClassLoader()
274     {
275         return globalClassLoader;
276     }
277
278     
279     /**
280      * Returns the thread local class loader.
281      *
282      * @return the thread local class loader.
283      * @see #setThreadClassLoader(ClassLoader)
284      */

285     public static ClassLoader JavaDoc getThreadClassLoader()
286     {
287         return (ClassLoader JavaDoc) localClassLoaderStack.top();
288     }
289
290
291     /**
292      * Sets the thread local class loader.
293      *
294      * @param classLoader a class loader
295      * @see #getClassLoader(ClassLoader)
296      * @see #resetThreadURLHandlerFactory()
297      */

298     public static void setThreadClassLoader(ClassLoader JavaDoc classLoader)
299     {
300         localClassLoaderStack.push(classLoader);
301     }
302
303     
304     /**
305      * Resets the the thread local class loader to its previous value.
306      */

307     public static void resetClassLoader()
308     {
309         localClassLoaderStack.pop();
310     }
311
312     
313     /**
314      * Sets a global class loader to be used for resource resolution.
315      *
316      * @param classLoader the class loader
317      * @see #getClassLoader(ClassLoader)
318      */

319     public static void setGlobalClassLoader(ClassLoader JavaDoc classLoader)
320     {
321         globalClassLoader = classLoader;
322     }
323     
324     
325     /**
326      * Attempts to find a resource using a class loader.
327      * <p/>
328      * The following sources are tried:
329      * <ul>
330      * <li>the class loader returned by {@link #getClassLoader(ClassLoader) <code>getClassLoader(classLoader)</code>}</li>
331      * <li>the context class loader</li>
332      * <li><code>clazz.getClassLoader()</code></li>
333      * <li><code>clazz.getResource()</code></li>
334      * </ul>
335      *
336      * @param location the resource name
337      * @param classLoader a class loader
338      * @param clazz a class
339      * @return the resource URL if found
340      */

341     public static URL JavaDoc findClassLoaderResource(String JavaDoc location, ClassLoader JavaDoc classLoader, Class JavaDoc clazz)
342     {
343         classLoader = getClassLoader(classLoader);
344         
345         URL JavaDoc url = null;
346         
347         if (classLoader != null)
348         {
349             url = classLoader.getResource(location);
350         }
351         
352         if (url == null)
353         {
354             classLoader = Thread.currentThread().getContextClassLoader();
355
356             if (classLoader != null)
357             {
358                 url = classLoader.getResource(location);
359             }
360
361             if (url == null)
362             {
363                 classLoader = clazz.getClassLoader();
364                 if (classLoader == null)
365                 {
366                     url = clazz.getResource("/" + location);
367                 }
368                 else
369                 {
370                     url = classLoader.getResource(location);
371                 }
372             }
373         }
374         
375         return url;
376     }
377 }
378
Popular Tags