FileDocCategorySizeDatePackage
JWhich.javaAPI DocGlassfish v2 API7276Fri May 04 22:34:58 BST 2007com.sun.enterprise.tools.common.util

JWhich

public class JWhich extends Object
version
1.00 April 1, 2000
author
Byron Nevins

Fields Summary
private String[]
classpath
private String
classpathArg
private String
classname
private String
jarClassname
private boolean
doReflect
private boolean
doExhaustive
private boolean
debug_
private String
result
Constructors Summary
public JWhich(String classname, String classpathArgIn)

		classpathArg = classpathArgIn;
		ctor(classname);
	
public JWhich(String classname)

		ctor(classname);
	
Methods Summary
private voidctor(java.lang.String classname)

		this.classname = classname;

		//if(doExhaustive)
			//doReflect = false;

		initClasspath();
		fixClassname();
		String[] locations = findClass();
		
		pr("");//NOI18N

		if(locations == null || locations.length <= 0)
		{
			pr("Can't find class");//NOI18N
			return;
		}

		for(int i = 0; i < locations.length; i++)
			pr(classname + " located in " + locations[i]);//NOI18N
		
		//if(doReflect)
			//new Reflect(classname);
	
private voiddebug(java.lang.String s)

		if(debug_)
			pr(s);
	
private booleanfindClass(java.lang.String path)

		if(path.toLowerCase().endsWith(".jar"))//NOI18N
		{
			return findClassInJar(path);
		}

		File f = new File(path + File.separator + classname);
		debug("Looking for " + f);//NOI18N

		return f.exists();
	
private java.lang.String[]findClass()

		ArrayList names = new ArrayList();

		for(int i = 0; i < classpath.length; i++)
		{
			String path = classpath[i];

			if(findClass(path))
			{
				names.add(path);
				debug("FOUND IT:  " + path);//NOI18N
			}
		}

		int num = names.size();

		debug("Found it in " + num + " places");//NOI18N

		if(num <= 0)
		{
			return null;
		}

		String[] ss = new String[num];
		ss = (String[])names.toArray(ss);
		return ss;
	
private booleanfindClassInJar(java.lang.String path)

		ZipInputStream zin = null;

		try
		{
			zin = new ZipInputStream(new FileInputStream(path));
			ZipEntry entry;

			while((entry = zin.getNextEntry()) != null)
			{  
				String name = entry.getName();
				zin.closeEntry();

				if(name.equals(jarClassname))
				{
					zin.close();
					return true;
				}
			}
			zin.close();
		}
		catch(IOException e)
		{
			debug("" + e + "  " + path);//NOI18N
		}
	
		return false;
	
private voidfixClassname()

		// change as follows:
		// com.netscape.blizzard.foo -->  com\netscape\blizzard\foo
		// com/netscape/blizzard/foo -->  com\netscape\blizzard\foo
		// com/netscape\blizzard.foo -->  com\netscape\blizzard\foo

		debug("old classname: " + classname);//NOI18N
		jarClassname = classname;

		classname = classname.replace('.", File.separatorChar);

		if(File.separatorChar != '/")
			classname = classname.replace('/", File.separatorChar);
		
		if(File.separatorChar != '\\")
			classname = classname.replace('\\", File.separatorChar);
		
		// classnames in jars ALWAYS look like: com/foo/goo.class

		jarClassname	= jarClassname.replace('.", '/");
		jarClassname	= jarClassname.replace('\\", '/");
		
		classname		= classname		+ ".class";//NOI18N
		jarClassname	= jarClassname	+ ".class";//NOI18N

		debug("new classname: " + classname);//NOI18N
		debug("new jarClassname: " + jarClassname);//NOI18N
	
public java.lang.StringgetResult()

		return result;
	
private voidinitClasspath()

		String cp;

		if(classpathArg == null)
			cp = System.getProperty("java.class.path");//NOI18N
		else
			cp = classpathArg;

		StringTokenizer		tokens	= new StringTokenizer(cp, ";", false);//NOI18N
		int					nTokens = tokens.countTokens();

		classpath = new String[nTokens];

		debug("" + nTokens + " tokens.");//NOI18N
		
		for(int i = 0; tokens.hasMoreTokens(); i++) 
		{
			String s = tokens.nextToken();
			debug(s);
			classpath[i] = s;
		}
     
public static voidmain(java.lang.String[] args)

		if(args == null || args.length == 0)
		{
			usage();
			return;
		}

		int argNum = 0;

		if(args[0].toLowerCase().equals("-classpath"))//NOI18N
		{
			if(args.length != 3)
			{
				usage();
				return;
			}

			new JWhich(args[2], args[1]);
		}
		else
			new JWhich(args[0]);
	
private voidpr(java.lang.String s)

		System.out.println(s); //NOI18N
		result += s;
	
private static voidusage()

		System.out.println("Usage:  java  " + JWhich.class.getName() + " [-classpath a_classpath] classname");//NOI18N