// Path: zing-common/src/main/java/org/zxc/zing/common/entity/ProviderInfo.java
// public class ProviderInfo {
//     private String address;
//     private int port;
// 
//     public ProviderInfo(String address, int port) {
//         this.address = address;
//         this.port = port;
//     }
// 
//     public int getPort() {
//         return port;
//     }
// 
//     public void setPort(int port) {
//         this.port = port;
//     }
// 
//     public String getAddress() {
//         return address;
//     }
// 
//     public void setAddress(String address) {
//         this.address = address;
//     }
// 
//     @Override
//     public boolean equals(Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
// 
//         ProviderInfo that = (ProviderInfo) o;
// 
//         if (port != that.port) return false;
//         if (!address.equals(that.address)) return false;
// 
//         return true;
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = address.hashCode();
//         result = 31 * result + port;
//         return result;
//     }
// 
//     @Override
//     public String toString() {
//         final StringBuilder sb = new StringBuilder("ProviderInfo{");
//         sb.append("address='").append(address).append('\'');
//         sb.append(", port=").append(port);
//         sb.append('}');
//         return sb.toString();
//     }
// }
// 
// Path: zing-common/src/main/java/org/zxc/zing/common/registry/RegistryManager.java
// public class RegistryManager {
// 
//     private static final Logger log = LoggerFactory.getLogger(RegistryManager.class);
// 
//     private static CuratorFramework client;
// 
//     private static Executor curatorEventThreadPool = Executors.newFixedThreadPool(100);
// 
//     private static volatile boolean started = false;
// 
//     public static void start() throws Exception {
//         if (!started) {
//             synchronized (RegistryManager.class) {
//                 if (!started) {
//                     String zookeeperAddress = ConfigManager.getInstance().getProperty(Constants.ZOOKEEPER_ADDRESS);
// 
//                     RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
//                     client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
// 
//                     client.start();
// 
//                     TreeCache treeCache = TreeCache.newBuilder(client, Constants.SERVICE_ZK_PATH_PREFIX).setCacheData(false).build();
//                     treeCache.getListenable().addListener(new ProviderNodeEventListener(), curatorEventThreadPool);
//                     treeCache.start();
// 
//                     started = client.blockUntilConnected(1000, TimeUnit.MILLISECONDS);
//                 }
//             }
//         }
//     }
// 
//     public static boolean publishService(String serviceName, String ip, int port) {
//         if (!started) {
//             throw new IllegalStateException("registry is not started.");
//         }
// 
//         if (Strings.isNullOrEmpty(ip)) {
//             throw new IllegalStateException("local ip address not configured yet!");
//         }
// 
//         String currentServerPath = ZookeeperPathUtils.formatProviderPath(serviceName, ip, port);
//         try {
//             String result = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(currentServerPath);
//             if (Strings.isNullOrEmpty(result)) {
//                 log.error("error when add service to zk:"+result);
//                 return false;
//             }
//             return true;
//         } catch (KeeperException.NodeExistsException nee) {
//             log.warn("target service "+currentServerPath+" already exists in zk");
//             return true;
//         } catch (Exception e) {
//             log.error("error when add service to zk:"+e.getMessage(), e);
//             return false;
//         }
//     }
// 
//     public static List<ProviderInfo> loadServerListOfService(String serviceName) {
//         if (!started) {
//             throw new IllegalStateException("registry is not started.");
//         }
// 
//         try {
//             List<String> stringList = client.getChildren().watched().forPath(String.format(Constants.SERVICE_ZK_PATH_FORMAT, serviceName));
//             List<ProviderInfo> providerInfoList = Lists.transform(stringList, ServerStr2ProviderInfoTransformer.INSTANCE);
//             return Lists.newArrayList(providerInfoList);
//         } catch (Exception e) {
//             log.error("Error when getting server list from registry:"+e.getMessage(), e);
//             return Collections.emptyList();
//         }
//     }
// }


import org.zxc.zing.common.entity.ProviderInfo;
import org.zxc.zing.common.registry.RegistryManager;
import java.util.List;
package com.zxc.zing.common.registry;



/**
 * Created by xuanchen.zhao on 16-1-17.
 */
public class RegistryManagerTest {

