JFinal实现注解绑定Model映射表--支持通配符路径扫描(包含jar)

之前有实现通过注解绑定Model映射表,但是扫描的文件只能扫描指定的包文件或者没扫描到jar文件,有一些朋友也实现过jar文件扫描,但是配置不够灵活,例如:不能支持扫描的路径通过通配符进行配置,扫描的jar必须指定集合,扫描的编译性能低等问题。

我为了解决这些问题,引入了spring,简化了配置提升了扫描编译性能。

定义自定义注解@TableBind

/**
 * @author yangty
 */
package red.sea.jfinal.plugin.tableBindplugin;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface TableBind {
	String tableName();
	String pkName() default "";
}

定义扩展Model类ExtendModel

package red.sea.model;

import com.jfinal.plugin.activerecord.Model;

/**
 * @author yangty
 * @date 2018-01-27
 */
public class  ExtendModel<M extends Model> extends Model<M> {

	public boolean openFlag=false;
	
	public ExtendModel setOpenFlag(boolean openFlag){
		this.openFlag=openFlag;
		return this;
	}
	@Override
	public M set(String attr, Object value) {
		if(openFlag){
			return super.set(attr, value);
		}else{
			return (M) this;
		}
	}
		
		
	}


定义扫描类工具ClassSearcher

/**
 * Copyright (c) 2018, yangty
 *
 */
package red.sea.jfinal.plugin.tableBindplugin;

import com.google.common.collect.Lists;
import com.jfinal.kit.PathKit;
import jxl.common.Logger;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

public class ClassSearcher {

    protected static final Logger LOG = Logger.getLogger(ClassSearcher.class);

    private String classpath = PathKit.getRootClassPath();

    private String libDir = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + "lib";

    private List<String> scanPackages = Lists.newArrayList();

    private boolean includeAllJarsInLib = false;

    private List<String> includeJars = Lists.newArrayList();
    
    private List<String> includeClassPaths = Lists.newArrayList();

    private Class target;
    
    List<File> classList = new ArrayList<File>();
    
    @SuppressWarnings("unchecked")
    private static <T> List<Class<? extends T>> extraction(Class<T> clazz, List<String> classFileList) {
        List<Class<? extends T>> classList = Lists.newArrayList();
        for (String classFile : classFileList) {
            Class<?> classInFile = Reflect.on(classFile).get();
            if (clazz.isAssignableFrom(classInFile) && clazz != classInFile) {
                classList.add((Class<? extends T>) classInFile);
            }
        }

        return classList;
    }

    public static ClassSearcher of(Class target) {
        return new ClassSearcher(target);
    }

    /**
     * @param baseDirName    查找的文件夹路径
     * @param targetFileName 需要查找的文件名
     */
    private static List<String> findFiles(String baseDirName, String targetFileName) {
        /**
         * 算法简述: 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件, 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。 队列不空,重复上述操作,队列为空,程序结束,返回结果。
         */
        List<String> classFiles = Lists.newArrayList();
        File baseDir = new File(baseDirName);
        if (!baseDir.exists() || !baseDir.isDirectory()) {
            LOG.error("search error:" + baseDirName + "is not a dir!");
        } else {
            String[] files = baseDir.list();
            for (int i = 0; i < files.length; i++) {
                File file = new File(baseDirName + File.separator + files[i]);
                if (file.isDirectory()) {
                    classFiles.addAll(findFiles(baseDirName + File.separator + files[i], targetFileName));
                } else {
                    if (wildcardMatch(targetFileName, file.getName())) {
                        String fileName = file.getAbsolutePath();
                        String open = PathKit.getRootClassPath() + File.separator;
                        String close = ".class";
                        int start = fileName.indexOf(open);
                        int end = fileName.indexOf(close, start + open.length());
                        String className = fileName.substring(start + open.length(), end).replaceAll(File.separator, ".");
                        classFiles.add(className);
                    }
                }
            }
        }
        return classFiles;
    }

    /**
     * 通配符匹配
     *
     * @param pattern  通配符模式
     * @param fileName 待匹配的字符串
     */
    private static boolean wildcardMatch(String pattern, String fileName) {
        int patternLength = pattern.length();
        int strLength = fileName.length();
        int strIndex = 0;
        char ch;
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
            ch = pattern.charAt(patternIndex);
            if (ch == '*') {
                // 通配符星号*表示可以匹配任意多个字符
                while (strIndex < strLength) {
                    if (wildcardMatch(pattern.substring(patternIndex + 1), fileName.substring(strIndex))) {
                        return true;
                    }
                    strIndex++;
                }
            } else if (ch == '?') {
                // 通配符问号?表示匹配任意一个字符
                strIndex++;
                if (strIndex > strLength) {
                    // 表示str中已经没有字符匹配?了。
                    return false;
                }
            } else {
                if ((strIndex >= strLength) || (ch != fileName.charAt(strIndex))) {
                    return false;
                }
                strIndex++;
            }
        }
        return strIndex == strLength;
    }

    public <T> List<Class<? extends T>> search() {
        List<String> classFileList = Lists.newArrayList();
        if (scanPackages.isEmpty()) {
            classFileList = findFiles(classpath, "*.class");
        } else {
            for (String scanPackage : scanPackages) {
                classFileList = findFiles(classpath + File.separator + scanPackage.replaceAll("\\.", "\\" + File.separator), "*.class");
            }
        }
        classFileList.addAll(findjarFiles(libDir));
        return extraction(target, classFileList);
    }
    
    public <T> List<Class<? extends T>> searchByJar() {
        List<String> classFileList = Lists.newArrayList();
        classFileList.addAll(findjarFiles(libDir));
        return extraction(target, classFileList);
    }
    
    public <T> List<Class<? extends T>> searchByPath(String pre) throws IOException {
    	List<String> classFileList = Lists.newArrayList();
    	if (includeClassPaths.isEmpty()) {
    		classFileList = findFiles(classpath, "*.class");
        } else {
            for (String includeClassPath : includeClassPaths) {
            	classFileList.addAll(findPathFiles(includeClassPath, pre));
            }
        }
    	
    	return extraction(target, classFileList);
    }


    public ClassSearcher(Class target) {
        this.target = target;
    }

    public ClassSearcher injars(List<String> jars) {
        if (jars != null) {
            includeJars.addAll(jars);
        }
        return this;
    }

    public ClassSearcher inJars(String... jars) {
        if (jars != null) {
            for (String jar : jars) {
                includeJars.add(jar);
            }
        }
        return this;
    }
    
    public ClassSearcher searchClassPath(List<String> inclassPaths) {
    	if(inclassPaths !=null) {
    		includeClassPaths.addAll(inclassPaths);
    	}
    	return this;
    }
    
    public ClassSearcher searchClassPath(String... inclassPaths) {
    	if(inclassPaths !=null) {
    		for(String inclassPath : inclassPaths) {
    			includeClassPaths.add(inclassPath);
    		}
    	}
    	return this;
    }

    public ClassSearcher includeAllJarsInLib(boolean includeAllJarsInLib) {
        this.includeAllJarsInLib = includeAllJarsInLib;
        return this;
    }

    public ClassSearcher classpath(String classpath) {
        this.classpath = classpath;
        return this;
    }

    public ClassSearcher libDir(String libDir) {
        this.libDir = libDir;
        return this;
    }

    public ClassSearcher scanPackages(List<String> scanPaths) {
        if (scanPaths != null) {
            scanPackages.addAll(scanPaths);
        }
        return this;
    }
    
    private List<String> findPathFiles(String path, String pre) throws IOException{
    	List<String> classFiles = Lists.newArrayList();
    	ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource[] resources=resolver.getResources(path);
		for(Resource resource: resources) {
			String classPath = resource.getURL().toString();
			int len = classPath.lastIndexOf(pre);
			String className = classPath.substring(len+1, classPath.length() - 6).replaceAll("/", ".");
			classFiles.add(className);
		}
		
		return classFiles;
    }
    
    
	/**
     * 查找jar包中的class
     */
    private List<String> findjarFiles(String baseDirName) {
        List<String> classFiles = Lists.newArrayList();
        File baseDir = new File(baseDirName);
        /**
         * 1.1 判断目录是否存在
         */
        if (!baseDir.exists() || !baseDir.isDirectory()) {
            LOG.error("file search error:" + baseDirName + " is not a dir!");
            return classFiles;
        }
        
        /**
         * 1.2 指定目录获取文件
         */
        File[] files = baseDir.listFiles();
        for (File file : files) {
        	/**
        	 * 1.3 如果是目录则进行递归
        	 */
            if (file.isDirectory()) {
                classFiles.addAll(findjarFiles(file.getAbsolutePath()));
                continue;
            }
            
            /**
             * 1.4 判断如果jar包扫描包含当前文件
             */
            if (includeAllJarsInLib || includeJars.contains(file.getName())) {
                JarFile localJarFile = null;
                try {
                    localJarFile = new JarFile(new File(baseDirName + File.separator + file.getName()));
                    Enumeration<JarEntry> entries = localJarFile.entries();
                    while (entries.hasMoreElements()) {
                        JarEntry jarEntry = entries.nextElement();
                        String entryName = jarEntry.getName();
                        if (scanPackages.isEmpty()) {
                            if (!jarEntry.isDirectory() && entryName.endsWith(".class")) {
                                String className = entryName.replaceAll(File.separator, ".").substring(0, entryName.length() - 6);
                                classFiles.add(className);
                            }
                        } else {
                            for (String scanPackage : scanPackages) {
                                scanPackage = scanPackage.replaceAll("\\.", "\\" + File.separator);
                                if (!jarEntry.isDirectory() && entryName.endsWith(".class") && entryName.startsWith(scanPackage)) {
                                    String className = entryName.replaceAll("/", ".").substring(0, entryName.length() - 6);
                                    classFiles.add(className);
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (localJarFile != null) {
                            localJarFile.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        return classFiles;
    }
	
	public static String getClassName(File classFile, String pre) {
		String objStr = classFile.toString().replaceAll("\\\\", "/");
		String className;
		className = objStr.substring(objStr.indexOf(pre)+pre.length(),objStr.indexOf(".class"));
		if (className.startsWith("/")) {
			className = className.substring(className.indexOf("/")+1);
		}
		return className.replaceAll("/", ".");
	}

	/**
	 * 根据文件路径,文件类型,文件目录层级
	 * @param path
	 * @param type
	 * @param level
	 */
	public List<File> listPath(File path, String type, int level){  
        File files[] = path.listFiles();
        for (int i = 0; i < files.length; i++){ 
            if(files[i].getName().endsWith(type)){
            	classList.add(files[i]);  
            }
            if (files[i].isDirectory())
            {  
                listPath(files[i],type, (level + 1));  
            }  
        }
        
        return classList;
    }

	
}

扩展ActiveRecordPlugin插件扫描指定路径的自定义注解

/**
 * @author yangty
 */
package red.sea.jfinal.plugin.tableBindplugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Model;
import jxl.common.Logger;
import red.sea.model.ExtendModel;

public class AutoTableBindPlugin extends ActiveRecordPlugin {
	
	protected static final Logger LOG = Logger.getLogger(AutoTableBindPlugin.class);
	
	List<File> classList = new ArrayList<File>();
	
	 List<Class<? extends ExtendModel<Model>>> extendModelsList = new ArrayList<Class<? extends ExtendModel<Model>>>();
	
	public ActiveRecordPlugin arp;
	
	private List<String> scanPackages = Lists.newArrayList();
	
	private List<String> inCludeClassPaths = Lists.newArrayList();
	
	
	public AutoTableBindPlugin(DataSource dataSource, ActiveRecordPlugin arp) {
		super(dataSource);
		this.arp=arp;
		try {
			sacnMode();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 扫描Model
	 * 通配符指定工程目录
	 * @throws IOException 
	 */
	private void sacnMode() throws IOException {
		
		inCludeClassPaths.add("classpath*:red/sea/**/data/**/*.class");
		inCludeClassPaths.add("classpath*:red/sea/**/view/**/*.class");
		extendModelsList = ClassSearcher.of(ExtendModel.class).searchClassPath(inCludeClassPaths).searchByPath("/red/");
		
        TableBind tb=null;
		for (Class<? extends ExtendModel<Model>> classFile : extendModelsList) {
			tb=(TableBind) classFile.getAnnotation(TableBind.class);
			if(null!=tb.pkName() && ! "".equals(tb.pkName())){
				arp.addMapping(tb.tableName(), tb.pkName(), classFile);
			}
		}
	}
	
	/**
	 * 扫描Model
	 * 指定目录
	 * @param fileDir
	 * @param pre
	 * @throws Exception
	 */
	private void scanModel(String fileDir, String pre) throws Exception {
		File dir = new File(fileDir);
		/**
		 * 根据指定文件目录,获取class
		 */
		ClassSearcher search = new ClassSearcher(ExtendModel.class);
		classList = search.listPath(dir,".class", 0);
        
        Class clazz=null;
        String className="";
        TableBind tb=null;
		for (File classFile : classList) {
			className = ClassSearcher.getClassName(classFile, pre);
			if(className.equals("red.sea.model.BaseModel")) {
				continue;
			}
			clazz = Class.forName(className);
			if (clazz.getSuperclass() == ExtendModel.class) {
				tb=(TableBind) clazz.getAnnotation(TableBind.class);
				if(null!=tb.pkName() && ! "".equals(tb.pkName())){
					arp.addMapping(tb.tableName(),tb.pkName(),clazz);
				}
			}
		}
	}
	
	@Override
	public boolean stop() {
		return true;
	}

	
}

启动jfinal注解扫描

在jFinal的启动配置过程中实例化插件类

AutoTableBindPlugin autoTableBind = new AutoTableBindPlugin(dataSource, arp);

通过插件运行jFinal

autoTableBind.arp.start();

 

转载于:https://my.oschina.net/yangty2017/blog/1613883

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值