1 32 33 package it.businesslogic.ireport; 34 35 import java.io.ByteArrayOutputStream ; 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.net.URL ; 41 import java.net.URLClassLoader ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.StringTokenizer ; 45 import java.util.zip.ZipEntry ; 46 import java.util.zip.ZipFile ; 47 import java.util.*; 48 49 56 public class ReportClassLoader extends java.lang.ClassLoader { 57 58 59 private ArrayList fPathItems; 60 61 62 private ArrayList fPathChachedItems; 63 private HashMap cachedClasses; 64 65 66 67 80 public ReportClassLoader() { 81 super( ReportClassLoader.class.getClassLoader() ); 82 setup(); 83 } 84 85 public ReportClassLoader(ClassLoader parent) { 86 87 super(parent); 88 setup(); 89 } 90 91 private void setup() 92 { 93 fPathItems = new ArrayList (); 94 fPathChachedItems = new ArrayList (); 95 cachedClasses = new HashMap (); 96 rescanLibDirectory(); 97 } 98 99 102 public void rescanLibDirectory() 103 { 104 try { 105 rescanAdditionalClasspath(); 106 } catch (Exception ex) {} 107 108 if (it.businesslogic.ireport.gui.MainFrame.getMainInstance() == null) return; 109 String irHome = it.businesslogic.ireport.gui.MainFrame.getMainInstance().IREPORT_HOME_DIR; 111 if (irHome == null) irHome = System.getProperty("ireport.home","."); 112 File lib_dir = new File (irHome,"lib"); 113 String classpath = it.businesslogic.ireport.util.Misc.nvl(System.getProperty("java.class.path"),""); 114 if (!lib_dir.exists()) 115 { 116 System.out.println("Cannot find lib in iReport home directory ("+it.businesslogic.ireport.gui.MainFrame.getMainInstance().IREPORT_HOME_DIR+")"); 117 return; 118 } 119 120 File [] new_libs = lib_dir.listFiles(); 122 123 for (int i=0; i< new_libs.length; ++i) 124 { 125 if (!new_libs[i].getName().toLowerCase().endsWith("jar") && 126 !new_libs[i].getName().toLowerCase().endsWith("zip")) continue; 127 128 if (classpath.indexOf( new_libs[i].getName()) < 0) 129 { 130 try { 131 if (!fPathChachedItems.contains(new_libs[i].getCanonicalPath())) 132 { 133 fPathChachedItems.add(new_libs[i].getCanonicalPath()); 137 } 139 } catch (Exception ex) 140 { 141 System.out.println("Invalid path: " + new_libs[i]); 142 } 143 } 144 } 145 } 146 147 public java.util.List getCachedItems() 148 { 149 return fPathChachedItems; 150 } 151 152 public void clearCache() 153 { 154 cachedClasses.clear(); 155 } 157 158 161 public void rescanAdditionalClasspath() 162 { 163 if (it.businesslogic.ireport.gui.MainFrame.getMainInstance() == null) return; 164 Vector cp = it.businesslogic.ireport.gui.MainFrame.getMainInstance().getClasspath(); 166 for (int i=0; i<cp.size(); ++i) 167 { 168 File f = new File ( cp.elementAt(i) + "" ); 169 if (!f.exists()) continue; 170 try { 171 if (!fPathChachedItems.contains(f.getCanonicalPath())) 172 { 173 fPathChachedItems.add(f.getCanonicalPath()); 175 } 176 } catch (Exception ex) 177 { 178 System.out.println("Invalid path: " + f); 179 } 180 181 182 } 183 } 184 185 188 public void addNoRelodablePath(String path) 189 { 190 if (!fPathChachedItems.contains(path)) 191 { 192 fPathChachedItems.add(path); 193 } 194 } 195 196 public void setRelodablePaths(String classPath) 197 { 198 scanPath(classPath); 199 } 200 201 private void scanPath(String classPath) { 202 String separator = System.getProperty("path.separator"); 203 fPathItems = new ArrayList (31); 204 StringTokenizer st = new StringTokenizer (classPath, separator); 205 while (st.hasMoreTokens()) { 206 String pp = st.nextToken(); 207 fPathItems.add(pp); 209 } 210 } 211 212 public URL getResource(String name) { 213 return this.findResource(name); 215 283 } 284 285 public InputStream getResourceAsStream(String name) { 286 287 for (int i = 0; i < fPathChachedItems.size(); i++) { 289 String path = (String ) fPathChachedItems.get(i); 290 291 if (isJar(path)) 292 { 293 InputStream is = getInputStreamFromJar(path, name); 294 if (is != null) return is; 295 } 296 else 297 { 298 File f = new File (path, name); 299 if (f.exists()) 300 { 301 try { 302 return new FileInputStream ( f ); 303 } catch (Exception ex) 304 {} 305 } 306 } 307 } 308 309 for (int i = 0; i < fPathItems.size(); i++) { 311 312 String path = (String ) fPathItems.get(i); 313 314 if (isJar(path)) 315 { 316 InputStream is = getInputStreamFromJar(path, name); 317 if (is != null) return is; 318 } 319 else 320 { 321 File f = new File (path, name); 322 if (f.exists()) 323 { 324 try { 325 return new FileInputStream ( f ); 326 } catch (Exception ex) 327 {} 328 } 329 } 330 } 331 332 return ClassLoader.getSystemResourceAsStream(name); 333 } 334 335 private InputStream getInputStreamFromJar(String archive_path, String fileName) { 336 ZipFile zipFile = null; 337 InputStream stream = null; 338 File archive = new File (archive_path); 339 if (!archive.exists()) 340 { 341 return null; 343 } 344 try { 345 zipFile = new ZipFile (archive); 346 } catch (IOException io) { 347 return null; 349 } 350 351 ZipEntry entry = zipFile.getEntry(fileName); 353 if (entry == null) 354 { 355 return null; 357 } 358 try { 359 return zipFile.getInputStream(entry); 360 } catch (IOException e) { 361 } finally { 362 } 363 364 return null; 365 } 366 367 368 public synchronized Class findClass(String name) throws ClassNotFoundException { 369 370 Class c = null; 371 372 if (cachedClasses.containsKey( name )) 373 { 374 c = (Class )cachedClasses.get(name); 375 376 } 377 else 378 { 379 c = loadClassData(name); 380 } 381 382 383 return c; 384 392 } 393 394 private Class loadClassData(String className) 397 throws ClassNotFoundException { 398 399 401 403 byte[] data = null; 404 405 if (!cachedClasses.containsKey(className)) 406 { 407 408 for (int i = 0; i < fPathChachedItems.size(); i++) { 409 410 411 String path = (String ) fPathChachedItems.get(i); 412 String fileName = className.replace('.', File.separatorChar) + ".class"; 413 414 415 if (isJar(path)) { 416 data = loadJarData(path, fileName.replace(File.separatorChar,'/')); 418 } else { 419 data = loadFileData(path, fileName.replace(File.separatorChar,'/')); 421 } 422 if (data != null) 423 { 424 Class c = defineClass(className, data, 0, data.length); 425 cachedClasses.put(className,c); 426 427 return c; 428 } 429 } 430 } 431 else 432 { 433 return (Class )cachedClasses.get(className); 434 } 435 436 for (int i = 0; i < fPathItems.size(); i++) { 438 439 440 String path = (String ) fPathItems.get(i); 441 String fileName = className.replace('.', File.separatorChar) + ".class"; 442 443 if (isJar(path)) { 444 data = loadJarData(path, fileName); 445 } else { 446 data = loadFileData(path, fileName); 447 } 448 if (data != null) 449 { 450 Class c = defineClass(className, data, 0, data.length); 451 return c; 452 } 453 } 454 455 throw new ClassNotFoundException (className); 456 } 457 458 boolean isJar(String pathEntry) { 459 return pathEntry.toLowerCase().endsWith(".jar") || pathEntry.toLowerCase().endsWith(".zip"); 460 } 461 462 private byte[] loadFileData(String path, String fileName) { 463 File file = new File (path, fileName); 464 if (file.exists()) { 466 return getClassData(file); 467 } 468 return null; 469 } 470 471 private byte[] getClassData(File f) { 472 try { 473 FileInputStream stream = new FileInputStream (f); 474 ByteArrayOutputStream out = new ByteArrayOutputStream (1000); 475 byte[] b = new byte[1000]; 476 int n; 477 while ((n = stream.read(b)) != -1) 478 out.write(b, 0, n); 479 stream.close(); 480 out.close(); 481 return out.toByteArray(); 482 483 } catch (IOException e) { 484 } 485 return null; 486 } 487 488 private byte[] loadJarData(String path, String fileName) { 489 ZipFile zipFile = null; 490 InputStream stream = null; 491 File archive = new File (path); 492 if (!archive.exists()) 493 { 494 return null; 496 } 497 try { 498 zipFile = new ZipFile (archive); 499 } catch (IOException io) { 500 return null; 502 } 503 504 ZipEntry entry = zipFile.getEntry(fileName); 506 if (entry == null) 507 { 508 return null; 510 } 511 int size = (int) entry.getSize(); 512 try { 513 stream = zipFile.getInputStream(entry); 514 byte[] data = new byte[size]; 515 int pos = 0; 516 while (pos < size) { 517 int n = stream.read(data, pos, data.length - pos); 518 pos += n; 519 } 520 zipFile.close(); 521 return data; 522 } catch (IOException e) { 523 } finally { 524 try { 525 if (stream != null) 526 stream.close(); 527 } catch (IOException e) { 528 } 530 } 531 return null; 533 } 534 535 536 public Enumeration findResources(String name) { 537 Vector urls = new Vector(); 539 540 URL [] pathUrls = new URL [fPathChachedItems.size() + fPathItems.size()]; 541 542 for (int i = 0; i < fPathChachedItems.size(); i++) { 543 String path = (String ) fPathChachedItems.get(i); 544 try { 545 pathUrls[i] = (new File (path)).toURL(); 546 } catch (Exception ex) 547 { 548 } 549 } 550 551 for (int i = 0; i < fPathItems.size(); i++) { 552 String path = (String ) fPathItems.get(i); 553 try { 554 pathUrls[i + fPathChachedItems.size()] = (new File (path)).toURL(); 555 } catch (Exception ex) 556 { 557 } 558 } 559 560 URLClassLoader urlCl = new URLClassLoader (pathUrls, null); 561 try { 562 return urlCl.findResources(name); 563 } catch (Exception ex) 564 { 565 566 } 567 568 return new Vector().elements(); 569 570 } 571 572 public URL findResource(String name) { 573 574 if (name.startsWith("/")) name = name.substring(1); 575 URL [] pathUrls = new URL [fPathChachedItems.size() + fPathItems.size()]; 576 577 for (int i = 0; i < fPathChachedItems.size(); i++) { 578 String path = (String ) fPathChachedItems.get(i); 579 try { 580 pathUrls[i] = (new File (path)).toURL(); 581 } catch (Exception ex) 582 { 583 } 584 } 585 586 for (int i = 0; i < fPathItems.size(); i++) { 587 String path = (String ) fPathItems.get(i); 588 try { 589 pathUrls[i + fPathChachedItems.size()] = (new File (path)).toURL(); 590 } catch (Exception ex) 591 { 592 } 593 } 594 595 URLClassLoader urlCl = new URLClassLoader (pathUrls, null); 596 try { 597 URL url = urlCl.findResource(name); 598 if (url != null) return url; 600 } catch (Exception ex) 601 { 602 ex.printStackTrace(); 603 } 604 605 return ClassLoader.getSystemResource(name); 606 607 } 608 } 609 | Popular Tags |