1 21 22 package org.apache.derbyBuild; 23 24 import org.apache.derby.iapi.services.classfile.*; 25 import org.apache.derby.iapi.util.ByteArray; 26 import java.util.*; 27 import java.util.zip.*; 28 import java.io.*; 29 30 import java.io.*; 31 32 66 67 public class classlister { 68 69 protected String [] sets; 70 protected Hashtable foundClasses; 71 73 protected boolean cloudscapeOnly = false; 74 protected boolean portingOnly = false; 75 protected boolean ignoreWebLogic = false; 76 protected boolean verbose = false; 77 protected boolean skipJava = true; 78 protected boolean skipJavax = true; 79 protected boolean skipOrg = true; 80 protected boolean skipInformix = true; 81 protected boolean skipDB2 = true; 82 protected boolean skipDB2e = true; 83 protected boolean skipSun = true; 84 protected boolean showAll = false; 85 protected boolean keepRolling = false; 86 protected boolean showOne = false; 87 protected Hashtable masterClassList = new Hashtable(); 88 protected String classpath[] = null; 89 protected String outputfile; 90 protected Hashtable classpathHash; 91 protected int indent = 0; 92 protected int errorCount = 0; 93 protected PrintWriter pwOut; 94 protected PrintStream psOut; 95 96 protected boolean db2jtools; 97 protected boolean db2jdrda; 98 99 protected boolean keepDependencyHistory; 100 101 protected static final String [] propFiles = { 102 "messages.properties", 103 "instructions.properties", 104 "metadata.properties" 105 }; 106 107 public static void main(String args[]) throws IOException { 108 109 classlister me = new classlister(); 110 111 me.sets = args; 112 113 me.run(); 114 if (me.errorCount > 0) 115 { 116 System.out.println(me.errorCount + " errors encountered."); 117 System.exit(1); 118 } 119 } 120 121 public classlister() { 122 cloudscapeOnly = Boolean.getBoolean("cloudscapeOnly"); 123 portingOnly = Boolean.getBoolean("portingOnly"); 124 ignoreWebLogic = Boolean.getBoolean("ignoreWebLogic"); 125 verbose = Boolean.getBoolean("verbose"); 126 skipJava = ! Boolean.getBoolean("doJava"); 127 skipJavax = ! Boolean.getBoolean("doJavax"); 128 skipOrg = ! Boolean.getBoolean("doOrg"); 129 showAll = Boolean.getBoolean("showAll"); 130 showOne = Boolean.getBoolean("showOne"); 131 keepRolling = Boolean.getBoolean("keepRolling"); 132 outputfile = System.getProperty("outputfile"); 133 db2jtools = Boolean.getBoolean("db2jtools"); 134 db2jdrda = Boolean.getBoolean("db2jdrda"); 135 136 keepDependencyHistory = showOne || showAll; 137 } 138 139 public void run() 140 { 141 try 143 { 144 File outFile = new File(outputfile); 145 pwOut = new PrintWriter( new BufferedWriter 146 (new FileWriter(outFile.getPath()), 10000), true ); 147 } 148 catch (IOException ioe) 149 { 150 System.out.println(ioe); 151 System.exit(1); 152 } 153 154 loadClasspath(); 155 157 foundClasses = new Hashtable(3000, 0.8f); 158 159 for (int i = 0; i < sets.length; i++) 160 { 161 162 try 165 { 166 167 String s = sets[i]; 168 169 if (s.endsWith(".class")) { 170 171 findDependencies(s.substring(0, s.length() - 6)); 172 } else { 173 174 FileInputStream fis = new FileInputStream(s); 175 176 Properties pset = new Properties(); 177 178 pset.load(fis); 179 180 findClasses(pset); 181 } 182 183 } 184 catch (IOException ioe) 185 { 186 System.err.println(ioe.toString()); 187 System.exit(1); 188 } 189 } 190 if (pwOut == null) 191 { 192 System.out.println("Need to specify an outputfile"); 193 System.exit(1); 194 } 195 for (Enumeration e = foundClasses.keys(); e.hasMoreElements(); ) { 196 String name = (String ) e.nextElement(); 197 String type = (String ) foundClasses.get(name); 198 if (type.equals("class")) { 199 if (ignoreWebLogic) { 200 if (name.startsWith("weblogic")) { 201 continue; 202 } 203 } 204 205 206 if (isCloudscapeCode(name)) { 207 208 if (name.startsWith("com.ibm.db2j.porting.")) { 209 if (cloudscapeOnly) 210 continue; 211 } else { 212 if (portingOnly) 213 continue; 214 } 215 216 } else { 217 if (cloudscapeOnly || portingOnly) 218 continue; 219 } 220 pwOut.println(name.replace('.', '/') + ".class"); 221 } 222 else { 223 if (name.startsWith("com/ibm/db2j/")) { 225 if (portingOnly) { 226 continue; 227 } 228 } else { 229 if (cloudscapeOnly || portingOnly) 230 continue; 231 } 232 233 pwOut.println(name); 234 } 235 } 236 if (showAll) 237 { 238 showAllItems(); 239 } 240 if (showOne) 241 { 242 showAllItemsOneLevel(); 243 } 244 } 245 246 247 protected void findClasses(Properties pset) throws IOException { 248 249 for (Enumeration e = pset.propertyNames(); e.hasMoreElements(); ) { 250 String key = (String ) e.nextElement(); 251 if (key.startsWith("derby.module.")) { 252 if (verbose) { 253 pwOut.println(pset.getProperty(key) + " needs "); 254 } 255 findDependencies(pset.getProperty(key)); 256 } 257 } 258 } 259 260 protected void loadClasspath() 261 { 262 classpathHash = new Hashtable(); 263 try 264 { 265 String classpathString = System.getProperty("java.class.path"); 266 if (verbose) 267 pwOut.println("classpath: " + classpathString); 268 StringTokenizer st = new StringTokenizer(classpathString, File.pathSeparator); 269 int entries = st.countTokens(); 270 classpath = new String [entries]; 271 for (int i = 0; i < entries; i++) 272 { 273 classpath[i] = st.nextToken(); 274 } 275 } 276 catch (SecurityException se) 277 { 278 pwOut.println("**error** SecurityException getting classpath"); 279 System.exit(1); 280 } 281 for (int i = 0; i < classpath.length; i++) 282 { 283 String pathEntry = classpath[i]; 284 if (pathEntry.toUpperCase(java.util.Locale.ENGLISH).endsWith(".ZIP") || 285 pathEntry.toUpperCase(java.util.Locale.ENGLISH).endsWith(".JAR") ) 286 { 287 ZipFile zipfile = null; 288 try 289 { 290 zipfile = new ZipFile(pathEntry.replace('/', File.separatorChar)); 291 } 292 catch (IOException ioe) 293 { 294 } 299 if (zipfile != null) 300 { 301 302 classpathHash.put(pathEntry, zipfile); 303 } 304 else 305 { 306 if (verbose) 307 { 308 pwOut.println("Ignoring <zip> entry: " + pathEntry); 309 } 310 311 } 312 } 313 else 314 { 315 File file = new File(pathEntry); 316 317 if (file.exists() && file.isDirectory()) 318 { 319 classpathHash.put(pathEntry, file); 320 } 321 else 322 { 323 if (verbose) 324 { 325 pwOut.println("Ignoring <dir> entry: " + pathEntry); 326 } 327 } 328 } 329 } 330 } 331 332 333 protected InputStream locateClass(String className, boolean beVerbose) 334 { 335 if (className.startsWith("/")) 336 { 337 className = className.substring(1); 338 } 339 if (beVerbose) 340 { 341 pwOut.println("Looking for " + className); 342 } 343 344 if (classpath == null) 345 { 346 loadClasspath(); 347 } 348 349 for (int i = 0; i < classpath.length; i++) 350 { 351 String pathEntry = classpath[i]; 352 Object hash = classpathHash.get(pathEntry); 353 if (hash != null) 354 { 355 if (hash instanceof ZipFile) 356 try 357 { 358 ZipFile zipfile = (ZipFile) hash; 359 360 ZipEntry entry = zipfile.getEntry(className); 361 362 if (entry != null) 363 { 364 InputStream is = zipfile.getInputStream(entry); 365 DataInputStream dis= new DataInputStream(new BufferedInputStream(is)); 366 return dis; 367 } 368 } 369 catch (IOException ioe) 370 { 371 if (beVerbose) 372 { 373 pwOut.println("IOException loading ZipFile or creating InputStream " + 374 " from it"); 375 pwOut.println(ioe); 376 } 377 } 378 else if (hash instanceof File) 379 { 380 File file = new File((File)hash, className.replace('/', File.separatorChar)); 381 if (beVerbose) 382 { 383 pwOut.println("looking to load file: " + file.getName()); 384 } 385 if (file.exists()) 386 { 387 if (beVerbose) 388 { 389 pwOut.println(" found it!"); 390 } 391 try 392 { 393 FileInputStream fis = new FileInputStream(file); 394 return new BufferedInputStream(fis, 8192); 395 } 396 catch (IOException ioe) 397 { 398 if (beVerbose) 399 { 400 pwOut.println("IOException creating FileInputStream"); 401 pwOut.println(ioe); 402 return null; 403 } 404 } 405 } 406 } 407 } 408 } 410 411 if (beVerbose) 413 { 414 pwOut.println("returing null on purpose"); 415 } 416 return null; 417 } 418 419 protected void findDependencies(String className) throws IOException { 420 indent++; 421 try { 422 if (className.startsWith("java.") && skipJava) 423 { 424 pwOut.println("Skipping JAVA " + className); 425 return; 426 } 427 if (className.startsWith("javax.") && skipJavax) 428 { 429 return; 431 } 432 if (className.startsWith("sun.") && skipSun) 433 { 434 return; 436 } 437 if (className.startsWith("org.") && skipOrg) 438 { 439 if (!className.startsWith("org.apache.derby")) 441 { 442 return; 444 } 445 } 446 if (className.startsWith("com.informix.") && skipInformix) 447 { 448 return; 450 } 451 if (className.startsWith("com.ibm.mobileservices.") && skipDB2e) 452 { 453 return; 455 } 456 if (className.startsWith("common.") && skipDB2) 457 { 458 return; 460 } 461 462 if (ignoreWebLogic) 463 { 464 if (className.startsWith("weblogic.")) 465 { 466 return; 467 } 468 } 469 470 if (db2jtools || db2jdrda) { 471 472 if ( 475 className.startsWith("org.apache.derby.authentication.") 476 || className.startsWith("org.apache.derby.catalog.") 477 || className.startsWith("org.apache.derby.iapi.db.") 478 || className.startsWith("org.apache.derby.diag.") 479 || className.startsWith("org.apache.derby.jdbc.") 480 || className.startsWith("org.apache.derby.vti.") 481 ) 482 { 483 return; 484 } 485 } 486 487 if (db2jdrda) { 491 492 if ( 493 className.startsWith("org.apache.derby.impl.sql") 494 || className.startsWith("org.apache.derby.impl.jdbc") 495 || className.startsWith("org.apache.derby.impl.services") 496 || className.startsWith("org.apache.derby.iapi.") 497 ) 498 { 499 return; 500 } 501 } 502 503 if (foundClasses.get(className) != null) 505 return; 506 507 if (verbose) { 508 for (int i =0; i < indent; i++) 509 { 510 System.out.print("."); 511 } 512 System.out.println(className); 513 } 514 515 521 boolean dontCheckDependencies = false; 522 528 529 530 531 try 532 { 533 Hashtable localHashtable = null; 534 535 if (keepDependencyHistory) { 536 localHashtable = (Hashtable) masterClassList.get(className); 537 if (localHashtable == null) 538 { 539 localHashtable = new Hashtable(); 540 masterClassList.put(className, localHashtable); 541 } 542 } 543 544 foundClasses.put(className, "class"); 545 546 if (dontCheckDependencies) 547 return; 548 549 String fileName = "/" + className.replace('.', '/') + ".class"; 550 551 InputStream is = locateClass(fileName, false); 552 553 if (is == null) { 554 pwOut.println("**error** Got NULL when looking for fileName = " + fileName); 555 if (!keepRolling) 556 { 557 System.exit(1); 558 } 559 else 560 { 561 errorCount++; 562 } 563 } 564 567 ClassInvestigator ch = ClassInvestigator.load(is); 568 is.close(); 569 570 for (Enumeration e = ch.referencedClasses(); e.hasMoreElements(); ) { 571 String x = (String ) e.nextElement(); 572 if (x.startsWith("com.ms.")) 574 { 575 continue; 576 } 577 578 if (!org.apache.derby.iapi.services.sanity.SanityManager.DEBUG) { 579 if (x.indexOf("SanityManager") != -1) { 580 581 boolean printSanityWarning = true; 582 583 int ld = className.lastIndexOf("."); 584 if (ld != -1) { 585 if (className.lastIndexOf("T_") == ld + 1) 586 printSanityWarning = false; 587 else if (className.lastIndexOf("T_") == ld + 1) 588 printSanityWarning = false; 589 else if (className.lastIndexOf("D_") == ld + 1) 590 printSanityWarning = false; 591 else if (className.lastIndexOf("TEST_") == ld + 1) 592 printSanityWarning = false; 593 else if (className.endsWith("SanityManager")) 594 printSanityWarning = false; 595 } 596 597 if (printSanityWarning) 598 System.out.println("SANITY >>> " + fileName); 599 } 600 } 601 602 if (keepDependencyHistory && (localHashtable.get(x) == null)) 603 { 604 605 localHashtable.put(x, "class"); 606 } 607 findDependencies(x); 608 } 609 } 610 catch (NullPointerException npe) 611 { 612 pwOut.println("**error** Got NullPointerException in findDependencies when looking up "); 613 pwOut.println(className); 614 615 npe.printStackTrace(); 616 if (!keepRolling) 617 { 618 System.exit(1); 619 } 620 errorCount++; 621 } 622 623 if (!isCloudscapeCode(className)) 625 return; 626 627 if (!db2jtools) 633 { 634 String packageName = className.substring(0, className.lastIndexOf('.') + 1); 635 636 for (int i = 0; i < propFiles.length; i++) { 637 String fileName = "/" + packageName.replace('.', '/') + propFiles[i]; 638 if (foundClasses.get(fileName) != null) 639 continue; 640 641 InputStream is = getClass().getResourceAsStream(fileName); 642 if (is == null) 643 continue; 644 is.close(); 645 646 647 foundClasses.put(fileName.substring(1), "file"); 648 } 649 } 650 } finally { 651 indent--; 652 } 653 } 654 protected boolean isCloudscapeCode(String name) { 655 return name.startsWith("com.ibm.db2j.") || 656 name.startsWith("com.ihost.cs.") || 657 name.startsWith("db2j.") || 658 name.startsWith("org.apache.derby"); 659 } 660 661 662 protected void showAllItems() 663 { 664 Enumeration e = masterClassList.keys(); 665 pwOut.println("------------Printing all dependents--------------"); 666 while (e.hasMoreElements()) 667 { 668 String kid = (String ) e.nextElement(); 669 pwOut.println(kid ); 670 Hashtable scoreboard = new Hashtable(); 671 Hashtable grandkids = (Hashtable) masterClassList.get(kid); 672 unrollHashtable("", grandkids, scoreboard, 1); 673 } 674 } 675 676 677 protected void showAllItemsOneLevel() 678 { 679 pwOut.println("Showing all dependencies"); 680 pwOut.println("One level only"); 681 pwOut.println("-----------------------------------"); 682 683 Enumeration e = masterClassList.keys(); 684 while (e.hasMoreElements()) 685 { 686 String key = (String ) e.nextElement(); 687 pwOut.println(key); 688 Hashtable h = (Hashtable) masterClassList.get(key); 689 Enumeration e2 = h.keys(); 690 Hashtable h2 = new Hashtable(); 691 while (e2.hasMoreElements()) 692 { 693 String key2 = (String ) e2.nextElement(); 694 pwOut.println("\t" + key2); 695 } 696 } 697 } 698 699 700 protected void unrollHashtable( String parent, Hashtable current, Hashtable scoreboard, int indentLevel) 701 { 702 String indentString = " "; 703 Enumeration e = current.keys(); 704 String key = null; 705 706 while (e.hasMoreElements()) 707 { 708 key = (String ) e.nextElement(); 709 if (key.equals(parent)) 710 { 711 continue; 712 } 713 pwOut.print(indentLevel + ":"); 714 715 Integer value = (Integer ) scoreboard.get(key); 716 if (value != null ) 717 { 718 for (int i = 0; i < indentLevel; i++) 719 { 720 pwOut.print(indentString); 721 } 722 pwOut.println(key + "*****REPEATED class back at level " + value + "****"); 723 return; 724 } 725 for (int i = 0; i < indentLevel; i++) 726 { 727 pwOut.print(indentString); 728 } 729 pwOut.println(key); 730 731 Hashtable currentsChildren = (Hashtable) masterClassList.get(key); 732 scoreboard.put(key, new Integer (indentLevel)); 733 unrollHashtable(key, currentsChildren, scoreboard, (indentLevel+1)); 734 scoreboard.put(key, new Integer (indentLevel)); 735 736 } 737 } 738 739 740 } 741 | Popular Tags |