IT_Programming/Java

Zip File 형태의 클래스 로드하기

JJun ™ 2011. 1. 7. 09:58

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();
         }
     };
  }


}

ZipClassLoaderTest.java
0.0MB