⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 factoryfinder.java

📁 java class loader factory
💻 JAVA
字号:
/**
 * <p>Title: Note information                      </p>
 * <p>Description: you can change it but you       </p>
 * <p>             must not remove this message    </p>
 * <p>             and author name                 </p>
 * <p>Copyright:   Copyright (c) 2004              </p>
 * @author: Jinglong Zhai;
 * @version 1.0
 */
package com.dr.util.find;
import java.io.*;
import java.util.*;

/**
 * <p>Description: Jinglong Zhai define task interface                      </p>
 * @author:        Jinglong Zhai
 * @define date:   2004-02-02  21:07
 * @who modify:    Jinglong Zhai
 * @modify date:   2004-02-02  21:07
 * @reason:        create;
 * @version 1.0
 */
public class FactoryFinder {

  private static String CLASS_SECTION = "classsection";

  /**
   * subclass of exception;
   */
  public static class Error extends java.lang.Error {
    protected Exception exceError;

    /**
     * @param exceptionMsg exception's object;
     * @param strMsg message you pass;
     */
    public Error(Exception exceptionMsg, String strMsg) {
      super("com.dragon.util.factoryfinder.FactoryFinder \n\r" + strMsg);
      this.exceError = exceptionMsg;
    }

    /**
     * @return the object of exception;
     */
    public Exception getException() {
      return this.exceError;
    }

    /**
     * @return the message of exception;
     */
    public String getMessage() {
      String strTemp = super.getMessage();
      if (strTemp == null && this.exceError != null) {
        return this.exceError.getMessage();
      }
      return strTemp;
    }
  }

  /**
   * Subclass of load class;It  is abstract and foundation;
   */
  private static abstract class ClassLoaderFoundation {
    /**
     * @return the object of ClassLoader;
     */
    abstract ClassLoader getContentClassLoader();
  }

  /**
   * Subclass of load class;It  is extends from ClassLoaderFoundation;
   */
  static class ClassLoderFoundationImpl
      extends ClassLoaderFoundation {
    /**
     * @return the object of ClassLoader;
     */
    ClassLoader getContentClassLoader() {
      return Thread.currentThread().getContextClassLoader();
    }
  }

  /**
   * Get classloader;
   * @throws Error self error;
   * @return the object of classLoader
   */
  private static ClassLoader GetClassLoader() throws Error {
    //
    ClassLoader classLoader;
    try {
      Class cla = Class.forName(FactoryFinder.class.getName() +
                                "$ClassLoderFoundationChild");
      ClassLoaderFoundation cf = (ClassLoaderFoundation) cla.newInstance();
      classLoader = cf.getContentClassLoader();
    }
    catch (java.lang.LinkageError le) {
      classLoader = FactoryFinder.class.getClassLoader();
    }
    catch (java.lang.ClassNotFoundException ce) {
      classLoader = FactoryFinder.class.getClassLoader();
    }
    catch (java.lang.Exception ee) {
      throw new Error(ee, ee.toString());
    }
    return classLoader;
  }

  /**
   * Generate instance with classname and classloader;
   * @param className name of class(this class will be instance)
   * @param classLoader the object of current thread's classloader
   * @return the object of class
   * @throws Error myself error
   */
  private static Object newInstance(String className, ClassLoader classLoader) throws
      Error {
    //
    try {
      Class cla;
      if (classLoader == null) {
        cla = Class.forName(className);
      }
      else {
        cla = classLoader.loadClass(className);
      }
      return cla.newInstance();
    }
    catch (ClassNotFoundException cnfe) {
      throw new Error(cnfe, "newInstance(...) ClassNotFoundException::" + className +
                      "  not find!!");
    }
    catch (java.lang.IllegalAccessException iae) {
      throw new Error(iae, "newInstance(...) IllegalAccessException::" + className +
                      "  not find!!");
    }
    catch (java.lang.InstantiationException ie) {
      throw new Error(ie, "newInstance(...) InstantiationException::" + className +
                      "  not find!!");
    }
  }

  /**
   * find the class with the name;
   * @param classId the id of class
   * @return the object of class
   * @throws Error self error
   */
  public static Object FindClass(String classId) throws Error {
    //
    ClassLoader classLoader = GetClassLoader();

    //At first:find in system properties;
    try {
      return newInstance(classId, classLoader);
    }
    catch (Error er) {
      er.printStackTrace();
    }

    throw new Error(null,"FindClass(...) "+classId+" not find");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -