크라켄 JAR 언로딩 완료

아주 간단하게 JarClassLoader를 직접 구현해서 끝장봤다.
그냥 URLClassLoader 쓰려고 했는데 JAR 파일 한 번 물면 죽어라 안 놔줘서..

이제 로딩만 끝나고 나면 언로딩을 하든 말든 그냥 파일 날릴 수 있다.
진작 이렇게 나올 것이지 (..)

내일부터는 문서화하고 버그 찾고 코드 좀 다듬어 줘야지.
완전 대충 막 찍어놓은 코드 천지라서 -_-
그러고보니 리소스 처리도 빼먹었네;

package kr.nchovy.kraken.common;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarClassLoader extends ClassLoader {
    private List<String> loadedClasses;

    public JarClassLoader(String jarPath) throws FileNotFoundException,
            IOException {
        loadedClasses = new LinkedList<String>();
        loadClasses(jarPath);
    }

    public List<String> getLoadedClasses() {
        return loadedClasses;
    }

    private void loadClasses(String path) throws FileNotFoundException,
            IOException {
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(path);

            Enumeration<JarEntry> entries = jarFile.entries();
            JarEntry entry = null;
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();

                if (entry.getName().endsWith(".class")) {
                    String className = getClassName(entry);

                    InputStream jarInputStream = jarFile.getInputStream(entry);
                    byte[] data = new byte[(int) entry.getSize()];

                    jarInputStream.read(data);
                    defineClass(className, data, 0, data.length);
                    loadedClasses.add(className);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jarFile != null)
                jarFile.close();
        }
    }

    private String getClassName(JarEntry classEntry) {
        return classEntry.getName().replace(".class", "").replace("/", ".");
    }
}

by xeraph | 2008/08/21 00:23 | NCHOVY | 트랙백 | 덧글(2)

트랙백 주소 : http://xeraph.egloos.com/tb/4563830
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Commented by 가짜집시 at 2008/08/21 10:02
OSGi 쪽은 어떤지요?
Commented by xeraph at 2008/08/21 11:13
막연히 복잡하지 않을까 해서 안 봤는데 오늘 한 번 봐야겠네요 ㅎㅎ

:         :

:

비공개 덧글

◀ 이전 페이지다음 페이지 ▶