All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.xiaoymin.swaggerbootstrapui.io.ResourceUtil Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.github.xiaoymin.swaggerbootstrapui.io;

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

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/***
 * 添加自定义Swagger Models
 * @since:swagger-bootstrap-ui 1.9.4
 * @author [email protected] 
 * 2019/04/30 10:59
 */
public class ResourceUtil{

    private static final Logger logger= LoggerFactory.getLogger(ResourceUtil.class);

    Set> classes=new HashSet<>();

    private ClassLoader classLoader;

    public Set> getClasses() {
        return classes;
    }

    public ClassLoader getClassLoader() {
        return classLoader==null?Thread.currentThread().getContextClassLoader():classLoader;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    /***
     * find resource by packageNames
     * @param packageNames 包路径名称
     * @return 扫描结果
     */
    public ResourceUtil find(String... packageNames){
        if (packageNames==null){
            throw new IllegalArgumentException("packageName can't be Empty!");
        }
        for (String packageName:packageNames){
            String packagePath=getPackagePath(packageName);
            try {
                //替换packagePath
                //List children=VFS.getInstance().list(packagePath);
                Set children=collectPaths(packagePath);
                for (String file:children){
                    if (file.endsWith(".class")){
                        String name=file.substring(0,file.indexOf('.')).replace('/','.');
                        try {
                            classes.add(getClassLoader().loadClass(name));
                        } catch (Throwable t) {
                            logger.warn("Could not examine class '" + file + "'" + " due to a " +
                                    t.getClass().getName() + " with message: " + t.getMessage());
                        }

                    }
                }
            } catch (IOException e) {
                logger.error("Could not read package: " + packageName, e);
            }

        }
        return this;
    }

    protected String getPackagePath(String packageName){
        return packageName==null?null:packageName.replace('.','/');
    }


    /***
     * 收集所有class
     * @param packagePath  包路径
     * @return 收集扫描结果
     */
    public Set collectPaths(String packagePath) throws IOException {
        Set list=new HashSet<>();
        list.addAll(VFS.getInstance().list(packagePath));
        list.addAll(VFS.getInstance().list("/"+packagePath));
        return list;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy