import! java.io.ByteArrayOutputStream;
import! java.io.IOException;
import! java.io.InputStream;
import! java.net.MalformedURLException;
import! java.net.URL;
import! java.util.Enumeration;
import! java.util.NoSuchElementException;
import! java.util.zip.ZipEntry;
import! java.util.zip.ZipFile;
public class ZipClassLoaderTest extends ClassLoader
{
private final ZipFile file;
private byte[] buffer;
private Class clz;
public ZipClassLoaderTest(String filename) throws IOException
{
super(ZipClassLoaderTest.class.getClassLoader());
this.file = new ZipFile(filename);
}
public Class loadClass(String name) throws ClassNotFoundException
{
Class c = findLoadedClass(name);
if(c == null)
{
try {
c = getParent().loadClass(name);
} catch (ClassNotFoundException e) { }
if(c == null)
c = findClass(name);
}
return c;
}
public Class findClass(String name) throws ClassNotFoundException
{
ZipEntry entry = this.file.getEntry(name.replace('.', '/') + ".class");
if (entry == null)
{
// throw new ClassNotFoundException(name);
System.out.println("ZipClassLoaderTest ZipEntry is NULL!");
clz = Thread.currentThread().getContextClassLoader().loadClass(name);
System.out.println((clz != null)? "ZipClassLoaderTest Thread.currentThread
().getContextClassLoader().loadClass: " + clz.getName() : "clz is NULL");
return clz;
}
try
{
byte[] array = new byte[1024];
InputStream in = this.file.getInputStream(entry);
ByteArrayOutputStream out = new ByteArrayOutputStream(array.length);
int length = in.read(array);
while (length > 0)
{
out.write(array, 0, length);
length = in.read(array);
}
System.out.println("ZipClassLoaderTest findClass - Linux Path & Name: " + this.file.getName
());
return defineClass(name, out.toByteArray(), 0, out.size());
}
catch (Exception exception)
{
System.out.println("ZipClassLoaderTest ZipEntry Exception!");
clz = Thread.currentThread().getContextClassLoader().loadClass(name);
System.out.println((clz != null)? "ZipClassLoaderTest Thread.currentThread
().getContextClassLoader().loadClass: " + clz.getName() : "clz is NULL");
return clz;
// throw new ClassNotFoundException(name, exception);
}
}
public synchronized URL findResource(String name)
{
ZipEntry entry = this.file.getEntry(name);
if (entry == null)
{
return null;
}
try
{
System.out.println("findResource - Linux Path & Name: " + this.file.getName());
return new URL("jar:file:" + this.file.getName() + "!/" + entry.getName());
}
catch (MalformedURLException exception)
{
return null;
}
}
protected Enumeration findResources(final String name)
{
return new Enumeration()
{
private URL element = findResource(name);
public boolean hasMoreElements()
{
return this.element != null;
}
public Object nextElement()
{
if (this.element != null)
{
URL element = this.element;
this.element = null;
return element;
}
throw new NoSuchElementException();
}
};
}
}
'IT_Programming > Java' 카테고리의 다른 글
[펌] 자바 1.4의 새기능: Assertion (0) | 2011.03.11 |
---|---|
[펌] 멀티스레드 프로그래밍에 대해 모르고 있던 5가지 사항 (0) | 2011.02.23 |
[펌] ClassLoader로 프로퍼티 사용 (0) | 2010.12.30 |
[펌] Java 자료 압축, 자료 손상 검사, 파일 묶기 (0) | 2010.12.30 |
Java로 윈도우 서비스 리스트 가져오는 방법 (0) | 2010.12.29 |