// Path: naive-core/src/main/java/com/goribun/naive/core/constants/SysErCode.java
// public enum SysErCode {
//     IO_ERROR(0001, "IO异常"),
//     OK_HTTP_ERROR(0002, "okhttp请求错误"),
//     RPC_ERROR(0003, "RPC调用失败"),
//     INIT_SERVER_ERR0R(0004, "初始化服务端失败"),
//     PROVIDE_ERR0R(0005, "提供rpc服务失败"),
//     ZK_ERR0R(0006, "zookeeper服务失败");
//     private int erCode;
//     private String msg;
// 
//     SysErCode(int erCode, String msg) {
//         this.erCode = erCode;
//         this.msg = msg;
//     }
// 
//     public int getErCode() {
//         return erCode;
//     }
// 
//     public String getMsg() {
//         return msg;
//     }
// 
//     public SysErCode getSysErCode(int erCode) {
//         for (SysErCode sys : SysErCode.values()) {
//             if (sys.getErCode() == erCode) {
//                 return sys;
//             }
//         }
//         return null;
//     }
// }
// 
// Path: naive-core/src/main/java/com/goribun/naive/core/exception/SysException.java
// public class SysException extends RuntimeException {
// 
//     private static final long serialVersionUID = 2363088507608422727L;
// 
//     private int errorCode;
// 
//     public SysException(SysErCode sysErCode) {
//         super(sysErCode.getMsg());
//         this.errorCode = sysErCode.getErCode();
//     }
// 
//     public SysException(SysErCode sysErCode, Throwable cause) {
//         super(sysErCode.getMsg(), cause);
//         this.errorCode = sysErCode.getErCode();
//     }
// }
// 
// Path: naive-manage/src/main/java/com/goribun/navie/manage/regist/IZkClient.java
// public interface IZkClient {
//     void connection();
// 
//     void destroy();
// 
//     ZooKeeper getZkConnect();
// }


import java.util.Map;
import com.google.common.base.Strings;
import com.goribun.naive.core.constants.SysErCode;
import com.goribun.naive.core.exception.SysException;
import com.goribun.naive.server.annotations.RpcServer;
import com.goribun.navie.manage.regist.IZkClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
package com.goribun.naive.server.plugings;



/**
 * @author chenchuan
 */
public class ServerBeanWareListener implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext cxf = event.getApplicationContext();

        Map<String, Object> map = cxf.getBeansWithAnnotation(RpcServer.class);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            //            Object impl = entry.getValue();
            //
            //            RpcServer rpcServer = impl.getClass().getAnnotation(RpcServer.class);
            //            Class[] interfaces = impl.getClass().getInterfaces();
            //            String key = Strings.isNullOrEmpty(rpcServer.value()) ? interfaces[0].getName() : rpcServer
            // .value();
            //
            //            //RPC的实现类只能实现一个接口,不能多继承接口,否则报错
            //            if (null == interfaces || interfaces.length > 1 || null != ServiceContext.getServer(key)) {
            //                throw new SysException(SysErCode.INIT_SERVER_ERR0R);
            //            }
            //
            //            ServiceContext.putService(key, impl);
            //
            //            String isRegistServer = cxf.getEnvironment().getProperty("regist.switch");
            //            if (isRegistServer.equalsIgnoreCase("true")) {
            //                IZkClient zkClient = cxf.getBean(IZkClient.class);
            //                String projectName = cxf.getEnvironment().getProperty("project.name");
            //                new RegistServerThread(zkClient, projectName).start();
            //            }

        }
    }

    public void regitServer(IZkClient client, String projectName) {
        if (null == client || Strings.isNullOrEmpty(projectName)) {
// Path: naive-core/src/main/java/com/goribun/naive/core/constants/SysErCode.java
// public enum SysErCode {
//     IO_ERROR(0001, "IO异常"),
//     OK_HTTP_ERROR(0002, "okhttp请求错误"),
//     RPC_ERROR(0003, "RPC调用失败"),
//     INIT_SERVER_ERR0R(0004, "初始化服务端失败"),
//     PROVIDE_ERR0R(0005, "提供rpc服务失败"),
//     ZK_ERR0R(0006, "zookeeper服务失败");
//     private int erCode;
//     private String msg;
// 
//     SysErCode(int erCode, String msg) {
//         this.erCode = erCode;
//         this.msg = msg;
//     }
// 
//     public int getErCode() {
//         return erCode;
//     }
// 
//     public String getMsg() {
//         return msg;
//     }
// 
//     public SysErCode getSysErCode(int erCode) {
//         for (SysErCode sys : SysErCode.values()) {
//             if (sys.getErCode() == erCode) {
//                 return sys;
//             }
//         }
//         return null;
//     }
// }
// 
// Path: naive-core/src/main/java/com/goribun/naive/core/exception/SysException.java
// public class SysException extends RuntimeException {
// 
//     private static final long serialVersionUID = 2363088507608422727L;
// 
//     private int errorCode;
// 
//     public SysException(SysErCode sysErCode) {
//         super(sysErCode.getMsg());
//         this.errorCode = sysErCode.getErCode();
//     }
// 
//     public SysException(SysErCode sysErCode, Throwable cause) {
//         super(sysErCode.getMsg(), cause);
//         this.errorCode = sysErCode.getErCode();
//     }
// }
// 
// Path: naive-manage/src/main/java/com/goribun/navie/manage/regist/IZkClient.java
// public interface IZkClient {
//     void connection();
// 
//     void destroy();
// 
//     ZooKeeper getZkConnect();
// }


// Path: naive-server/src/main/java/com/goribun/naive/server/plugings/ServerBeanWareListener.java
import java.util.Map;
import com.google.common.base.Strings;
import com.goribun.naive.core.constants.SysErCode;
import com.goribun.naive.core.exception.SysException;
import com.goribun.naive.server.annotations.RpcServer;
import com.goribun.navie.manage.regist.IZkClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

package com.goribun.naive.server.plugings;



/**
 * @author chenchuan
 */
public class ServerBeanWareListener implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext cxf = event.getApplicationContext();

        Map<String, Object> map = cxf.getBeansWithAnnotation(RpcServer.class);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            //            Object impl = entry.getValue();
            //
            //            RpcServer rpcServer = impl.getClass().getAnnotation(RpcServer.class);
            //            Class[] interfaces = impl.getClass().getInterfaces();
            //            String key = Strings.isNullOrEmpty(rpcServer.value()) ? interfaces[0].getName() : rpcServer
            // .value();
            //
            //            //RPC的实现类只能实现一个接口,不能多继承接口,否则报错
            //            if (null == interfaces || interfaces.length > 1 || null != ServiceContext.getServer(key)) {
            //                throw new SysException(SysErCode.INIT_SERVER_ERR0R);
            //            }
            //
            //            ServiceContext.putService(key, impl);
            //
            //            String isRegistServer = cxf.getEnvironment().getProperty("regist.switch");
            //            if (isRegistServer.equalsIgnoreCase("true")) {
            //                IZkClient zkClient = cxf.getBean(IZkClient.class);
            //                String projectName = cxf.getEnvironment().getProperty("project.name");
            //                new RegistServerThread(zkClient, projectName).start();
            //            }

        }
    }

    public void regitServer(IZkClient client, String projectName) {
        if (null == client || Strings.isNullOrEmpty(projectName)) {
            throw new SysException(SysErCode.PROVIDE_ERR0R);