자바 동적 로드 jar 패키지

8533 단어 종류jar
  package org.util;

  import java.io.File;

  import java.io.IOException;

  import java.net.MalformedURLException;

  import java.net.URL;

  import java.net.URLClassLoader;

  import java.util.ArrayList;

  import java.util.Enumeration;

  import java.util.List;

  import java.util.jar.JarEntry;

  import java.util.jar.JarFile;

  import DC.Utility.Configuration;

  public class ClassLoad {

  static URLClassLoader loader = null;

  /**

  *         jar

  * @return

  */

  public static URLClassLoader getClassLoad() {

  Configuration config = new Configuration(Configuration.getRoot()+File.separator

  + "classpath.properties");

  if (loader == null) {

  URLClassLoaderUtil urlClass = new URLClassLoaderUtil(config

  .getValue("classpath1"), false);

  loader = urlClass.getClassLoader();

  }

  return loader;

  }

  /**

  *         jar  

  * @param url      

  * @param isFile  true     false   

  * @return

  */

  public static URLClassLoader getClassLoad(String url, boolean isFile)

  {

  URLClassLoaderUtil urlClass = new URLClassLoaderUtil(url, false);

  URLClassLoader loader = urlClass.getClassLoader();

  return loader;

  }

  }

  class URLClassLoaderUtil {

  URLClassLoader classLoader = null;// URLClassLoader    

  private String jarFileName;

  private boolean isFile = true;

  List<String> jars = new ArrayList<String>(0);

  /**

  *        jar 

  *

  * @param jarFileName

  */

  public URLClassLoaderUtil(String jarFileName) {

  this.setJarFileName(jarFileName);

  this.inti();

  }

  /**

  *   jar   isFile false         jar

  *

  * @param jarFileName

  *              

  * @param isFile

  */

  public URLClassLoaderUtil(String jarFileName, boolean isFile) {

  this.setJarFileName(jarFileName);

  this.setFile(isFile);

  this.inti();

  }

  /**

  *   ,      ,  jar       classpath

  */

  private void inti() {

  //   jar     classpath

  if (this.isFile) {

  File f = new File(jarFileName);

  addPath(f.toURI().toString());

  jars.add(f.getAbsolutePath());

  } else {

  ReadJarFile df = new ReadJarFile(jarFileName, new String[] { "jar",

  "zip" });

  this.jars = df.getFiles();

  List<String> jarURLs = df.getFilesURL();

  Object[] o = jarURLs.toArray();

  addPath(o);

  }

  }

 

  /**

  *    ,class  

  *

  *@paramcallBack

  */

  public void callBack() {

  for (String s : this.jars) {

  loadClass(s);

  }

  }

  /**

  *    jar   classpath

  *

  *@paramjarURL

  */

  private void addPath(String jarURL) {

  try {

  classLoader = new URLClassLoader(new URL[] { new URL(jarURL) });

  } catch (MalformedURLException e) {

  e.printStackTrace();

  }

  }

  /**

  *  jar   classpath

  *

  *@paramjarURLs

  */

  private void addPath(Object[] jarURLs) {

  URL[] urls = new URL[jarURLs.length];

  for (int i = 0; i < jarURLs.length; i++) {

  try {

  System.out.println(jarURLs[i].toString());

  urls[i] = new URL(jarURLs[i].toString());

  } catch (MalformedURLException e) {

  e.printStackTrace();

  }

  }

  classLoader = new URLClassLoader(urls);

  }

  /**

  *    class

  *

  *@paramjarFileName

  *@paramcallBack

  */

  // private void loadClass(String jarFileName, ClassCallBack callBack) {

  private void loadClass(String jarFileName) {

  JarFile jarFile = null;

  try {

  jarFile = new JarFile(jarFileName);

  } catch (IOException e) {

  e.printStackTrace();

  }

  Enumeration<JarEntry> en = jarFile.entries();

  while (en.hasMoreElements()) {

  JarEntry je = en.nextElement();

  String name = je.getName();

  String s5 = name.replace('/', '.');

  if (s5.lastIndexOf(".class") > 0) {

  String className = je.getName().substring(0,

  je.getName().length() - ".class".length()).replace('/',

  '.');

  Class c = null;

  try {

  c = this.classLoader.loadClass(className);

  System.out.println(className);

  } catch (ClassNotFoundException e) {

  System.out.println("NO CLASS: " + className);

  // continue;

  } catch (NoClassDefFoundError e) {

  System.out.println("NO CLASS: " + className);

  // continue;

  }

  //     callBack.operate(c);

  }

  }

  }

  public String getJarFileName() {

  return jarFileName;

  }

 

  /**

  *   jar  

  *

  * @param jarFileName

  */

  public void setJarFileName(String jarFileName) {

  this.jarFileName = jarFileName;

  }

  public boolean isFile() {

  return isFile;

  }

  public URLClassLoader getClassLoader() {

  return classLoader;

  }

  public void setClassLoader(URLClassLoader classLoader) {

  this.classLoader = classLoader;

  }

  public void setFile(boolean isFile) {

  this.isFile = isFile;

  }

  }

  /**

  *   jarwenjian

  * @author hml

  *

  */

  class ReadJarFile {

  List<String> jarList = new ArrayList<String>();

  List<String> filesURL = new ArrayList<String>();

  /**

  *           

  *

  * @param jarFileName

  *              

  * @param strings

  *              

  */

  public ReadJarFile(String jarFileName, String[] strings) {

  // TODO Auto-generated constructor stub

  File f = new File(jarFileName);

  File[] fl = f.listFiles();

  for (File file : fl) {

  for (String str : strings) {

  if (file.getName().endsWith(str)) {

  jarList.add(file.getName());

  filesURL.add(file.toURI().toString());

  }

  }

  }

  }

  /**

  *      

  *

  * @return

  */

  public List<String> getFiles() {

  // TODO Auto-generated method stub

  return filesURL;

  }

  /**

  *       

  *

  * @return

  */

  public List<String> getFilesURL() {

  // TODO Auto-generated method stub

  return filesURL;

  }

  }
