posts - 1,  comments - 25,  trackbacks - 0
This class is used to dynmaic add user specify location to system loader or external loader

/*
 * $Id: ClassLoaderUtil.java, Dec 22, 2009, 8:22:21 PM, liama. Exp$
 *
 */

import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

import sun.misc.Launcher;

/**
 * Class/Interface description
 *
 * @author liama</a>
 * @since JDK 1.5
 */
public class ClassLoaderUtil {
    /**
     * The value is used for get parent loader.
     */
    private static Field classes;

    /**
     * The add url method loader.
     */
    private static Method addURL;
    static {
        try {
             classes = ClassLoader.class.getDeclaredField("parent");
             addURL = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
         } catch (Exception e) {
             e.printStackTrace();
         }
         classes.setAccessible(true);
         addURL.setAccessible(true);
     }

    /**
     * The value is used for system loader
     */
    private static URLClassLoader system = (URLClassLoader) getSystemClassLoader();

    /**
     * The value is used for external loader
     */
    private static URLClassLoader ext = (URLClassLoader) getExtClassLoader();

    /**
     * Get system loader
     *
     * @return {@link ClassLoader}
     */
    public static ClassLoader getSystemClassLoader() {
        return ClassLoader.getSystemClassLoader();
     }

    /**
     * Get external loader
     *
     * @return {@link ClassLoader}
     */
    public static ClassLoader getExtClassLoader() {
        return getSystemClassLoader().getParent();
     }

    /**
     * Get sub class loader from system class loader
     *
     * @return {@link List}
     */
    public static List getClassesLoadedBySystemClassLoader() {
        return getClassesLoadedByClassLoader(getSystemClassLoader());
     }

    /**
     * Get sub class loader from external class loader
     *
     * @return {@link List}
     */
    public static List getClassesLoadedByExtClassLoader() {
        return getClassesLoadedByClassLoader(getExtClassLoader());
     }

    /**
     * Get sub class loader from  class loader
     *
     * @param cl {@link ClassLoader}
     * @return {@link List}
     */
    public static List getClassesLoadedByClassLoader(ClassLoader cl) {
        try {
            return (List) classes.get(cl);
         } catch (Exception e) {
            //throw new RootException(e);
             e.printStackTrace();
            return null;
        }
     }

    /**
     * Get bootstrap urls
     *
     * @return {@link URL}
     */
    public static URL[] getBootstrapURLs() {
        return Launcher.getBootstrapClassPath().getURLs();
     }

    /**
     * Get system urls
     *
     * @return {@link URL}
     */
    public static URL[] getSystemURLs() {
        return system.getURLs();
     }

    /**
     * Get external urls
     *
     * @return {@link URL}
     */
    public static URL[] getExtURLs() {
        return ext.getURLs();
     }

    /**
     * Print class path to specify stream
     *
     * @param ps {@link PrintStream}
     * @param classPath {@link URL}
     */
    private static void list(PrintStream ps, URL[] classPath) {
        for (int i = 0; i < classPath.length; i++) {
             ps.println(classPath[i]);
         }
     }

    /**
     * List boot strap class path
     */
    public static void listBootstrapClassPath() {
         listBootstrapClassPath(System.out);
     }

    /**
     * Print boot strap class path
     *
     * @param ps {@link PrintStream}
     */
    public static void listBootstrapClassPath(PrintStream ps) {
         ps.println("BootstrapClassPath:");
         list(ps, getBootstrapClassPath());
     }

    /**
     * list system class path
     */
    public static void listSystemClassPath() {
         listSystemClassPath(System.out);
     }

    /**
     * Print system class path
     *
     * @param ps {@link PrintStream}
     */
    public static void listSystemClassPath(PrintStream ps) {
         ps.println("SystemClassPath:");
         list(ps, getSystemClassPath());
     }

    /**
     * List external class path
     */
    public static void listExtClassPath() {
         listExtClassPath(System.out);
     }

    /**
     * Print external class path
     *
     * @param ps {@link PrintStream}
     */
    public static void listExtClassPath(PrintStream ps) {
         ps.println("ExtClassPath:");
         list(ps, getExtClassPath());
     }

    /**
     * Get boot strap class path
     *
     * @return {@link URL}
     */
    public static URL[] getBootstrapClassPath() {
        return getBootstrapURLs();
     }

    /**
     * Get system class path
     *
     * @return {@link URL}
     */
    public static URL[] getSystemClassPath() {
        return getSystemURLs();
     }

    /**
     * Get external class path
     *
     * @return {@link URL}
     */
    public static URL[] getExtClassPath() {
        return getExtURLs();
     }

    /**
     * Add user specify URL to system class loader
     *
     * @param url {@link URL}
     */
    public static void addURL2SystemClassLoader(URL url) {
        try {
             addURL.invoke(system, new Object[] { url });
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

    /**
     * Add user specify URL to external class loader
     *
     * @param url {@link URL}
     */
    public static void addURL2ExtClassLoader(URL url) {
        try {
             addURL.invoke(ext, new Object[] { url });
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

    /**
     * Add user specify String to class loader
     *
     * @param path {@link String}
     */
    public static void addClassPath(String path) {
         addClassPath(new File(path));
     }

    /**
     * Add user specify String to external class loader
     *
     * @param path {@link String}
     */
    public static void addExtClassPath(String path) {
         addExtClassPath(new File(path));
     }

    /**
     * Add user specify File to  class loader
     *
     * @param dirOrJar {@link File}
     */
    public static void addClassPath(File dirOrJar) {
        try {
             addURL2SystemClassLoader(dirOrJar.toURL());
         } catch (MalformedURLException e) {
            //throw new RootException(e);
             e.printStackTrace();
         }
     }

    /**
     * Add user specify File to external class loader
     *
     * @param dirOrJar {@link File}
     */
    public static void addExtClassPath(File dirOrJar) {
        try {
             addURL2ExtClassLoader(dirOrJar.toURL());
         } catch (MalformedURLException e) {
             e.printStackTrace();
         }
     }
   
    /**
     * Add specify path to specify loader
     *
     * @param cl {@link ClassLoader}
     * @param path String
     */
    public static void addClassPath2ClassLoader(ClassLoader cl, String path){
        try {
            addURL.invoke(cl, new File(path).toURL());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
   
    /**
     * Add specify path to specify loader
     *
     * @param cl {@link ClassLoader}
     * @param path String
     */
    public static void addClassPath2ClassLoader(ClassLoader cl, URL path){
        try {
            addURL.invoke(cl, path);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

posted on 2009-12-24 21:04 Daniel 阅读(591) 评论(0)  编辑  收藏 所属分类: CoreJava

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(3)

随笔档案

文章分类

文章档案

相册

搜索

  •  

最新评论