1 28 package net.sf.jasperreports.engine.util; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.IOException ; 34 35 36 40 public class JRClassLoader extends ClassLoader 41 { 42 43 46 protected JRClassLoader() 47 { 48 super(); 49 } 50 51 54 protected JRClassLoader(ClassLoader parent) 55 { 56 super(parent); 57 } 58 59 60 63 public static Class loadClassForName(String className) throws ClassNotFoundException 64 { 65 Class clazz = null; 66 67 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 68 if (classLoader != null) 69 { 70 try 71 { 72 clazz = Class.forName(className, true, classLoader); 73 } 74 catch (ClassNotFoundException e) 75 { 76 } 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 101 public static Class loadClassFromFile(String className, File file) throws IOException 102 { 103 Class clazz = null; 104 105 ClassLoader 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 e) 115 { 116 } 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 145 public static Class loadClassFromBytes(String className, byte[] bytecodes) 146 { 147 Class clazz = null; 148 149 ClassLoader 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 e) 159 { 160 } 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 189 protected Class loadClass(String className, File file) throws IOException 190 { 191 FileInputStream fis = null; 192 ByteArrayOutputStream baos = null; 193 194 byte[] bytecodes = new byte[10000]; 195 int ln = 0; 196 197 try 198 { 199 fis = new FileInputStream (file); 200 baos = new ByteArrayOutputStream (); 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 e) 218 { 219 } 220 } 221 222 if (fis != null) 223 { 224 try 225 { 226 fis.close(); 227 } 228 catch(IOException e) 229 { 230 } 231 } 232 } 233 234 return loadClass(className, baos.toByteArray()); 235 } 236 237 238 241 protected Class loadClass(String className, byte[] bytecodes) 242 { 243 Class 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 |