FileDocCategorySizeDatePackage
JarLoader.javaAPI DocExample5755Sun Oct 25 18:13:36 GMT 1998None

JarLoader

public class JarLoader extends SecureClassLoader

Fields Summary
private URL
urlBase
public boolean
printLoadMessages
Hashtable
classArrays
Hashtable
classIds
static int
groupNum
ThreadGroup
threadGroup
Constructors Summary
public JarLoader(String base, ClassLoader parent)


	     
		super(parent);
		try {
			if (!(base.endsWith("/")))
				base = base + "/";
			urlBase = new URL(base);
			classArrays = new Hashtable();
			classIds = new Hashtable();
		} catch (Exception e) {
			throw new IllegalArgumentException(base);
		}
	
Methods Summary
public voidcheckPackageAccess(java.lang.String name)

		SecurityManager sm = System.getSecurityManager();
		if (sm != null)
			sm.checkPackageAccess(name);
	
protected java.lang.ClassfindClass(java.lang.String name)

		String urlName = name.replace('.", '/");
		byte buf[];
		Class cl;

		SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			int i = name.lastIndexOf('.");
			if (i >= 0)
				sm.checkPackageDefinition(name.substring(0, i));
		}

		buf = (byte[]) classArrays.get(urlName);
		if (buf != null) {
			// In 1.2 beta 4, the code source requires an array of certificates
			// instead of an arragy of objects
			java.security.cert.Certificate ids[] =
					(java.security.cert.Certificate []) classIds.get(urlName);
			CodeSource cs = new CodeSource(urlBase, ids);
			cl = defineClass(name, buf, 0, buf.length, cs);
			return cl;
		}

		try {
			URL url = new URL(urlBase, urlName + ".class");
			if (printLoadMessages)
				System.out.println("Loading " + url);
			InputStream is = url.openConnection().getInputStream();
			buf = getClassBytes(is);
			CodeSource cs = new CodeSource(urlBase, null);
			cl = defineClass(name, buf, 0, buf.length, cs);
			return cl;
		} catch (Exception e) {
			System.out.println("Can't load " + name + ": " + e);
			return null;
		}
	
private byte[]getClassBytes(java.io.InputStream is)

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedInputStream bis = new BufferedInputStream(is);
		boolean eof = false;
		while (!eof) {
			try {
				int i = bis.read();
				if (i == -1)
					eof = true;
				else baos.write(i);
			} catch (IOException e) {
				return null;
			}
		}
		return baos.toByteArray();
	
java.lang.StringgetHost()

		return urlBase.getHost();
	
java.lang.ThreadGroupgetThreadGroup()

		if (threadGroup == null)
			threadGroup = new ThreadGroup("JavaRuner ThreadGroup-" + groupNum++);
		return threadGroup;
	
private voidloadClassBytes(java.util.jar.JarInputStream jis, java.lang.String jarName, java.util.jar.JarEntry je)

		if (printLoadMessages)
			System.out.println("\t" + jarName);
		BufferedInputStream jarBuf = new BufferedInputStream(jis);
		ByteArrayOutputStream jarOut = new ByteArrayOutputStream();
		int b;
		try {
			while ((b = jarBuf.read()) != -1)
				jarOut.write(b);
			String className = jarName.substring(0, jarName.length() - 6);
			classArrays.put(className, jarOut.toByteArray());
			// In 1.2 beta 4, the getIdentities method has been replaced with
			// the getCertificates method, which is very convenient, since
			// we need a certificate to initialize the codebase anyway
			// classIds.put(className, je.getIdentities());
			java.security.cert.Certificate c[] = je.getCertificates();
			if (c == null)
				c = new java.security.cert.Certificate[0];
			classIds.put(className, c);
		} catch (IOException ioe) {
			System.out.println("Error reading entry " + jarName);
		}
	
public voidreadJarFile(java.lang.String name)

		URL jarUrl = null;
		JarInputStream jis;
		JarEntry je;

		try {
			jarUrl = new URL(urlBase, name);
		} catch (MalformedURLException mue) {
			System.out.println("Unknown jar file " + name);
			return;
		}
		if (printLoadMessages)
			System.out.println("Loading jar file " + jarUrl);

		try {
			jis = new JarInputStream(jarUrl.openConnection().getInputStream());
		} catch (IOException ioe) {
			System.out.println("Can't open jar file " + jarUrl);
			return;
		}

		try {
			while ((je = jis.getNextJarEntry()) != null) {
				String jarName = je.getName();
				if (jarName.endsWith(".class"))
					loadClassBytes(jis, jarName, je);
				// else ignore it; it could be an image or audio file
				jis.closeEntry();
			}
		} catch (IOException ioe) {
			System.out.println("Badly formatted jar file");
		}