`
yuancihang
  • 浏览: 142615 次
  • 性别: Icon_minigender_1
  • 来自: 洛阳
社区版块
存档分类
最新评论

自定义类加载器 二

阅读更多

package com.yuan.common.asm;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuan.common.collection.ListEnumeration;
import com.yuan.common.compress.JarTool;
import com.yuan.common.file.DefaultFilenameFilter;

public class LibClassLoader extends AbstractClassLoader {

    private static final Logger logger = LoggerFactory.getLogger(LibClassLoader.class);
   
    protected List<File> jarList = new ArrayList<File>();
   
    public LibClassLoader(String libPath, ClassLoader parent){
        super(parent);
        File[] jars = new File(libPath).listFiles(new DefaultFilenameFilter("jar", true));
        jarList = Arrays.asList(jars);
    }
    public LibClassLoader(List<String> libPathList, ClassLoader parent){
        super(parent);
        for(String libPath : libPathList){
            File[] jars = new File(libPath).listFiles(new DefaultFilenameFilter("jar", true));
            jarList.addAll(Arrays.asList(jars));
        }
    }
   
    protected byte[] loadClassData(String name)throws ClassNotFoundException {
        String classEntryName = name.replaceAll("\\.", "/") + ".class";
        byte[] classData = null;
        for(File jarFile : jarList) {
            try {
                classData = JarTool.readEntry(jarFile.getAbsolutePath(), classEntryName);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
                throw new ClassNotFoundException(e.getMessage(), e);
            }
            if(classData != null ){
                break;
            }
        }
        if(classData == null){
            throw new ClassNotFoundException(name);
        }
        return classData;
    }
   
    protected URL findResource(String name) {
        for (File jarFile : jarList) {
            try {
                if (JarTool.existsEntry(jarFile.getAbsolutePath(), name)) {
                    return getJarUrl(jarFile.getAbsolutePath(), name);
                }
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
        return null;
    }

    protected Enumeration<URL> findResources(String name) throws IOException {
        ListEnumeration<URL> list = new ListEnumeration<URL>();
        for (File jarFile : jarList) {
            try {
                if (JarTool.existsEntry(jarFile.getAbsolutePath(), name)) {
                    list.add(getJarUrl(jarFile.getAbsolutePath(), name));
                }
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
        return list;
    }
   
    protected URL getJarUrl(String jarFile, String entryName){
        try {
            return new URL("jar:file:/"+jarFile+"!/"+entryName);
        } catch (MalformedURLException e) {
            logger.warn(e.getMessage(), e);
        }
        return null;
    }

}

 

 

package com.yuan.common.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import com.yuan.common.util.AssertUtil;

public class DefaultFilenameFilter implements FilenameFilter {
   
    private boolean ignoreDir = false; //是否忽略文件夹,默认不忽略
    private List<String> extList = new ArrayList<String>();
   
    public DefaultFilenameFilter(final String ext){
        this.extList.add("."+ext);
    }
    public DefaultFilenameFilter(final String... exts){
        if((exts != null) && (exts.length > 0)){
            for(String ext : exts){
                this.extList.add("."+ext);
            }
        }
       
    }
   
    public DefaultFilenameFilter(final String ext, boolean ignoreDir){
        this.ignoreDir = ignoreDir;
        this.extList.add("."+ext);
    }
   
    public void addExt(final String ext){
        this.extList.add("."+ext);
    }
   
    public boolean accept(File dir, String name) {
        if(ignoreDir){
            if(new File(dir+File.separator+name).isDirectory()){
                return false;
            }else{
                return endsWithExt(name);
            }
        }else{
            return endsWithExt(name)||new File(dir+File.separator+name).isDirectory();
        }//if
    }//accept
   
    protected boolean endsWithExt(String name){
        if(!AssertUtil.notEmpty(extList)){
            return true;
        }
        for(String ext : extList){
            if(name.endsWith(ext)){
                return true;
            }
        }
       
        return false;
    }

}

 

 

package com.yuan.common.compress;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 将java类文件打包
 * @author yuan
 *
 */
public class JarTool {
   
    private static final Integer BUFFER_SIZE = 512;
   
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(JarTool.class);

   
    /**
     * 压缩文件夹及其子文件夹
     * @param source String 源文件夹,如: d:/tmp
     * @param dest String 目标文件,如: e:/tmp.jar
     * @throws IOException
     */
    public static void compressFolder(String source, String dest)throws IOException{
        JarOutputStream jos = new JarOutputStream(new FileOutputStream(dest));
        jos.setLevel(Deflater.BEST_COMPRESSION);
        compressJarFolder(jos, new File(source),"");
        jos.close();
    }
   
    private static void compressJarFolder(JarOutputStream jos, File f, String base)throws IOException{
        if(f.isFile()){
            compressJarFile(jos, f, base);
        }else if(f.isDirectory()){
            compressDirEntry(jos, f, base);
           
            String[] fileList = f.list();
            for(String file:fileList){
                String newSource = f.getAbsolutePath() + File.separator + file;
                File newFile = new File(newSource);
                String newBase = base + "/" + f.getName()+"/"+newFile.getName();
                if(base.equals("")){
                    newBase = newFile.getName();//f.getName()+"/"+newFile.getName();
                }else{
                    newBase = base + "/" + newFile.getName();
                }
               
                logger.info("正在压缩文件从 "+newSource+"    到 "+newBase);
                compressJarFolder(jos, newFile, newBase);
               
            }//for
           
        }//if
    }
   
    //压缩单个文件
    private static void compressJarFile(JarOutputStream jos, File f, String base)throws IOException{
        jos.putNextEntry(new ZipEntry(base));
       
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));
       
        byte[] data = new byte[JarTool.BUFFER_SIZE];
        while ((bin.read(data)) != -1) {
            jos.write(data);
        }
        bin.close();
        jos.closeEntry();
    }
   
    public static void compressFile(String sourceFile)throws IOException{
        String jarFile = sourceFile.substring(0, sourceFile.lastIndexOf('.')) + ".jar";
        compressFile(sourceFile, jarFile);
    }
   
    //压缩单个文件到JAR文件中
    public static void compressFile(String sourceFile, String jarFile)throws IOException{
        File f = new File(sourceFile);
        String base = f.getName();
        JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
        jos.putNextEntry(new ZipEntry(base));
       
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));
       
        byte[] data = new byte[JarTool.BUFFER_SIZE];
        while ((bin.read(data)) != -1) {
            jos.write(data);
        }
        bin.close();
        jos.closeEntry();
        jos.close();
    }
   
    //压缩空文件夹
    private static void compressDirEntry(JarOutputStream jos, File f, String base)throws IOException{
        jos.putNextEntry(new ZipEntry(base + "/"));
       
        jos.closeEntry();
    }
   
    public static byte[] readEntry(String jarFile, String entryName)throws IOException{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        boolean existEntry = readEntry(jarFile, entryName, baos);
        if(!existEntry){
            return null;
        }
        return baos.toByteArray();
    }
   
    public static boolean readEntry(String jarFile, String entryName, OutputStream os)throws IOException{
        JarFile jar = new JarFile(new File(jarFile));
        JarEntry entry = jar.getJarEntry(entryName);
        if(entry == null){
            return false;
        }
        InputStream is = new BufferedInputStream(jar.getInputStream(entry));
        int len = 0;
        byte[] b = new byte[1024];
        while ((len = is.read(b, 0, b.length)) != -1) {
            os.write(b, 0, len);
        }
        is.close();
        return true;
    }
   
    public static InputStream getEntryInputStream(String jarFile, String entryName) throws IOException{
        JarFile jar = new JarFile(new File(jarFile));
        JarEntry entry = jar.getJarEntry(entryName);
        if(entry == null){
            return null;
        }
        return jar.getInputStream(entry);
    }
   
    public static InputStream getEntryInputStream(URL url) throws IOException{
        JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
       
        return jarConnection.getInputStream();
    }
   
    public static boolean existsEntry(String jarFile, String entryName)throws IOException{
        JarFile jar = new JarFile(new File(jarFile));
        JarEntry entry = jar.getJarEntry(entryName);
        if(entry == null){
            return false;
        }
        return true;
    }
   
    public static String readTextEntry(String jarFile, String entryName, String encoding)throws IOException{
        String entryContent = "";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        boolean existEntry = readEntry(jarFile, entryName, baos);
        if(!existEntry){
            return null;
        }
        byte[] content = baos.toByteArray();
        baos.close();
        if(encoding == null){//按平台默认编码
            entryContent = new String(content);
        }else{
            entryContent = new String(content, encoding);
        }
        return entryContent;
    }
   
    public static String readTextEntry(String jarFile, String entryName)throws IOException{
        return readTextEntry(jarFile, entryName, null);
    }

    public static void uncompress(String jarFile)throws IOException{
        File f = new File(jarFile);
        String path = f.getAbsolutePath();
        int index = path.lastIndexOf(".");
        if(index > 0){
            path = path.substring(0, index);
        }
        uncompress(jarFile, path);
    }
    public static void uncompress(String jarFile, String outputDir)throws IOException{
        if(!new File(outputDir).exists()){
            new File(outputDir).mkdirs();
        }
        JarFile jar = new JarFile(new File(jarFile));
        Enumeration<JarEntry> e = jar.entries();
        while(e.hasMoreElements()){
            JarEntry entry = e.nextElement();
            saveEntry(jar, entry, outputDir);
        }

    }
    private static void saveEntry(JarFile jar, JarEntry entry, String outputDir)throws IOException{
        logger.info("解压缩" + entry.getName() + " ... ... ");
        File f = new File(new File(outputDir), entry.getName());
        if(entry.isDirectory()){
            f.mkdirs();
            return ;
        }else{
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);
        InputStream is = new BufferedInputStream(jar.getInputStream(entry));
        int len = 0;
        byte[] b = new byte[1024];
        while ((len = is.read(b, 0, b.length)) != -1) {
            fos.write(b, 0, len);
        }
        is.close();
        fos.close();
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics