// Path: naive-server/src/main/java/com/goribun/naive/server/context/ServiceContext.java
// public class ServiceContext {
// 
//     private ServiceContext() {
//     }
// 
//     private static Map<String, Class<?>> serviceMap = new ConcurrentHashMap<>();
// 
//     public static Class<?> getServer(String className) {
//         return serviceMap.get(className);
//     }
// 
//     public static void putService(String className, Class<?> obj) {
//         serviceMap.put(className, obj);
//     }
// }
// 
// Path: naive-server/src/main/java/com/goribun/naive/server/utils/RPCServiceScanner.java
// public class RPCServiceScanner {
// 
//     private RPCServiceScanner() {
//     }
// 
//     //默认包扫描路径
//     private static final String DEFAULT_SCAN_PACKAGE = "cn.wangxs.crm.service.*";
// 
//     public static List<Class<?>> scan() {
// 
//         PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//         MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
// 
//         //优先取得自定义配置
//         String pkgString = PropertyUtil.getProperty(Const.SCAN_PACKAGE_KEY_, DEFAULT_SCAN_PACKAGE);
// 
//         List<Class<?>> list = new ArrayList<>();
//         //多个路径拆分成路径数组
//         String[] pkgArray = StringUtils.tokenizeToStringArray(pkgString, ConfigurableApplicationContext
//                 .CONFIG_LOCATION_DELIMITERS);
// 
//         for (String pkg : pkgArray) {
//             //类名转资源路径
//             String resourcePath = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils
//                     .resolvePlaceholders(pkg)) + ".class";
// 
//             try {
//                 //根据资源路径取得资源数组
//                 Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
//                         + resourcePath);
// 
//                 for (Resource resource : resources) {
//                     if (resource.isReadable()) {
//                         MetadataReader reader = readerFactory.getMetadataReader(resource);
//                         String className = reader.getClassMetadata().getClassName();
//                         Class<?> clazz = null;
//                         try {
//                             clazz = Class.forName(className);
//                         } catch (ClassNotFoundException e) {
//                             e.printStackTrace();
//                         }
//                         //判断是否带有RpcServer注解
//                         if (clazz != null && clazz.getAnnotation(RpcService.class) != null) {
//                             list.add(clazz);
//                         }
//                     }
//                 }
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return list;
//     }
// 
// }


import java.util.List;
import com.goribun.naive.core.annotation.RpcService;
import com.goribun.naive.server.context.ServiceContext;
import com.goribun.naive.server.utils.RPCServiceScanner;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
package com.goribun.naive.server.listener;



/**
 * 单独的listener，需要在web.xml进行配置
 * <p>
 * 加载所有@RpcService的接口信息
 *
 * @author chenchuan
 */
public class NaiveAloneListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        //扫描所有服务类
        List<Class<?>> serviceClassList = RPCServiceScanner.scan();

        //缓存service的class信息
        for (Class<?> clazz : serviceClassList) {
            String value = clazz.getAnnotation(RpcService.class).value();
            String key = StringUtils.isNotBlank(value) ? value : clazz.getName();
// Path: naive-server/src/main/java/com/goribun/naive/server/context/ServiceContext.java
// public class ServiceContext {
// 
//     private ServiceContext() {
//     }
// 
//     private static Map<String, Class<?>> serviceMap = new ConcurrentHashMap<>();
// 
//     public static Class<?> getServer(String className) {
//         return serviceMap.get(className);
//     }
// 
//     public static void putService(String className, Class<?> obj) {
//         serviceMap.put(className, obj);
//     }
// }
// 
// Path: naive-server/src/main/java/com/goribun/naive/server/utils/RPCServiceScanner.java
// public class RPCServiceScanner {
// 
//     private RPCServiceScanner() {
//     }
// 
//     //默认包扫描路径
//     private static final String DEFAULT_SCAN_PACKAGE = "cn.wangxs.crm.service.*";
// 
//     public static List<Class<?>> scan() {
// 
//         PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//         MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
// 
//         //优先取得自定义配置
//         String pkgString = PropertyUtil.getProperty(Const.SCAN_PACKAGE_KEY_, DEFAULT_SCAN_PACKAGE);
// 
//         List<Class<?>> list = new ArrayList<>();
//         //多个路径拆分成路径数组
//         String[] pkgArray = StringUtils.tokenizeToStringArray(pkgString, ConfigurableApplicationContext
//                 .CONFIG_LOCATION_DELIMITERS);
// 
//         for (String pkg : pkgArray) {
//             //类名转资源路径
//             String resourcePath = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils
//                     .resolvePlaceholders(pkg)) + ".class";
// 
//             try {
//                 //根据资源路径取得资源数组
//                 Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
//                         + resourcePath);
// 
//                 for (Resource resource : resources) {
//                     if (resource.isReadable()) {
//                         MetadataReader reader = readerFactory.getMetadataReader(resource);
//                         String className = reader.getClassMetadata().getClassName();
//                         Class<?> clazz = null;
//                         try {
//                             clazz = Class.forName(className);
//                         } catch (ClassNotFoundException e) {
//                             e.printStackTrace();
//                         }
//                         //判断是否带有RpcServer注解
//                         if (clazz != null && clazz.getAnnotation(RpcService.class) != null) {
//                             list.add(clazz);
//                         }
//                     }
//                 }
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return list;
//     }
// 
// }


// Path: naive-server/src/main/java/com/goribun/naive/server/listener/NaiveAloneListener.java
import java.util.List;
import com.goribun.naive.core.annotation.RpcService;
import com.goribun.naive.server.context.ServiceContext;
import com.goribun.naive.server.utils.RPCServiceScanner;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

package com.goribun.naive.server.listener;



/**
 * 单独的listener，需要在web.xml进行配置
 * <p>
 * 加载所有@RpcService的接口信息
 *
 * @author chenchuan
 */
public class NaiveAloneListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        //扫描所有服务类
        List<Class<?>> serviceClassList = RPCServiceScanner.scan();

        //缓存service的class信息
        for (Class<?> clazz : serviceClassList) {
            String value = clazz.getAnnotation(RpcService.class).value();
            String key = StringUtils.isNotBlank(value) ? value : clazz.getName();
            ServiceContext.putService(key, clazz);