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();
}
}
分享到:
相关推荐
自定义类加载器实现自定义加载。自定义类加载器实现自定义加载
简单的自定义类加载器问候世界hello word,基于磁盘的ClassLoader
看完一个Java加载原理教程后,写了这个自己的类加载器,作个笔记,以便以且使用
3-7Tomcat中自定义类加载器的使用与源码实现(1).mp4
java类加载器学习二、自定义类加载器
Java加壳核心代码,请调试后使用。本资源配合博客进行讲解,博客地址:http://blog.csdn.net/JavaBuilt/article/details/79522837
主要介绍了Java基于自定义类加载器实现热部署过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java实现的自定义类加载器,结合具体实例形式分析了java自定义类加载器的原理与具体实现技巧,需要的朋友可以参考下
3-7Tomcat中自定义类加载器的使用与源码实现(2).mp4
ClassLoader的API使用和自定义
主要介绍了java自定义类加载器代码示例,具有一定借鉴价值,需要的朋友可以了解下。
DevLoader.zip tomcat 类加载器
自定义Java类加载器demo,自定义了一个classLoader,重写了loadClass 和findClass,注意 loadClass打破了双亲委派机制,所有的类都要在自定义的class文件中找到,而findClass遵循了双亲委派机制
本文主要介绍了java 类加载与自定义类加载器。具有一定的参考价值,下面跟着小编一起来看下吧
java,自定义加载类的实现。1.自定义加载类的实现。2双亲委派的验证。3如何制定父类加载器。
JCL是可配置,动态和可扩展的自定义类加载器,可直接从Jar文件和其他来源加载Java类。 这样做的动机是创建隔离的类加载器,该类加载器可以轻松地与IoC框架(如Spring)和Web应用程序集成。 整个库,包括其代码库和...
iOS中的高性能自定义点加载指示器
主要介绍了Java语言中的自定义类加载器实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
Java允许使用不同的类加载器,甚至自定义的类加载器。类加载器从源文件(通常是.class 或 .jar文件)获得不依赖平台的字节码,然后将它们加载到JVM内存空间,所以它们能被解释和执行。默认状态下,应用程序的每个类...
通过DexClassLoader类加载器来解析优化前面的dex文件 通过DexClassLoader的loadClass函数来载入类 通过获得到的类的newInstance函数来生成需要的对象 开始使用获取到的动态类对象~\(≧▽≦)/~啦啦啦