KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > util > JWhich


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * @version 1.00 April 1, 2000
26  * @author Byron Nevins
27  */

28
29 package com.sun.enterprise.tools.common.util;
30
31 import java.io.*;
32 import java.util.*;
33 import java.util.jar.*;
34 import java.util.zip.*;
35 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
36
37 public class JWhich
38 {
39     public static void main(String JavaDoc[] args)
40     {
41         if(args == null || args.length == 0)
42         {
43             usage();
44             return;
45         }
46
47         int argNum = 0;
48         
49         JWhich jwhich;
50
51         if(args[0].toLowerCase().equals("-classpath"))//NOI18N
52
{
53             if(args.length != 3)
54             {
55                 usage();
56                 return;
57             }
58
59             jwhich = new JWhich(args[2], args[1]);
60         }
61         else
62             jwhich = new JWhich(args[0]);
63     }
64
65     ///////////////////////////////////////////////////////////
66

67     public JWhich(String JavaDoc classname, String JavaDoc classpathArg)
68     {
69         this.classpathArg = classpathArg;
70         ctor(classname);
71     }
72
73     ///////////////////////////////////////////////////////////
74

75     public JWhich(String JavaDoc classname)
76     {
77         ctor(classname);
78     }
79
80     ///////////////////////////////////////////////////////////
81

82     public String JavaDoc getResult()
83     {
84         return result;
85     }
86
87     ///////////////////////////////////////////////////////////
88

89     private void ctor(String JavaDoc classname)
90     {
91         this.classname = classname;
92
93         //if(doExhaustive)
94
//doReflect = false;
95

96         initClasspath();
97         fixClassname();
98         String JavaDoc[] locations = findClass();
99         
100         pr("");//NOI18N
101

102         if(locations == null || locations.length <= 0)
103         {
104             pr("Can't find class");//NOI18N
105
return;
106         }
107
108         for(int i = 0; i < locations.length; i++)
109             pr(classname + " located in " + locations[i]);//NOI18N
110

111         //if(doReflect)
112
//new Reflect(classname);
113
}
114
115     ///////////////////////////////////////////////////////////
116

117     private static void usage()
118     {
119         System.out.println("Usage: java " + JWhich.class.getName() + " [-classpath a_classpath] classname");//NOI18N
120
}
121
122     ///////////////////////////////////////////////////////////
123

124     private void initClasspath()
125     {
126         String JavaDoc cp;
127
128         if(classpathArg == null)
129             cp = System.getProperty("java.class.path");//NOI18N
130
else
131             cp = classpathArg;
132
133         StringTokenizer tokens = new StringTokenizer(cp, ";", false);//NOI18N
134
int nTokens = tokens.countTokens();
135
136         classpath = new String JavaDoc[nTokens];
137
138         debug("" + nTokens + " tokens.");//NOI18N
139

140         for(int i = 0; tokens.hasMoreTokens(); i++)
141         {
142             String JavaDoc s = tokens.nextToken();
143             debug(s);
144             classpath[i] = s;
145         }
146      }
147
148     ///////////////////////////////////////////////////////////
149

150     private void fixClassname()
151     {
152         // change as follows:
153
// com.netscape.blizzard.foo --> com\netscape\blizzard\foo
154
// com/netscape/blizzard/foo --> com\netscape\blizzard\foo
155
// com/netscape\blizzard.foo --> com\netscape\blizzard\foo
156

157         debug("old classname: " + classname);//NOI18N
158
jarClassname = classname;
159
160         classname = classname.replace('.', File.separatorChar);
161
162         if(File.separatorChar != '/')
163             classname = classname.replace('/', File.separatorChar);
164         
165         if(File.separatorChar != '\\')
166             classname = classname.replace('\\', File.separatorChar);
167         
168         // classnames in jars ALWAYS look like: com/foo/goo.class
169

170         jarClassname = jarClassname.replace('.', '/');
171         jarClassname = jarClassname.replace('\\', '/');
172         
173         classname = classname + ".class";//NOI18N
174
jarClassname = jarClassname + ".class";//NOI18N
175

176         debug("new classname: " + classname);//NOI18N
177
debug("new jarClassname: " + jarClassname);//NOI18N
178
}
179
180     ///////////////////////////////////////////////////////////
181

182     private String JavaDoc[] findClass()
183     {
184         ArrayList names = new ArrayList();
185
186         for(int i = 0; i < classpath.length; i++)
187         {
188             String JavaDoc path = classpath[i];
189
190             if(findClass(path))
191             {
192                 names.add(path);
193                 debug("FOUND IT: " + path);//NOI18N
194
}
195         }
196
197         int num = names.size();
198
199         debug("Found it in " + num + " places");//NOI18N
200

201         if(num <= 0)
202         {
203             return null;
204         }
205
206         String JavaDoc[] ss = new String JavaDoc[num];
207         ss = (String JavaDoc[])names.toArray(ss);
208         return ss;
209     }
210
211     ///////////////////////////////////////////////////////////
212

213     private boolean findClass(String JavaDoc path)
214     {
215         if(path.toLowerCase().endsWith(".jar"))//NOI18N
216
{
217             return findClassInJar(path);
218         }
219
220         File f = new File(path + File.separator + classname);
221         debug("Looking for " + f);//NOI18N
222

223         return f.exists();
224     }
225
226     ///////////////////////////////////////////////////////////
227

228     private boolean findClassInJar(String JavaDoc path)
229     {
230         ZipInputStream zin = null;
231
232         try
233         {
234             zin = new ZipInputStream(new FileInputStream(path));
235             ZipEntry entry;
236
237             while((entry = zin.getNextEntry()) != null)
238             {
239                 String JavaDoc name = entry.getName();
240                 zin.closeEntry();
241
242                 if(name.equals(jarClassname))
243                 {
244                     zin.close();
245                     return true;
246                 }
247             }
248             zin.close();
249         }
250         catch(IOException e)
251         {
252             debug("" + e + " " + path);//NOI18N
253
}
254     
255         return false;
256     }
257
258     ///////////////////////////////////////////////////////////
259

260     private void debug(String JavaDoc s)
261     {
262         if(debug_)
263             pr(s);
264     }
265
266     ///////////////////////////////////////////////////////////
267

268     private void pr(String JavaDoc s)
269     {
270         System.out.println(s); //NOI18N
271
result += s;
272     }
273
274     ///////////////////////////////////////////////////////////
275

276     private String JavaDoc[] classpath = null;
277     private String JavaDoc classpathArg = null;
278     private String JavaDoc classname = null;
279     private String JavaDoc jarClassname = null;
280     private boolean doReflect = false;
281     private boolean doExhaustive = true;
282     private boolean debug_ = false;
283     private String JavaDoc result = new String JavaDoc();
284 }
285
Popular Tags