Search in sources :

Example 1 with MvcStartUpException

use of com.duangframework.core.exceptions.MvcStartUpException in project duangframework by tcrct.

the class AbstractClassTemplate method getList.

/**
 * TODO  替换重复的包路径
 */
/*
    private void replaceDuplicatePackageName() {
        if (ToolsKit.isEmpty(packageSet) ) {
           throw new EmptyNullException("包路径为空,请指定包路径");
        }

        Set<String> tmpPackageSet = new HashSet<>(packageSet.size());

        String[] packageArray = packageSet.toArray(new String[packageSet.size()]);

        Arrays.sort(packageArray, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return (o1.length() > o2.length()) ? 0 : 1;
            }

            @Override
            public boolean equals(Object obj) {
                return false;
            }
        });

        for (Iterator<String> it = packageSet.iterator(); it.hasNext();) {
            String key = it.next();
            for(String packageName : packageArray) {
                if (key.startsWith(packageName)) {

                }
            }
        }
    }
    */
/**
 * 取出所有指定包名及指定jar文件前缀的Class类
 * @return
 * @throws Exception
 */
@Override
public List<Class<?>> getList() throws Exception {
    List<Class<?>> classList = new ArrayList<>();
    for (String packageName : packageSet) {
        Enumeration<URL> urls = PathKit.duang().resource(packageName).paths();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            if (ToolsKit.isEmpty(url)) {
                continue;
            }
            String protocol = url.getProtocol();
            // logger.debug(protocol +"                  "+url.getPath());
            if ("file".equalsIgnoreCase(protocol)) {
                String packagePath = url.getPath().replaceAll("%20", " ");
                addClass(classList, packagePath, packageName);
            } else if ("jar".equalsIgnoreCase(protocol)) {
                // 若在 jar 包中,则解析 jar 包中的 entry
                JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
                JarFile jarFile = jarURLConnection.getJarFile();
                String jarFileName = jarFile.getName();
                int indexOf = jarFileName.lastIndexOf("/");
                if (indexOf == -1) {
                    indexOf = jarFileName.lastIndexOf("\\");
                }
                if (indexOf == -1) {
                    throw new MvcStartUpException("取jar包取时出错");
                }
                jarFileName = jarFileName.substring(indexOf + 1, jarFileName.length());
                boolean isContains = false;
                for (String key : jarNameSet) {
                    if (jarFileName.contains(key)) {
                        isContains = true;
                        break;
                    }
                }
                if (!isContains) {
                    continue;
                }
                Enumeration<JarEntry> jarEntries = jarFile.entries();
                while (jarEntries.hasMoreElements()) {
                    JarEntry jarEntry = jarEntries.nextElement();
                    String fileName = jarEntry.getName();
                    // 是class文件
                    if (fileName.endsWith(".class")) {
                        String suffix = fileName.substring(fileName.lastIndexOf("."));
                        String subFileName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length() - suffix.length());
                        String filePkg = fileName.contains("/") ? fileName.substring(0, fileName.length() - subFileName.length() - suffix.length() - 1).replaceAll("/", ".") : "";
                        // 执行添加类操作
                        doAddClass(classList, filePkg, subFileName, suffix);
                    }
                }
            }
            if (!Const.IS_RELOAD_SCANNING) {
                // 将所有URL添加到集合
                Const.URL_LIST.add(url);
            }
        }
    }
    return classList;
}
Also used : MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException) JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL)

Example 2 with MvcStartUpException

use of com.duangframework.core.exceptions.MvcStartUpException in project duangframework by tcrct.

the class BeanHelper method duang.