    public static void main(String[] args) throws Exception {
// Path: zing-common/src/main/java/org/zxc/zing/common/entity/ProviderInfo.java
// public class ProviderInfo {
//     private String address;
//     private int port;
// 
//     public ProviderInfo(String address, int port) {
//         this.address = address;
//         this.port = port;
//     }
// 
//     public int getPort() {
//         return port;
//     }
// 
//     public void setPort(int port) {
//         this.port = port;
//     }
// 
//     public String getAddress() {
//         return address;
//     }
// 
//     public void setAddress(String address) {
//         this.address = address;
//     }
// 
//     @Override
//     public boolean equals(Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
// 
//         ProviderInfo that = (ProviderInfo) o;
// 
//         if (port != that.port) return false;
//         if (!address.equals(that.address)) return false;
// 
//         return true;
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = address.hashCode();
//         result = 31 * result + port;
//         return result;
//     }
// 
//     @Override
//     public String toString() {
//         final StringBuilder sb = new StringBuilder("ProviderInfo{");
//         sb.append("address='").append(address).append('\'');
//         sb.append(", port=").append(port);
//         sb.append('}');
//         return sb.toString();
//     }
// }
// 
// Path: zing-common/src/main/java/org/zxc/zing/common/registry/RegistryManager.java
// public class RegistryManager {
// 
//     private static final Logger log = LoggerFactory.getLogger(RegistryManager.class);
// 
//     private static CuratorFramework client;
// 
//     private static Executor curatorEventThreadPool = Executors.newFixedThreadPool(100);
// 
//     private static volatile boolean started = false;
// 
//     public static void start() throws Exception {
//         if (!started) {
//             synchronized (RegistryManager.class) {
//                 if (!started) {
//                     String zookeeperAddress = ConfigManager.getInstance().getProperty(Constants.ZOOKEEPER_ADDRESS);
// 
//                     RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
//                     client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
// 
//                     client.start();
// 
//                     TreeCache treeCache = TreeCache.newBuilder(client, Constants.SERVICE_ZK_PATH_PREFIX).setCacheData(false).build();
//                     treeCache.getListenable().addListener(new ProviderNodeEventListener(), curatorEventThreadPool);
//                     treeCache.start();
// 
//                     started = client.blockUntilConnected(1000, TimeUnit.MILLISECONDS);
//                 }
//             }
//         }
//     }
// 
//     public static boolean publishService(String serviceName, String ip, int port) {
//         if (!started) {
//             throw new IllegalStateException("registry is not started.");
//         }
// 
//         if (Strings.isNullOrEmpty(ip)) {
//             throw new IllegalStateException("local ip address not configured yet!");
//         }
// 
//         String currentServerPath = ZookeeperPathUtils.formatProviderPath(serviceName, ip, port);
//         try {
//             String result = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(currentServerPath);
//             if (Strings.isNullOrEmpty(result)) {
//                 log.error("error when add service to zk:"+result);
//                 return false;
//             }
//             return true;
//         } catch (KeeperException.NodeExistsException nee) {
//             log.warn("target service "+currentServerPath+" already exists in zk");
//             return true;
//         } catch (Exception e) {
//             log.error("error when add service to zk:"+e.getMessage(), e);
//             return false;
//         }
//     }
// 
//     public static List<ProviderInfo> loadServerListOfService(String serviceName) {
//         if (!started) {
//             throw new IllegalStateException("registry is not started.");
//         }
// 
//         try {
//             List<String> stringList = client.getChildren().watched().forPath(String.format(Constants.SERVICE_ZK_PATH_FORMAT, serviceName));
//             List<ProviderInfo> providerInfoList = Lists.transform(stringList, ServerStr2ProviderInfoTransformer.INSTANCE);
//             return Lists.newArrayList(providerInfoList);
//         } catch (Exception e) {
//             log.error("Error when getting server list from registry:"+e.getMessage(), e);
//             return Collections.emptyList();
//         }
//     }
// }


// Path: zing-common/src/test/java/com/zxc/zing/common/registry/RegistryManagerTest.java
import org.zxc.zing.common.entity.ProviderInfo;
import org.zxc.zing.common.registry.RegistryManager;
import java.util.List;

package com.zxc.zing.common.registry;



/**
 * Created by xuanchen.zhao on 16-1-17.
 */
public class RegistryManagerTest {

    public static void main(String[] args) throws Exception {
        RegistryManager.start();