throws ClassNotFoundException
{
// Just try to find the resource by the (almost) same name
String resourceName = className.replace('.', '/') + ".class";
int max = urlinfos.size();
Resource resource = null;
for (int i = 0; i < max && resource == null; i++)
{
URLLoader loader = (URLLoader)urlinfos.elementAt(i);
if (loader == null)
continue;
Class k = loader.getClass(className);
if (k != null)
return k;
resource = loader.getResource(resourceName);
}
if (resource == null)
throw new ClassNotFoundException(className + " not found in " + this);
// Try to read the class data, create the CodeSource, Package and
// construct the class (and watch out for those nasty IOExceptions)
try
{
byte[] data;
InputStream in = resource.getInputStream();
try
{
int length = resource.getLength();
if (length != -1)
{
// We know the length of the data.
// Just try to read it in all at once
data = new byte[length];
int pos = 0;
while (length - pos > 0)
{
int len = in.read(data, pos, length - pos);
if (len == -1)
throw new EOFException("Not enough data reading from: "
+ in);
pos += len;
}
}
else
{
// We don't know the data length.
// Have to read it in chunks.
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
byte[] b = new byte[4096];
int l = 0;
while (l != -1)
{
l = in.read(b);
if (l != -1)
out.write(b, 0, l);
}
data = out.toByteArray();
}
}
finally
{
in.close();
}
final byte[] classData = data;
// Now get the CodeSource
final CodeSource source = resource.getCodeSource();
// Find out package name
String packageName = null;
int lastDot = className.lastIndexOf('.');
if (lastDot != -1)
packageName = className.substring(0, lastDot);
if (packageName != null && getPackage(packageName) == null)
{
// define the package
Manifest manifest = resource.getLoader().getManifest();
if (manifest == null)
definePackage(packageName, null, null, null, null, null, null,
null);
else
definePackage(packageName, manifest,
resource.getLoader().getBaseURL());
}
// And finally construct the class!
SecurityManager sm = System.getSecurityManager();
Class result = null;
if (sm != null && securityContext != null)
{
result = AccessController.doPrivileged
(new PrivilegedAction<Class>()
{
public Class run()
{
return defineClass(className, classData,
0, classData.length,
source);
}
}, securityContext);
}
else
result = defineClass(className, classData, 0, classData.length, source);
// Avoid NullPointerExceptions.
Certificate[] resourceCertificates = resource.getCertificates();
if(resourceCertificates != null)
super.setSigners(result, resourceCertificates);
return result;
}