package com.bexp;
import java.io.*;
import java.util.*;
import java.net.*;
public class NetworkClassLoader
// extends ClassLoader
extends URLClassLoader
{
// ClassLoader parent = null;
// ClassLoader jar = null;
//ClassLoader local_loader = null;
/*
public NetworkClassLoader() throws Exception
{
super();
System.out.println("Default constructor");//testing
}
*/
ClassLoader fallback;
public NetworkClassLoader(ClassLoader _parent) throws Exception
{
//super(_parent);
super(new URL[]
{(new URI("file:client.jar")).toURL()},_parent);
fallback = new URLClassLoader(new URL[]{(new URI("file:client.jar")).toURL()},this);
//super.loadClass("com.bexp.ejb.ObjSession",true);
//System.out.println("constructor; parent: "+_parent);//testing
/*
jar = new java.net.URLClassLoader(new java.net.URL[]
{(new java.net.URI("file:client.jar")).toURL()},_parent);
*/
}
protected synchronized Class> findClass(String name) throws ClassNotFoundException
{
//System.out.println("@@@@@@@@@@ finding class "+name);//testing
//if(name.startsWith("[")) { System.out.println("@@@@@@@@@@ finding class "+name); }//testing
try
{ return super.findClass(name); }
catch(Throwable ex)
{
if(name.startsWith("com.bexp.swing")) { throw new ClassNotFoundException(); }
//if((name.startsWith("[L"))&&(name.endsWith(";")))
if((name.startsWith("[")))
{
//loadClass(name.substring(2, name.length()-1));
return Class.forName(name, false, fallback);
}
//^^^- na nat i suda nat
//System.out.println("-------- downloading class "+name);//testing
//System.out.println("$$$$$$$$$$ "+ex);//testing
//throw new ClassNotFoundException();
return downloadClass(name);
}
}
static String hostname="unspecified";
public static String getHostName()
{ return hostname; }
public static void setHostName(String _hostname)
{ hostname = _hostname; }
Class downloadClass(String name) throws ClassNotFoundException
{
try
{
java.net.URLConnection connection
= (new java.net.URI("http://"+hostname+
":8080/BEXP_SD/classSource?classname="+name))
.toURL().openConnection();
InputStream istream = new BufferedInputStream(connection.getInputStream());
byte[] buf = new byte[istream.available()];
ByteArrayOutputStream dstream = new ByteArrayOutputStream(istream.available());
//istream.read(data);
int bytesRead;
while ((bytesRead = istream.read(buf)) != -1)
{ dstream.write(buf, 0, bytesRead); }
byte[] data = dstream.toByteArray();
istream.close();
//System.out.println("Bytes read: "+data.length);//testing
int realStart = 0;//9;
//if(true){ System.out.println("******"+name); }//testing
return defineClass(name,data,realStart,data.length-realStart);
} catch(Exception ex)
{
ex.printStackTrace();//debug
throw new ClassNotFoundException();
}
}
/*
public synchronized Class> loadClass(String name) throws ClassNotFoundException
{
System.out.println("@@@@@@@@@@ load1 "+name);//testing
try
{ return super.loadClass(name); }
catch(Throwable ex)
{
/*
try
{ return super.loadClass(name,true); }
catch(Throwable exx)
{
//System.out.println("-------- downloading class "+name);//testing
System.out.println("$$$$$$$$$$ "+exx);//testing
throw new ClassNotFoundException();
//return loadClass(name,true);
}
* /
System.out.println("######### "+ex);//testing
throw new ClassNotFoundException();
}
}
protected synchronized Class> loadClass(String name, boolean resolve) throws ClassNotFoundException
{
System.out.println("@@@@@@@@@@ load2 "+name);//testing
Class result;
try
{ result = super.loadClass(name,resolve); }
catch(Throwable ex)
{
try {
//System.out.println("ddd");//testing
result = jar.loadClass(name);
}
catch(Throwable exx)
{
System.out.println("-------- downloading class "+name);
/*
try
{
}catch(Throwable exxx)
{
exxx.printStackTrace();//testing
throw new ClassNotFoundException("Server unreachable");
}
* /
throw new ClassNotFoundException();
}
//System.out.println("-------- downloading class "+name);//testing
//System.out.println("$$$$$$$$$$ "+ex);//testing
throw new ClassNotFoundException();
}
return result;
}
*/
}
|