KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > JarReader


1 /*
2  * Created on Nov 18, 2006
3  */

4 package com.openedit.util;
5
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 import com.openedit.OpenEditRuntimeException;
12
13 public abstract class JarReader
14 {
15     protected List JavaDoc fieldCompletedFiles;
16     
17     public void processInClasspath(String JavaDoc inName)
18     {
19         //Bug alert: On Windows there is a bug in JDK that locks files when calling Class.getResource()
20
//So we unzip the jar files ourself so that nothing is locked
21
try
22         {
23             String JavaDoc cp = System.getProperty("java.class.path");
24             //log.info("Classpath: " + cp);
25
String JavaDoc[] files = cp.split(File.pathSeparator);
26             processFiles(files, inName);
27         }
28         catch ( Exception JavaDoc ex)
29         {
30             throw new OpenEditRuntimeException(ex);
31         }
32     }
33     protected void processFiles(String JavaDoc[] files, String JavaDoc inName)
34     {
35         if( files == null)
36         {
37             return;
38         }
39         FileUtils fileU = new FileUtils();
40
41         try
42         {
43             File JavaDoc tmp = File.createTempFile("openedit", "cpdir");
44             fileU.deleteAll(tmp );
45             ZipUtil unzip = new ZipUtil();
46             unzip.setFindFileName(inName);
47             unzip.setExitOnFirstFind(true);
48             for (int i = 0; i < files.length; i++)
49             {
50                 File JavaDoc cpdir = new File JavaDoc( files[i]);
51                 if( getCompletedFiles().contains(cpdir))
52                 {
53                     continue;
54                 }
55                 getCompletedFiles().add( cpdir );
56                 if( cpdir.isFile())
57                 {
58                     tmp.mkdirs();
59                     unzip.unzip(cpdir, tmp);
60                     cpdir = tmp;
61                 }
62                 //log.info("Looking in:" + cpdir.getPath());
63
File JavaDoc found = new File JavaDoc( cpdir,inName );
64                 if( found.exists() )
65                 {
66                     processFile(found);
67                 }
68                 fileU.deleteAll(tmp );
69             }
70         } catch ( IOException JavaDoc ex)
71         {
72             throw new OpenEditRuntimeException(ex);
73         }
74     }
75     public void processInLibDir(File JavaDoc inLibDir, String JavaDoc inName)
76     {
77         File JavaDoc[] jars = inLibDir.listFiles();
78         if( jars != null)
79         {
80             String JavaDoc[] names = new String JavaDoc[jars.length];
81             for (int i = 0; i < jars.length; i++)
82             {
83                 names[i] = jars[i].getAbsolutePath();
84             }
85             processFiles(names, inName);
86         }
87         
88     }
89     
90     public abstract void processFile( File JavaDoc inFile);
91
92     public List JavaDoc getCompletedFiles()
93     {
94         if( fieldCompletedFiles == null)
95         {
96             fieldCompletedFiles = new ArrayList JavaDoc();
97         }
98         return fieldCompletedFiles;
99     }
100     public void setCompletedFiles(List JavaDoc inCompletedFiles)
101     {
102         fieldCompletedFiles = inCompletedFiles;
103     }
104 }
105
Popular Tags