KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.URLStreamHandlerFactory JavaDoc;
41
42 import net.sf.jasperreports.engine.JRException;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  * @version $Id: JRLoader.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
48  */

49 public class JRLoader
50 {
51
52
53     /**
54      *
55      */

56     //private static boolean wasWarning = false;
57

58
59     /**
60      *
61      */

62     public static Object JavaDoc loadObject(String JavaDoc fileName) throws JRException
63     {
64         return loadObject(new File JavaDoc(fileName));
65     }
66
67
68     /**
69      *
70      */

71     public static Object JavaDoc loadObject(File JavaDoc file) throws JRException
72     {
73         if (!file.exists() || !file.isFile())
74         {
75             throw new JRException( new FileNotFoundException JavaDoc(String.valueOf(file)) );
76         }
77
78         Object JavaDoc obj = null;
79
80         FileInputStream JavaDoc fis = null;
81         ObjectInputStream JavaDoc ois = null;
82
83         try
84         {
85             fis = new FileInputStream JavaDoc(file);
86             BufferedInputStream JavaDoc bufferedIn = new BufferedInputStream JavaDoc(fis);
87             ois = new ObjectInputStream JavaDoc(bufferedIn);
88             obj = ois.readObject();
89         }
90         catch (IOException JavaDoc e)
91         {
92             throw new JRException("Error loading object from file : " + file, e);
93         }
94         catch (ClassNotFoundException JavaDoc e)
95         {
96             throw new JRException("Class not found when loading object from file : " + file, e);
97         }
98         finally
99         {
100             if (ois != null)
101             {
102                 try
103                 {
104                     ois.close();
105                 }
106                 catch(IOException JavaDoc e)
107                 {
108                 }
109             }
110
111             if (fis != null)
112             {
113                 try
114                 {
115                     fis.close();
116                 }
117                 catch(IOException JavaDoc e)
118                 {
119                 }
120             }
121         }
122
123         return obj;
124     }
125
126
127     /**
128      *
129      */

130     public static Object JavaDoc loadObject(URL JavaDoc url) throws JRException
131     {
132         Object JavaDoc obj = null;
133
134         InputStream JavaDoc is = null;
135         ObjectInputStream JavaDoc ois = null;
136
137         try
138         {
139             is = url.openStream();
140             ois = new ObjectInputStream JavaDoc(is);
141             obj = ois.readObject();
142         }
143         catch (IOException JavaDoc e)
144         {
145             throw new JRException("Error loading object from URL : " + url, e);
146         }
147         catch (ClassNotFoundException JavaDoc e)
148         {
149             throw new JRException("Class not found when loading object from URL : " + url, e);
150         }
151         finally
152         {
153             if (ois != null)
154             {
155                 try
156                 {
157                     ois.close();
158                 }
159                 catch(IOException JavaDoc e)
160                 {
161                 }
162             }
163
164             if (is != null)
165             {
166                 try
167                 {
168                     is.close();
169                 }
170                 catch(IOException JavaDoc e)
171                 {
172                 }
173             }
174         }
175
176         return obj;
177     }
178
179
180     /**
181      *
182      */

183     public static Object JavaDoc loadObject(InputStream JavaDoc is) throws JRException
184     {
185         Object JavaDoc obj = null;
186
187         ObjectInputStream JavaDoc ois = null;
188
189         try
190         {
191             ois = new ObjectInputStream JavaDoc(is);
192             obj = ois.readObject();
193         }
194         catch (IOException JavaDoc e)
195         {
196             throw new JRException("Error loading object from InputStream", e);
197         }
198         catch (ClassNotFoundException JavaDoc e)
199         {
200             throw new JRException("Class not found when loading object from InputStream", e);
201         }
202         finally
203         {
204             //FIXMENOW should not close the stream
205
if (ois != null)
206             {
207                 try
208                 {
209                     ois.close();
210                 }
211                 catch(IOException JavaDoc e)
212                 {
213                 }
214             }
215         }
216
217         return obj;
218     }
219
220
221     /**
222      *
223      */

224     public static Object JavaDoc loadObjectFromLocation(String JavaDoc location) throws JRException
225     {
226         return loadObjectFromLocation(location, null, null);
227     }
228
229
230     /**
231      *
232      */

233     public static Object JavaDoc loadObjectFromLocation(String JavaDoc location, ClassLoader JavaDoc classLoader) throws JRException
234     {
235         return loadObjectFromLocation(location, classLoader, null);
236     }
237
238     
239     /**
240      *
241      */

242     public static Object JavaDoc loadObjectFromLocation(
243         String JavaDoc location,
244         ClassLoader JavaDoc classLoader,
245         URLStreamHandlerFactory JavaDoc urlHandlerFactory
246         ) throws JRException
247     {
248         URL JavaDoc url = JRResourcesUtil.createURL(location, urlHandlerFactory);
249         if (url != null)
250         {
251             return loadObject(url);
252         }
253
254         File JavaDoc file = new File JavaDoc(location);
255         if (file.exists() && file.isFile())
256         {
257             return loadObject(file);
258         }
259
260         url = JRResourcesUtil.findClassLoaderResource(location, classLoader, JRLoader.class);
261         if (url != null)
262         {
263             return loadObject(url);
264         }
265
266         throw new JRException("Could not load object from location : " + location);
267     }
268
269
270     /**
271      *
272      */

273     public static byte[] loadBytes(File JavaDoc file) throws JRException
274     {
275         ByteArrayOutputStream JavaDoc baos = null;
276         FileInputStream JavaDoc fis = null;
277
278         try
279         {
280             fis = new FileInputStream JavaDoc(file);
281             baos = new ByteArrayOutputStream JavaDoc();
282
283             byte[] bytes = new byte[10000];
284             int ln = 0;
285             while ((ln = fis.read(bytes)) > 0)
286             {
287                 baos.write(bytes, 0, ln);
288             }
289
290             baos.flush();
291         }
292         catch (IOException JavaDoc e)
293         {
294             throw new JRException("Error loading byte data : " + file, e);
295         }
296         finally
297         {
298             if (baos != null)
299             {
300                 try
301                 {
302                     baos.close();
303                 }
304                 catch(IOException JavaDoc e)
305                 {
306                 }
307             }
308
309             if (fis != null)
310             {
311                 try
312                 {
313                     fis.close();
314                 }
315                 catch(IOException JavaDoc e)
316                 {
317                 }
318             }
319         }
320
321         return baos.toByteArray();
322     }
323
324
325     /**
326      *
327      */

328     public static byte[] loadBytes(URL JavaDoc url) throws JRException
329     {
330         ByteArrayOutputStream JavaDoc baos = null;
331         InputStream JavaDoc is = null;
332
333         try
334         {
335             is = url.openStream();
336             baos = new ByteArrayOutputStream JavaDoc();
337
338             byte[] bytes = new byte[10000];
339             int ln = 0;
340             while ((ln = is.read(bytes)) > 0)
341             {
342                 baos.write(bytes, 0, ln);
343             }
344
345             baos.flush();
346         }
347         catch (IOException JavaDoc e)
348         {
349             throw new JRException("Error loading byte data : " + url, e);
350         }
351         finally
352         {
353             if (baos != null)
354             {
355                 try
356                 {
357                     baos.close();
358                 }
359                 catch(IOException JavaDoc e)
360                 {
361                 }
362             }
363
364             if (is != null)
365             {
366                 try
367                 {
368                     is.close();
369                 }
370                 catch(IOException JavaDoc e)
371                 {
372                 }
373             }
374         }
375
376         return baos.toByteArray();
377     }
378
379
380     /**
381      *
382      */

383     public static byte[] loadBytes(InputStream JavaDoc is) throws JRException
384     {
385         ByteArrayOutputStream JavaDoc baos = null;
386
387         try
388         {
389             baos = new ByteArrayOutputStream JavaDoc();
390
391             byte[] bytes = new byte[10000];
392             int ln = 0;
393             while ((ln = is.read(bytes)) > 0)
394             {
395                 baos.write(bytes, 0, ln);
396             }
397
398             baos.flush();
399         }
400         catch (IOException JavaDoc e)
401         {
402             throw new JRException("Error loading byte data from input stream.", e);
403         }
404         finally
405         {
406             if (baos != null)
407             {
408                 try
409                 {
410                     baos.close();
411                 }
412                 catch(IOException JavaDoc e)
413                 {
414                 }
415             }
416         }
417
418         return baos.toByteArray();
419     }
420
421
422     /**
423      *
424      */

425     public static byte[] loadBytesFromLocation(String JavaDoc location) throws JRException
426     {
427         return loadBytesFromLocation(location, null, null);
428     }
429
430
431     /**
432      *
433      */

434     public static byte[] loadBytesFromLocation(String JavaDoc location, ClassLoader JavaDoc classLoader) throws JRException
435     {
436         return loadBytesFromLocation(location, classLoader, null);
437     }
438         
439     
440     /**
441      *
442      */

443     public static byte[] loadBytesFromLocation(
444         String JavaDoc location,
445         ClassLoader JavaDoc classLoader,
446         URLStreamHandlerFactory JavaDoc urlHandlerFactory
447         ) throws JRException
448     {
449         URL JavaDoc url = JRResourcesUtil.createURL(location, urlHandlerFactory);
450         if (url != null)
451         {
452             return loadBytes(url);
453         }
454
455         File JavaDoc file = new File JavaDoc(location);
456         if (file.exists() && file.isFile())
457         {
458             return loadBytes(file);
459         }
460
461         url = JRResourcesUtil.findClassLoaderResource(location, classLoader, JRLoader.class);
462         if (url != null)
463         {
464             return loadBytes(url);
465         }
466
467         throw new JRException("Byte data not found at location : " + location);
468     }
469
470
471     /**
472      * Tries to open an input stream for a location.
473      * <p>
474      * The method tries to interpret the location as a file name, a resource name or
475      * an URL. If any of these succeed, an input stream is created and returned.
476      *
477      * @param location the location
478      * @return an input stream if the location is an existing file name, a resource name on
479      * the classpath or an URL or <code>null</code> otherwise.
480      *
481      * @throws JRException
482      */

483     public static InputStream JavaDoc getLocationInputStream(String JavaDoc location) throws JRException
484     {
485         InputStream JavaDoc is = null;
486         
487         is = getResourceInputStream(location);
488         
489         if (is == null)
490         {
491             is = getFileInputStream(location);
492         }
493         
494         if (is == null)
495         {
496             is = getURLInputStream(location);
497         }
498         
499         return is;
500     }
501
502
503     /**
504      * Tries to open a file for reading.
505      *
506      * @param filename the file name
507      * @return an input stream for the file or <code>null</code> if the file was not found
508      * @throws JRException
509      */

510     public static InputStream JavaDoc getFileInputStream(String JavaDoc filename) throws JRException
511     {
512         InputStream JavaDoc is = null;
513         
514         File JavaDoc file = new File JavaDoc(filename);
515         if (file.exists() && file.isFile())
516         {
517             try
518             {
519                 is = new FileInputStream JavaDoc(file);
520             }
521             catch (FileNotFoundException JavaDoc e)
522             {
523                 throw new JRException("Error opening file " + filename);
524             }
525         }
526         
527         return is;
528     }
529
530
531     /**
532      * Tries to open an input stream for a resource.
533      *
534      * @param resource the resource name
535      * @return an input stream for the resource or <code>null</code> if the resource was not found
536      */

537     public static InputStream JavaDoc getResourceInputStream(String JavaDoc resource)
538     {
539         InputStream JavaDoc is = null;
540         
541         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
542         if (classLoader != null)
543         {
544             is = classLoader.getResourceAsStream(resource);
545         }
546         
547         if (is == null)
548         {
549             classLoader = JRLoader.class.getClassLoader();
550             if (classLoader != null)
551             {
552                 is = classLoader.getResourceAsStream(resource);
553             }
554             
555             if (is == null)
556             {
557                 is = JRProperties.class.getResourceAsStream("/" + resource);
558             }
559         }
560
561         return is;
562     }
563
564
565     /**
566      * Tries to open an input stream for an URL.
567      *
568      * @param spec the string to parse as an URL
569      * @return an input stream for the URL or null if <code>spec</code> is not a valid URL
570      * @throws JRException
571      */

572     public static InputStream JavaDoc getURLInputStream(String JavaDoc spec) throws JRException
573     {
574         InputStream JavaDoc is = null;
575         
576         try
577         {
578             URL JavaDoc url = new URL JavaDoc(spec);
579             is = url.openStream();
580         }
581         catch (MalformedURLException JavaDoc e)
582         {
583         }
584         catch (IOException JavaDoc e)
585         {
586             throw new JRException("Error opening URL " + spec);
587         }
588         
589         return is;
590     }
591 }
592
Popular Tags