KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > diagnostics > 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.util.diagnostics;
30
31 import java.io.*;
32 import java.util.*;
33 import java.util.jar.*;
34 import java.util.zip.*;
35 //Bug 4677074 begin
36
import java.util.logging.Logger JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import com.sun.logging.LogDomains;
39 //Bug 4677074 end
40

41 public class JWhich
42 {
43 //Bug 4677074 begin
44
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
45 //Bug 4677074 end
46
public static void main(String JavaDoc[] args)
47     {
48         if(args == null || args.length == 0)
49         {
50             usage();
51             return;
52         }
53
54         int argNum = 0;
55         
56         JWhich jwhich;
57
58         if(args[0].toLowerCase().equals("-classpath"))//NOI18N
59
{
60             if(args.length != 3)
61             {
62                 usage();
63                 return;
64             }
65
66             jwhich = new JWhich(args[2], args[1]);
67         }
68         else
69             jwhich = new JWhich(args[0]);
70     }
71
72     ///////////////////////////////////////////////////////////
73

74     public JWhich(String JavaDoc classname, String JavaDoc classpathArg)
75     {
76         this.classpathArg = classpathArg;
77         ctor(classname);
78     }
79
80     ///////////////////////////////////////////////////////////
81

82     public JWhich(String JavaDoc classname)
83     {
84         ctor(classname);
85     }
86
87     ///////////////////////////////////////////////////////////
88

89     public String JavaDoc getResult()
90     {
91         return result;
92     }
93
94     ///////////////////////////////////////////////////////////
95

96     private void ctor(String JavaDoc classname)
97     {
98         this.classname = classname;
99
100         //if(doExhaustive)
101
//doReflect = false;
102

103         initClasspath();
104         fixClassname();
105         String JavaDoc[] locations = findClass();
106         
107         pr("");//NOI18N
108

109         if(locations == null || locations.length <= 0)
110         {
111             pr("Can't find class");//NOI18N
112
return;
113         }
114
115         for(int i = 0; i < locations.length; i++)
116             pr(classname + " located in " + locations[i]);//NOI18N
117

118         //if(doReflect)
119
//new Reflect(classname);
120
}
121
122     ///////////////////////////////////////////////////////////
123

124     private static void usage()
125     {
126         System.out.println("Usage: java " + JWhich.class.getName() + " [-classpath a_classpath] classname");//NOI18N
127
}
128
129     ///////////////////////////////////////////////////////////
130

131     private void initClasspath()
132     {
133         String JavaDoc cp;
134
135         if(classpathArg == null)
136             cp = System.getProperty("java.class.path");//NOI18N
137
else
138             cp = classpathArg;
139
140         StringTokenizer tokens = new StringTokenizer(cp, ";", false);//NOI18N
141
int nTokens = tokens.countTokens();
142
143         classpath = new String JavaDoc[nTokens];
144
145         debug("" + nTokens + " tokens.");//NOI18N
146

147         for(int i = 0; tokens.hasMoreTokens(); i++)
148         {
149             String JavaDoc s = tokens.nextToken();
150             debug(s);
151             classpath[i] = s;
152         }
153      }
154
155     ///////////////////////////////////////////////////////////
156

157     private void fixClassname()
158     {
159         // change as follows:
160
// com.netscape.blizzard.foo --> com\netscape\blizzard\foo
161
// com/netscape/blizzard/foo --> com\netscape\blizzard\foo
162
// com/netscape\blizzard.foo --> com\netscape\blizzard\foo
163

164         debug("old classname: " + classname);//NOI18N
165
jarClassname = classname;
166
167         classname = classname.replace('.', File.separatorChar);
168
169         if(File.separatorChar != '/')
170             classname = classname.replace('/', File.separatorChar);
171         
172         if(File.separatorChar != '\\')
173             classname = classname.replace('\\', File.separatorChar);
174         
175         // classnames in jars ALWAYS look like: com/foo/goo.class
176

177         jarClassname = jarClassname.replace('.', '/');
178         jarClassname = jarClassname.replace('\\', '/');
179         
180         classname = classname + ".class";//NOI18N
181
jarClassname = jarClassname + ".class";//NOI18N
182

183         debug("new classname: " + classname);//NOI18N
184
debug("new jarClassname: " + jarClassname);//NOI18N
185
}
186
187     ///////////////////////////////////////////////////////////
188

189     private String JavaDoc[] findClass()
190     {
191         ArrayList names = new ArrayList();
192
193         for(int i = 0; i < classpath.length; i++)
194         {
195             String JavaDoc path = classpath[i];
196
197             if(findClass(path))
198             {
199                 names.add(path);
200                 debug("FOUND IT: " + path);//NOI18N
201
}
202         }
203
204         int num = names.size();
205
206         debug("Found it in " + num + " places");//NOI18N
207

208         if(num <= 0)
209         {
210             return null;
211         }
212
213         String JavaDoc[] ss = new String JavaDoc[num];
214         ss = (String JavaDoc[])names.toArray(ss);
215         return ss;
216     }
217
218     ///////////////////////////////////////////////////////////
219

220     private boolean findClass(String JavaDoc path)
221     {
222         if(path.toLowerCase().endsWith(".jar"))//NOI18N
223
{
224             return findClassInJar(path);
225         }
226
227         File f = new File(path + File.separator + classname);
228         debug("Looking for " + f);//NOI18N
229

230         return f.exists();
231     }
232
233     ///////////////////////////////////////////////////////////
234

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

267     private void debug(String JavaDoc s)
268     {
269         if(debug_)
270             pr(s);
271     }
272
273     ///////////////////////////////////////////////////////////
274

275     private void pr(String JavaDoc s)
276     {
277 //Bug 4677074 System.out.println(s);
278
//Bug 4677074 begin
279
_logger.log(Level.FINE,s);
280 //Bug 4677074 end
281
result += s;
282     }
283
284     ///////////////////////////////////////////////////////////
285

286     private String JavaDoc[] classpath = null;
287     private String JavaDoc classpathArg = null;
288     private String JavaDoc classname = null;
289     private String JavaDoc jarClassname = null;
290     private boolean doReflect = false;
291     private boolean doExhaustive = true;
292     private boolean debug_ = false;
293     private String JavaDoc result = new String JavaDoc();
294 }
295
Popular Tags