<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    以下是整理后的代碼部分,歡迎批評(píng)指正。

    MyClassLoader.java
    /*
     * @MyClassLoader.java    07/04/17
     *
     * Copyright Zhao Jiucheng. All rights reserved.
     
    */

    package com.neusoft.classloader;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Hashtable;
    import java.util.jar.JarEntry;
    import java.util.jar.JarInputStream;

    /**
     * A class loader is an object that is responsible for loading classes. Given
     * the binary name of a class, a class loader should attempt to locate or
     * generate data that constitutes a definition for the class. A typical strategy
     * is to transform the name into a file name and then read a "class file" of
     * that name from a file system.
     * 
     * 
    @version 1.0, 07/04/17
     * 
    @author Zhao Jiucheng
     * 
     
    */

    public class MyClassLoader extends ClassLoader {

        
    // a classpath for search
        private static String myClasspath = new String("");

        
    // hashtable that memory the loaded classes
        private static Hashtable<String, Class<?>> loadClassHashTable = new Hashtable<String, Class<?>>();

        
    // hashtable that memory the time of loading a class
        private static Hashtable<String, Long> loadClassTime = new Hashtable<String, Long>();

        
    // the null constructor
        public MyClassLoader() {

        }


        
    /**
         * create a classloader and specify a classpath.
         * 
         * 
    @param myClasspath
         *            the specified classpath name.
         
    */

        
    public MyClassLoader(String myClasspath) {
            
    if (!myClasspath.endsWith("\\")) {
                myClasspath 
    = myClasspath + "\\";
            }

            MyClassLoader.myClasspath 
    = myClasspath;
        }


        
    /**
         * set the classpath
         * 
         * 
    @param myClasspath
         *            the specified classpath name
         
    */

        
    public void SetmyClasspath(String myClasspath) {
            
    if (!myClasspath.endsWith("\\")) {
                myClasspath 
    = myClasspath + "\\";
            }

            MyClassLoader.myClasspath 
    = myClasspath;
        }


        
    /**
         * Loads the class with the specified binary name. This method searches for
         * classes in the same manner as the loadClass(String, boolean) method.
         * Invoking this method is equivalent to invoking {loadClass(name,false)}.
         * 
         * 
    @param className
         *            The binary name of the class.
         * 
         * 
    @return The resulting <tt>Class</tt> object.
         * 
         * 
    @throws ClassNotFoundException
         *             If the class was not found.
         
    */

        @SuppressWarnings(
    "unchecked")
        
    public Class loadClass(String className) throws ClassNotFoundException {
            
    return loadClass(className, false);
        }


        
    /**
         * Loads the class with the specified binary name. The default
         * implementation of this method searches for classes in the following
         * order:
         * 
         * Invoke {findLoadedClass(String)} to check if the class has already been
         * loaded.
         * 
         * Invoke {findSystemClass(String)} to load the system class.
         * 
         * Invoke the {findClass(String)} method to find the class.
         * 
         * If the class was found using the above steps, and the resolve flag is
         * true, this method will then invoke the {resolveClass(Class)} method on
         * the resulting Class object.
         * 
         * 
    @param name
         *            The binary name of the class.
         * 
         * 
    @param resolve
         *            If true then resolve the class.
         * 
         * 
    @return The resulting Class object.
         * 
         * 
    @throws ClassNotFoundException
         *             If the class could not be found.
         
    */

        @SuppressWarnings(
    "unchecked")
        
    protected Class loadClass(String name, boolean resolve)
                
    throws ClassNotFoundException {

            
    try {
                Class foundClass 
    = findLoadedClass(name);

                
    // check if the class has already been loaded.
                if (foundClass != null{
                    System.out.println(
    "Complete to load the class: " + name);
                    
    return foundClass;
                }


                
    // if the class is systemClass, load the system class by system
                if (name.startsWith("java.")) {
                    foundClass 
    = findSystemClass(name);
                    loadClassHashTable.put(name, foundClass);
                    System.out.println(
    "System is loading the class: " + name);
                    
    return foundClass;
                }


                
    // invoke the findClass() method to load the class
                try {
                    foundClass 
    = findClass(name);
                }
     catch (Exception fnfe) {
                }


                
    if (resolve && (foundClass != null)) {
                    resolveClass(foundClass);
                }

                
    return foundClass;
            }
     catch (Exception e) {
                
    throw new ClassNotFoundException(e.toString());
            }

        }


        
    /**
         * Finds the class with the specified binary name.The default implementation
         * throws a ClassNotFoundException.
         * 
         * 
    @param className
         *            The binary name of the class.
         * 
         * 
    @return The resulting Class object.
         * 
         * 
    @throws ClassNotFoundException
         *             If the class could not be found.
         
    */

        @SuppressWarnings(
    "unchecked")
        
    public Class findClass(String className) {

            
    byte[] classData = null;
            
    try {
                classData 
    = loadClassData(className);
            }
     catch (IOException e) {
                e.printStackTrace();
            }

            
    if( classData == null){
                
    return null;
            }


            System.out.println(
    "MyClassLoader is loading : " + className + "");
            Class c 
    = defineClass(className, classData, 0, classData.length);
            MyClassLoader.loadClassHashTable.put(className, c);
            System.out.println(
    "Complete to load the class :" + className);
            
    return c;
        }


        
    /**
         * Loads the classData with the specified binary name. This method searches
         * for classes in the specified classpath as
         * searchFile(myClasspath,className) method.
         * 
         * 
    @param name
         *            The binary name of the class
         * 
         * 
    @return The resulting the classData of the class object by byte[]
         * 
         * 
    @throws IOException
         *             if have some failed or interrupted I/O operations.
         
    */

        
    private byte[] loadClassData(String className) throws IOException {

            String filePath 
    = searchFile(myClasspath, className + ".class");

            
    if (!(filePath == null || filePath == "")) {

                System.out.println(
    "It have found the file : " + className
                        
    + ".  Begin to read the data and load the class。");
                FileInputStream inFile 
    = new FileInputStream(filePath);
                
    byte[] classData = new byte[inFile.available()];
                inFile.read(classData);
                inFile.close();
                loadClassTime.put(className, 
    new File(filePath).lastModified());
                
    return classData;
            }
     else {

                filePath 
    = searchFile(myClasspath, className + ".java");
                
    if (!(filePath == null || filePath == "")) {
                    System.out.println(
    "It have found the file : " + filePath
                            
    + ".  Begin to translate");
                    Runtime.getRuntime().exec(
    "javac " + filePath);
                    
    try {
                        Thread.sleep(
    1000);
                    }
     catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(
    "Translate it over : " + filePath);
                    
    return loadClassData(className);
                }
     else {
                    System.out
                            .println(
    "Haven't found the file, and fail to read the classData!");
                    
    return null;
                }

            }

        }


        
    /**
         * Loads the class with the specified binary name.The default implementation
         * throws a ClassNotFoundException.
         * 
         * 
    @param classData
         *            The data of the class.
         * 
    @param className
         *            The binary name of the class.
         * 
         * 
    @return The resulting Class object.
         * 
         * 
    @throws ClassNotFoundException
         *             If the class could not be found.
         
    */

        
    public Class loadClass(byte[] classData, String className)
                
    throws ClassNotFoundException {

            System.out.println(
    "MyClassLoader is loading : " + className + "");
            Class c 
    = defineClass(className, classData, 0, classData.length);
            loadClassHashTable.put(className, c);
            System.out.println(
    "Complete to load the class :" + className);

            
    return c;
        }


        
    /**
         * Loads the class with the specified binary name.The default implementation
         * throws a ClassNotFoundException.
         * 
         * 
    @param className
         *            The binary name of the class.
         * 
    @param jarName
         *            The binary name of the jar that search the class from it.
         * 
         * 
    @return The resulting Class object.
         * 
         * 
    @throws ClassNotFoundException
         *             If the class could not be found.
         
    */

        
    protected Class loadClass(String className, String jarName)
                
    throws ClassNotFoundException {

            String jarPath 
    = searchFile(myClasspath, jarName + ".jar");
            JarInputStream in 
    = null;

            
    if (!(jarPath == null || jarPath == "")) {

                
    try {
                    in 
    = new JarInputStream(new FileInputStream(jarPath));
                    JarEntry entry;
                    
    while ((entry = in.getNextJarEntry()) != null{
                        String outFileName 
    = entry.getName().substring(
                                entry.getName().lastIndexOf(
    "/"+ 1,
                                entry.getName().length());
                        
    if (outFileName.equals(className + ".class")) {
                            
    if (entry.getSize() == -1{
                                System.err.println(
    "error : can't read the file!");
                                
    return null;
                            }

                            
    byte[] classData = new byte[(int) entry.getSize()];
                            System.out
                                    .println(
    "It have found the file : "
                                            
    + className
                                            
    + ".  Begin to read the data and load the class。");
                            in.read(classData);
                            
    return loadClass(classData, className);
                        }

                    }

                    System.out.println(
    "Haven't found the file " + className
                            
    + " in " + jarName + ".jar.");
                }
     catch (IOException e) {
                    e.printStackTrace();
                }
     finally {
                    
    try {
                        in.close();
                    }
     catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }
     else {
                System.out.println(
    "Haven't found the jarFile: " + jarName
                        
    + ".jar.");
                
    return null;
            }

            
    return null;
        }


        
    /**
         * Reloads the class with the specified binary name. Needn't have to restart
         * JVM then reload the class.
         * 
         * 
    @param className
         *            The binary name of the class need to reload .
         * 
         * 
    @return The resulting Class object.
         * 
         * 
    @throws ClassNotFoundException
         *             If the class was not found.
         
    */

        
    public Class reload(String fileName) {

            String filePath 
    = searchFile(myClasspath, fileName + ".class");
            Long a 
    = new File(filePath).lastModified();

            
    if (!a.equals(loadClassTime.get(fileName))) {
                loadClassHashTable.remove(fileName);
                loadClassTime.remove(fileName);
                
    try {
                    MyClassLoader mc2 
    = new MyClassLoader(myClasspath);
                    mc2.loadClass(fileName);
                }
     catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }
     else {
                System.out
                        .println(
    "The class is the newest version , needn't reloading.");
            }

            
    return null;
        }


        
    /**
         * search the file with the specified binary name. Needn't have to restart
         * JVM then reload the class.
         * 
         * 
    @param classpath
         *            the specified path where we search.
         * 
    @param fileName
         *            The binary name of the file that want to search.
         * 
         * 
    @return The resulting file path.
         
    */

        
    public String searchFile(String classpath, String fileName) {

            String cut 
    = fileName.substring(fileName.lastIndexOf('.'), fileName
                    .length());
            String path 
    = fileName.substring(0, fileName.lastIndexOf('.')).replace(
                    
    '.''/')
                    
    + cut;

            File f 
    = new File(classpath + path);
            
    if (f.isFile()) {
                
    return f.getPath();
            }
     else {
                String objects[] 
    = new File(classpath).list();
                
    for (int i = 0; i < objects.length; i++{
                    
    if (new File(classpath + File.separator + objects[i])
                            .isDirectory()) 
    {
                        String s 
    = searchFile(classpath + objects[i]
                                
    + File.separator, fileName);
                        
    if (s == null || s == ""{
                            
    continue;
                        }
     else {
                            
    return s;
                        }

                    }

                }

            }

            
    return null;
        }
    ;

        
    public static void main(String[] args) {
            MyClassLoader c1 
    = new MyClassLoader(
                    
    "E:/javawork/teacher's classloader/bin");
            
    //MyClassLoader c2 = new MyClassLoader(
            
    //        "E:/javawork/teacher's classloader");
            try {
                
    while (true{
                    c1.loadClass(
    "com.neusoft.test.A");
                    
    //c2.loadClass("com.neusoft.test.B");
                    try {
                        Thread.sleep(
    300000);
                    }
     catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
     catch (Exception e) {
                e.printStackTrace();
            }

        }

    }



    MyThread.java
    /*
     * @MyThread.java    2007/04/17
     *
     * Copyright Zhao Jiucheng. All rights reserved.
     * 
     
    */

    package com.neusoft.classloader;

    import java.io.File;


    /**
     * Create a Thread to watch the specify directory,if new classes haved
     * appended,load them.
     * 
     * 
    @author Zhao Jiucheng
     * 
    @version 1.0, 2007/04/17
     
    */


    public class MyThread extends Thread {

        
    // for get the classpath
        private String filePath;

        
    // instantiation a ClassLoader
        private MyClassLoader mcl;

        
    /**
         * Constructs a new MyThread for the given path.
         * 
         * 
    @param path
         * 
    @return null
         
    */

        
    public MyThread(String path) {
            
    this.filePath = path;
            mcl 
    = new MyClassLoader(path);
        }


        
    /**
         * Watch pecify directory to search for appended classes time after time.
         * 
         * 
    @param filepath
         * 
         * 
    @return null
         * 
    @exception ClassNotFoundException
         *                if the class could not be found.
         
    */

        
    public void search(String filePath) {
            File dir 
    = new File(filePath);
            String[] fileList 
    = dir.list();
            
    for (int i = 0; i < fileList.length; i++{

                
    if (new File(filePath + File.separator + fileList[i]).isDirectory()) {

                    search(filePath 
    + fileList[i]);
                }
     else if (new File(filePath + File.separator + fileList[i])
                        .isFile()) 
    {

                    
    if (fileList[i].endsWith(".class")) {
                        
    try {

                            mcl.loadClass(fileList[i].substring(
    0, fileList[i]
                                    .length() 
    - 6));
                        }
     catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }

                    }

                }

            }

        }


        
    /**
         * If this thread was constructed using a separate runnable run object, then
         * that runnable object's run method is called;
         * 
         * 
    @return null
         * 
    @exception InterruptedException
         *                if the thread has been interrupted.
         
    */


        
    public void run() {
            
    int i = 1;
            
    while (true{
                search(filePath);
                System.out.println(
    "searching " + i++ + "s");
                
    try {
                    Thread.sleep(
    1000);
                }
     catch (InterruptedException e) {
                    e.printStackTrace();
                }

                
    if (i == 20{
                    System.out.println(
    "search over!");
                    
    break;
                }

            }

        }


        
    public static void main(String[] args) {
            MyThread t 
    = new MyThread("D:\\soft\\aa");
            t.start();
        }

    }


    MyURLClassLoader.java
    /*
     * @MyURLClassLoader.java    2007/04/17
     *
     * Copyright Zhao Jiucheng. All rights reserved.
     * 
     
    */

    package com.neusoft.classloader;

    import java.net.*;


    /**
     * This class loader is used to load classes and resources from a search path
     * 
     * 
    @author Zhao Jiucheng
     * 
    @version 1.0, 2007/04/17
     
    */

    public class MyURLClassLoader extends URLClassLoader {
        
    /**
         * Constructs a new URLClassLoader for the given URLs.
         * 
         * 
    @param url
         
    */

        
    public MyURLClassLoader(URL[] url) {
            
    super(url);
        }


        
    /**
         * Finds the class with the specified binary name.
         * 
         * 
    @param name
         *            The binary name of the class
         * 
    @return The resulting Class object
         * 
         * 
    @throws ClassNotFoundException
         *             If the class could not be found
         
    */

        @SuppressWarnings(
    "unchecked")
        
    public Class findClass(final String name) {
            
    try {
                
    return super.findClass(name);
            }
     catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            
    return null;
        }


        
    public static void main(String[] args) {
            URL url 
    = null;
            
    try {
                url 
    = new URL("http://192.168.44.19:8088/Webtest/");
            }
     catch (MalformedURLException e) {
                e.printStackTrace();
            }

            MyURLClassLoader urlClassLoader1 
    = new MyURLClassLoader(
                    
    new URL[] { url });
            Class c1 
    = urlClassLoader1.findClass("Tree");
            System.out.println(c1);

        }


    }




    歡迎來(lái)訪!^.^!
    本BLOG僅用于個(gè)人學(xué)習(xí)交流!
    目的在于記錄個(gè)人成長(zhǎng).
    所有文字均屬于個(gè)人理解.
    如有錯(cuò)誤,望多多指教!不勝感激!

    Feedback

    # re: JAVA CLASS LOADING技術(shù)研究---整理后的代碼  回復(fù)  更多評(píng)論   

    2007-04-18 14:39 by lx
    和java原有classloader比好像沒(méi)有太大improvement ...對(duì)于了解class loader原理還是有幫助的

    # re: JAVA CLASS LOADING技術(shù)研究---整理后的代碼  回復(fù)  更多評(píng)論   

    2007-04-19 15:42 by wanglin
    除了委托加載,ms還有上下文加載。

    后者資料甚少,sun官方也沒(méi)有提供詳細(xì)的介紹,而且雖然現(xiàn)在java技術(shù)中已經(jīng)在使用他,但是還是有問(wèn)題。。。。。。。。

    # re: JAVA CLASS LOADING技術(shù)研究---整理后的代碼  回復(fù)  更多評(píng)論   

    2007-04-19 16:40 by 久城
    恩,謝謝,個(gè)人感覺(jué)問(wèn)題還挺大。比如Java中暫時(shí)還沒(méi)有提供區(qū)分版本的機(jī)制,如果使用了比較復(fù)雜的類裝載器體系結(jié)構(gòu),在出現(xiàn)了某個(gè)包或者類的多個(gè)版本時(shí),就有可能出現(xiàn)問(wèn)題了。

    # re: JAVA CLASS LOADING技術(shù)研究---整理后的代碼  回復(fù)  更多評(píng)論   

    2007-04-19 22:10 by 匿名
    版本區(qū)分的問(wèn)題,通過(guò)類加載器可以部分的解決(不同的classload下的包是不會(huì)沖突的),在深入java虛擬機(jī)那本書(shū)中,作者提到classload相當(dāng)于提供了一個(gè)名字空間。 如果你說(shuō)的版本區(qū)分是同一個(gè)classload下的的確會(huì)有問(wèn)題。 我說(shuō)的上下文加載比較啰唆。你可以搜索一下

    Copyright © 久城

    主站蜘蛛池模板: 国产女高清在线看免费观看| 老司机亚洲精品影院在线观看 | 亚洲色精品vr一区二区三区| 日日躁狠狠躁狠狠爱免费视频| 国产成人免费手机在线观看视频| 亚洲精品无码专区在线播放| 毛片A级毛片免费播放| 久久亚洲国产成人影院| 成年女人毛片免费视频| 亚洲国产成人无码AV在线| 国产一区二区免费在线| 老司机午夜精品视频在线观看免费| 国产片免费在线观看| 全黄A免费一级毛片| 国产亚洲欧洲Aⅴ综合一区| 免费播放在线日本感人片| 亚洲短视频男人的影院| 精品无码人妻一区二区免费蜜桃| 亚洲精品国产情侣av在线| 国拍在线精品视频免费观看| 亚洲中文字幕无码久久2020| 国产精品二区三区免费播放心 | 亚洲精品免费在线| jlzzjlzz亚洲jzjzjz| 女人让男人免费桶爽30分钟| 亚洲成a人无码亚洲成av无码| 又色又污又黄无遮挡的免费视| 一级特级女人18毛片免费视频 | 特级aa**毛片免费观看| 国产精品亚洲精品日韩已方| 国产午夜无码精品免费看| 亚洲网址在线观看| 97无码免费人妻超级碰碰碰碰 | 亚洲日本在线电影| 免费在线观看日韩| 色www永久免费网站| 亚洲精品不卡视频| 青草草在线视频永久免费| 成人免费网站久久久| 久热综合在线亚洲精品| 日韩免费精品视频|