public static void duang() {
    // 扫描指定包路径下的类文件,类文件包含有指定的注解类或文件名以指定的字符串结尾的
    Map<String, List<Class<?>>> classMap = ClassScanKit.duang().annotations(InstanceFactory.MVC_ANNOTATION_SET).packages(ConfigKit.duang().key("base.package.path").asArray()).jarname(ConfigKit.duang().key("jar.prefix").asArray()).packages("com.duangframework.mvc").map();
    if (ToolsKit.isNotEmpty(classMap)) {
        String proxyKey = Proxy.class.getSimpleName();
        // 找出所有代理类进行实例化并缓存
        Map<Class<? extends Annotation>, IProxy> annotationMap = new HashMap<>();
        List<Class<?>> proxyList = classMap.get(proxyKey);
        if (ToolsKit.isNotEmpty(proxyList)) {
            for (Class<?> proxyClass : proxyList) {
                Proxy proxy = proxyClass.getAnnotation(Proxy.class);
                if (ToolsKit.isNotEmpty(proxy)) {
                    Class<? extends Annotation> aopClass = proxy.aop();
                    IProxy proxyObj = ClassUtils.newInstance(proxyClass);
                    annotationMap.put(aopClass, proxyObj);
                }
            }
        }
        // 将扫描后的Class进行实例化并缓存(Proxy除外)
        try {
            for (Iterator<Map.Entry<String, List<Class<?>>>> it = classMap.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry<String, List<Class<?>>> entry = it.next();
                String key = entry.getKey();
                if (proxyKey.equals(key)) {
                    continue;
                }
                List<Class<?>> classList = entry.getValue();
                if (ToolsKit.isEmpty(classList)) {
                    continue;
                }
                Map<Class<?>, Object> subBeanMap = BeanUtils.getAllBeanMaps().get(key);
                if (ToolsKit.isEmpty(subBeanMap)) {
                    subBeanMap = new HashMap<>(classList.size());
                }
                for (Class<?> cls : classList) {
                    Object clsObj = createBean(annotationMap, cls);
                    if (clsObj != null) {
                        // 实例化后,用类全名作key, 实例化对象作value缓存起来
                        subBeanMap.put(cls, clsObj);
                    }
                }
                BeanUtils.setAllBeanMaps(key, subBeanMap);
            }
        } catch (Exception e) {
            throw new MvcStartUpException(e.getMessage(), e);
        }
    }
    logger.warn("BeanHelper Success...");
}
Also used : IProxy(com.duangframework.core.interfaces.IProxy) Annotation(java.lang.annotation.Annotation) MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException) IProxy(com.duangframework.core.interfaces.IProxy) Proxy(com.duangframework.core.annotation.aop.Proxy) MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException)

Example 3 with MvcStartUpException

use of com.duangframework.core.exceptions.MvcStartUpException in project duangframework by tcrct.

the class ContextLoaderListener method initContext.

/**
 *  初始化 IDuang
 * @throws Exception
 */
private void initContext() throws Exception {
    String configClass = ConfigKit.duang().key("mvc.config").asString();
    if (ToolsKit.isEmpty(configClass)) {
        throw new MvcStartUpException("IDuang子类路径不能为空!");
    }
    if (null == duangFrameword) {
        try {
            duangFrameword = ObjectKit.newInstance(configClass);
            duangFrameword.addHandlers();
            duangFrameword.addPlugins();
            logger.warn("initContext success");
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            throw new MvcStartUpException("不能创建类: " + configClass, e);
        }
    }
}
Also used : MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException) MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException)

Example 4 with MvcStartUpException

use of com.duangframework.core.exceptions.MvcStartUpException in project duangframework by tcrct.

the class AbstractNettyServer method init.

private void init() {
    if (bootStrap.getPort() == 0) {
        throw new MvcStartUpException("Server Startup Fail: " + bootStrap.getPort() + " is not setting!");
    }
    if (isUse()) {
        throw new MvcStartUpException("Server Startup Fail: " + bootStrap.getPort() + " is use!");
    }
    nettyBootstrap = new ServerBootstrap();
    nettyBootstrap.group(bootStrap.getBossGroup(), bootStrap.getWorkerGroup());
    // 连接数
    nettyBootstrap.option(ChannelOption.SO_BACKLOG, bootStrap.getBockLog()).childOption(ChannelOption.ALLOCATOR, bootStrap.getAllocator()).childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT).childOption(ChannelOption.SO_RCVBUF, 65536).childOption(ChannelOption.SO_SNDBUF, 65536).childOption(ChannelOption.SO_REUSEADDR, // 重用地址
    true).childOption(ChannelOption.SO_KEEPALIVE, // 开启Keep-Alive,长连接
    true).childOption(ChannelOption.TCP_NODELAY, // 不延迟,消息立即发送
    true).childOption(ChannelOption.ALLOW_HALF_CLOSURE, false);
    nettyBootstrap.channel(bootStrap.getDefaultChannel());
}
Also used : MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

MvcStartUpException (com.duangframework.core.exceptions.MvcStartUpException)4 Proxy (com.duangframework.core.annotation.aop.Proxy)1 IProxy (com.duangframework.core.interfaces.IProxy)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Annotation (java.lang.annotation.Annotation)1 JarURLConnection (java.net.JarURLConnection)1 URL (java.net.URL)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1