/**
* project:codecreate
* package:com.bluedavid.util
* file:DriverUtil.java
* date:2009-12-15   12:35:59
*/

package com.bluedavid.util;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* description
*
* @author
*
*/
public class DriverUtil {
/**
* description:   jar           
*
* @param file    *.jar,    
* @return          
*/
public static Set<String> getDrivers(String file) {
   JarFile jar = null;
   Set<String> set = new HashSet<String>();
   try {
    jar = new JarFile(file);
   } catch (IOException e) {
    return null;
   }
   Enumeration<JarEntry> entries = jar.entries();
  
   URLClassLoader loader = getLoad(file);
   String name;
   while(entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    if(entry.getName().endsWith("Driver.class")) {
     name = entry.getName();
     name = name.substring(0, name.length() - 6);
     name = name.replaceAll("/", ".");
     Class<?> cc = null;
     try {
      cc = loader.loadClass(name);
     } catch (ClassNotFoundException e) {
      continue;
     }
     if(isDriver(cc))
      set.add(name);
    }
   }
  
  
   return set;
}

/**
* description:         
*
* @param clazz
* @return
*/
public static boolean isDriver(Class<?> clazz) {
   Class<?>[] ccc = clazz.getInterfaces();
   boolean flag = false;
   for(Class<?> aa : ccc) {
    if(aa.getName().equals("java.sql.Driver")) {
     flag = true;
    }
   }
   if(!flag) {
    for (; clazz != null; clazz = clazz.getSuperclass()) {
     Class<?>[] interfaces = clazz.getInterfaces();
     for(Class<?> aa : interfaces) {
      if(aa.getName().equals("java.sql.Driver")) {
       flag = true;
      }
     }
    }
   }
   return flag;
}

/**
* description:      (jar),    ClassLoader
*
* @param file   *.jar,    
* @return        ClassLoader
*/
public static URLClassLoader getLoad(String file) {
   URL[] url = null;
   try {
    url = new URL[]{new URL("file:"+file)};
   } catch (MalformedURLException e) {
    return null;
   }
   URLClassLoader loader = new URLClassLoader(url);
   return loader;
}

/**
* description:
*
* @param file     jar  (    )
* @param clazz        class  com.mysql.jdbc.MysqlDriver
* @return
*/
public static Class<?> getDynamic(String file, String clazz) {
   URLClassLoader loader = getLoad(file);
   try {
    return loader.loadClass(clazz);
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
   return null;
}

}

좋은 웹페이지 즐겨찾기