KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyBuild > classlister


1 /*
2
3    Derby - Class org.apache.derbyBuild.classlister
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

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 /**
33
34     A tool that generates a list of required classes from a
35     set of properties files. The value of any property within a property
36     file that starts with 'derby.module.' is taken as a class name.
37     That class name and all the clases it requires are listed to
38     System.out, to facilitate building a zip file. Classes that
39     start with 'java.' or 'javax.' are not listed and are not
40     checked for dependent classes.
41     <P>
42     If the class name starts with 'com.ibm.db2j.' then a messages.properties
43     file is searched for corresponding to that class, if one exists then
44     is is added to the list of files printed.
45     <P>
46     The search path for the classes is $CLASSPATH
47     <P>
48     If the system property cloudscapeOnly is set to true then only classes
49     and message.properties files are listed that start with com.ibm.db2j.
50     <P>
51     The output for each class or properties file is a relative file
52     name that uses '/' as the file separator. e.g.
53
54     com/ibm/db2j/core/Setup.class
55
56   <P>
57     The output order of the classes & files is random.
58     <P>
59
60
61     Usage: java [-DignoreWebLogic=true] [-Dverbose=true] [-DcloudscapeOnly=true] [-DruntimeOnly=true]
62     [-Ddb2jtools=true]
63     [-DportingOnly=true] [-Doutputfile=<filename>] org.apache.derbyBuild.classlister
64     property_file [ property_file ... ]
65 **/

66
67 public class classlister {
68
69     protected String JavaDoc[] sets;
70     protected Hashtable foundClasses;
71     //protected ClassUtilitiesFactory cuf;
72

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 JavaDoc classpath[] = null;
89     protected String JavaDoc 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 JavaDoc[] propFiles = {
102         "messages.properties",
103         "instructions.properties",
104                 "metadata.properties"
105     };
106
107     public static void main(String JavaDoc 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         //System.out.println("outputfile: " + outputfile);
142
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         //cuf = new ModifyClasses();
156

157         foundClasses = new Hashtable(3000, 0.8f);
158         
159         for (int i = 0; i < sets.length; i++)
160         {
161
162             // If a set name ends in '.class' then take it as a class
163
// name of the form com.acme.foo.MyClass.class.
164
try
165             {
166
167                 String JavaDoc 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 JavaDoc name = (String JavaDoc) e.nextElement();
197             String JavaDoc type = (String JavaDoc) 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                 // is a file name
224
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 JavaDoc key = (String JavaDoc) 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 JavaDoc 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 JavaDoc[entries];
271                 for (int i = 0; i < entries; i++)
272                 {
273                     classpath[i] = st.nextToken();
274                 }
275             }
276             catch (SecurityException JavaDoc 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 JavaDoc 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                     // can't do anything about it; zipfile doesn't exists
295
// it can happen if the person sticks a directory called
296
// foo.zip in the classpath or foo.zip doesn't exist as
297
// a file
298
}
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 JavaDoc 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 JavaDoc pathEntry = classpath[i];
352             Object JavaDoc 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             //
409
}
410
411         // could not find it
412
if (beVerbose)
413         {
414             pwOut.println("returing null on purpose");
415         }
416         return null;
417     }
418
419     protected void findDependencies(String JavaDoc 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             //System.out.println("Skipping JAVAX " + className);
430
return;
431         }
432                 if (className.startsWith("sun.") && skipSun)
433                 {
434                         //System.out.println("Skipping Sun " + className);
435
return;
436                 }
437         if (className.startsWith("org.") && skipOrg)
438         {
439             // Allow opensource org.apache.derby classes
440
if (!className.startsWith("org.apache.derby"))
441             {
442             //System.out.println("Skipping org " + className);
443
return;
444             }
445         }
446         if (className.startsWith("com.informix.") && skipInformix)
447         {
448             //System.out.println("Skipping Informix " + className);
449
return;
450         }
451         if (className.startsWith("com.ibm.mobileservices.") && skipDB2e)
452         {
453             //System.out.println("Skipping DB2e " + className);
454
return;
455         }
456         if (className.startsWith("common.") && skipDB2)
457         {
458             //System.out.println("Skipping DB2 common " + className);
459
return;
460         }
461
462         if (ignoreWebLogic)
463         {
464             if (className.startsWith("weblogic."))
465             {
466                 return;
467             }
468         }
469
470         if (db2jtools || db2jdrda) {
471
472             // for tools skip classes that are part of the db2j product api
473
// they should be pulled in from cs.jar or any client.jar
474
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         // drda explicitly brings in some database engine classes.
488
// they must be picke dup from cs.jar and not put in
489
// the network server jar.
490
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         // already seen class
504
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         /*
516             org.apache.derby.iapi.reference.ClassName &
517             RegisteredFormatIds has a list of all registered classes, If we pull this in then
518             we will pull in the complete set of classes. So we add this to our list but don't
519             dependency check it.
520         */

521         boolean dontCheckDependencies = false;
522         /*
523         if (className.equals("org.apache.derby.iapi.reference.ClassName") ||
524             className.equals("org.apache.derby.iapi.services.io.RegisteredFormatIds")) {
525             dontCheckDependencies = true;
526         }
527         */

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 JavaDoc 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             //byte[] classData = new byte[is.available()];
565
//is.read(classData);
566

567             ClassInvestigator ch = ClassInvestigator.load(is);
568             is.close();
569
570             for (Enumeration e = ch/*.getClassInfo()*/.referencedClasses(); e.hasMoreElements(); ) {
571                 String JavaDoc x = (String JavaDoc) e.nextElement();
572                 // skip microsoft classes
573
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 JavaDoc 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         // look for properties only with cloudscape code ...
624
if (!isCloudscapeCode(className))
625             return;
626
627                 // The following block of code checks the package of each class
628
// scanned to see if there is a corresponding properties file
629
// from propFiles and adds it to the list of found classes.
630
// derbytools.jar should not contain any of these files, so skip
631
// for that jar. See also DERBY-1537.
632
if (!db2jtools)
633                 {
634             String JavaDoc packageName = className.substring(0, className.lastIndexOf('.') + 1);
635
636             for (int i = 0; i < propFiles.length; i++) {
637             String JavaDoc 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 JavaDoc 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 JavaDoc kid = (String JavaDoc) 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 JavaDoc key = (String JavaDoc) 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 JavaDoc key2 = (String JavaDoc) e2.nextElement();
694                 pwOut.println("\t" + key2);
695             }
696         }
697     }
698
699
700     protected void unrollHashtable( String JavaDoc parent, Hashtable current, Hashtable scoreboard, int indentLevel)
701     {
702         String JavaDoc indentString = " ";
703         Enumeration e = current.keys();
704         String JavaDoc key = null;
705  
706         while (e.hasMoreElements())
707         {
708             key = (String JavaDoc) e.nextElement();
709             if (key.equals(parent))
710             {
711                 continue;
712             }
713             pwOut.print(indentLevel + ":");
714
715             Integer JavaDoc value = (Integer JavaDoc) 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 JavaDoc(indentLevel));
733             unrollHashtable(key, currentsChildren, scoreboard, (indentLevel+1));
734             scoreboard.put(key, new Integer JavaDoc(indentLevel));
735             
736         }
737     }
738             
739
740 }
741
Popular Tags