KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34
35
36 /**
37  * @author Teodor Danciu (teodord@users.sourceforge.net)
38  * @version $Id: JRClassLoader.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
39  */

40 public class JRClassLoader extends ClassLoader JavaDoc
41 {
42
43     /**
44      *
45      */

46     protected JRClassLoader()
47     {
48         super();
49     }
50
51     /**
52      *
53      */

54     protected JRClassLoader(ClassLoader JavaDoc parent)
55     {
56         super(parent);
57     }
58
59
60     /**
61      *
62      */

63     public static Class JavaDoc loadClassForName(String JavaDoc className) throws ClassNotFoundException JavaDoc
64     {
65         Class JavaDoc clazz = null;
66
67         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
68         if (classLoader != null)
69         {
70             try
71             {
72                 clazz = Class.forName(className, true, classLoader);
73             }
74             catch (ClassNotFoundException JavaDoc e)
75             {
76                 //if (log.isWarnEnabled())
77
// log.warn("Failure using Thread.currentThread().getContextClassLoader() in JRClassLoader class. Using JRClassLoader.class.getClassLoader() instead.");
78
}
79         }
80
81         if (clazz == null)
82         {
83             classLoader = JRClassLoader.class.getClassLoader();
84             if (classLoader == null)
85             {
86                 clazz = Class.forName(className);
87             }
88             else
89             {
90                 clazz = Class.forName(className, true, classLoader);
91             }
92         }
93
94         return clazz;
95     }
96
97
98     /**
99      * @deprecated To be removed in future versions.
100      */

101     public static Class JavaDoc loadClassFromFile(String JavaDoc className, File JavaDoc file) throws IOException JavaDoc
102     {
103         Class JavaDoc clazz = null;
104
105         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
106         if (classLoader != null)
107         {
108             try
109             {
110                 clazz =
111                     (new JRClassLoader(classLoader))
112                         .loadClass(className, file);
113             }
114             catch(NoClassDefFoundError JavaDoc e)
115             {
116                 //if (log.isWarnEnabled())
117
// log.warn("Failure using Thread.currentThread().getContextClassLoader() in JRClassLoader class. Using JRClassLoader.class.getClassLoader() instead.");
118
}
119         }
120     
121         if (clazz == null)
122         {
123             classLoader = JRClassLoader.class.getClassLoader();
124             if (classLoader == null)
125             {
126                 clazz =
127                     (new JRClassLoader())
128                         .loadClass(className, file);
129             }
130             else
131             {
132                 clazz =
133                     (new JRClassLoader(classLoader))
134                         .loadClass(className, file);
135             }
136         }
137         
138         return clazz;
139     }
140
141
142     /**
143      *
144      */

145     public static Class JavaDoc loadClassFromBytes(String JavaDoc className, byte[] bytecodes)
146     {
147         Class JavaDoc clazz = null;
148
149         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
150         if (classLoader != null)
151         {
152             try
153             {
154                 clazz =
155                     (new JRClassLoader(classLoader))
156                         .loadClass(className, bytecodes);
157             }
158             catch(NoClassDefFoundError JavaDoc e)
159             {
160                 //if (log.isWarnEnabled())
161
// log.warn("Failure using Thread.currentThread().getContextClassLoader() in JRClassLoader class. Using JRClassLoader.class.getClassLoader() instead.");
162
}
163         }
164     
165         if (clazz == null)
166         {
167             classLoader = JRClassLoader.class.getClassLoader();
168             if (classLoader == null)
169             {
170                 clazz =
171                     (new JRClassLoader())
172                         .loadClass(className, bytecodes);
173             }
174             else
175             {
176                 clazz =
177                     (new JRClassLoader(classLoader))
178                         .loadClass(className, bytecodes);
179             }
180         }
181
182         return clazz;
183     }
184
185
186     /**
187      * @deprecated To be removed in future versions.
188      */

189     protected Class JavaDoc loadClass(String JavaDoc className, File JavaDoc file) throws IOException JavaDoc
190     {
191         FileInputStream JavaDoc fis = null;
192         ByteArrayOutputStream JavaDoc baos = null;
193
194         byte[] bytecodes = new byte[10000];
195         int ln = 0;
196
197         try
198         {
199             fis = new FileInputStream JavaDoc(file);
200             baos = new ByteArrayOutputStream JavaDoc();
201
202             while ( (ln = fis.read(bytecodes)) > 0 )
203             {
204                 baos.write(bytecodes, 0, ln);
205             }
206
207             baos.flush();
208         }
209         finally
210         {
211             if (baos != null)
212             {
213                 try
214                 {
215                     baos.close();
216                 }
217                 catch(IOException JavaDoc e)
218                 {
219                 }
220             }
221
222             if (fis != null)
223             {
224                 try
225                 {
226                     fis.close();
227                 }
228                 catch(IOException JavaDoc e)
229                 {
230                 }
231             }
232         }
233
234         return loadClass(className, baos.toByteArray());
235     }
236
237
238     /**
239      *
240      */

241     protected Class JavaDoc loadClass(String JavaDoc className, byte[] bytecodes)
242     {
243         Class JavaDoc clazz = null;
244
245         clazz =
246             defineClass(
247                 className,
248                 bytecodes,
249                 0,
250                 bytecodes.length,
251                 JRClassLoader.class.getProtectionDomain()
252                 );
253
254         return clazz;
255     }
256
257
258 }
259
Popular Tags