package com.ikasoa.zk;

import java.util.ArrayList;
import java.util.List;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ikasoa.core.loadbalance.ServerInfo;
import com.ikasoa.core.utils.StringUtil;

/**
 * Zookeeper基础操作类
 * 
 * @author <a href="mailto:larry7696@gmail.com">Larry</a>
 * @version 0.1
 */
public class ZkBase {

	private static final Logger LOG = LoggerFactory.getLogger(ZkBase.class);

	private ZkClient zkClient;

	private String zkNode;

	private List<String> nodeList;

	public final String ZK_ROOT_NODE = "/";

	public ZkBase(String zkServerString, String zkNode) {
		if (StringUtil.isEmpty(zkServerString)) {
			throw new RuntimeException("zkServerString is null !");
		} else {
			zkClient = new ZkClient(zkServerString);
		}
		if (StringUtil.isEmpty(zkNode)) {
			this.zkNode = ZK_ROOT_NODE;
		} else {
			this.zkNode = zkNode;
		}
		zkClient.subscribeDataChanges(zkNode, new IZkDataListener() {

			@Override
			public void handleDataChange(String nodePath, Object nodeObj) throws Exception {
				nodeList = getChildren();
			}

			@Override
			public void handleDataDeleted(String nodePath) throws Exception {
				nodeList = getChildren();
			}

		});

		zkClient.subscribeStateChanges(new IZkStateListener() {

			@Override
			public void handleNewSession() throws Exception {
				nodeList = getChildren();
			}

			@Override
			public void handleSessionEstablishmentError(Throwable arg0) throws Exception {
				nodeList = getChildren();
			}

			@Override
			public void handleStateChanged(KeeperState arg0) throws Exception {
				nodeList = getChildren();
			}

		});

		zkClient.subscribeChildChanges(this.zkNode, new IZkChildListener() {

			@Override
			public void handleChildChange(String parentPath, List<String> currentChildList) throws Exception {
				nodeList = currentChildList;
			}

		});

	}

	public List<ServerInfo> getServerInfoList() {
		List<ServerInfo> serverInfoList = new ArrayList<>();
		List<String> nList = zkClient.getChildren(zkNode);
		for (String n : nList) {
			ZkServerNode zksn = (ZkServerNode) zkClient
					.readData(new StringBuilder(zkNode).append("/").append(n).toString());
			serverInfoList.add(new ServerInfo(zksn.getServerHost(), zksn.getServerPort()));
		}
		LOG.debug("serverInfoList is : " + serverInfoList);
		return serverInfoList;
	}

	public boolean isExistNode(String serverName, String serverHost, int serverPort) {
		if (nodeList == null || nodeList.isEmpty()) {
			nodeList = getChildren();
		}
		for (String n : nodeList) {
			if (n.contains(new StringBuilder(serverName).append("-").append(serverHost).append("-").append(serverPort)
					.toString())) {
				return true;
			}
		}
		return false;
	}

	public List<String> getChildren() {
		return zkClient.getChildren(zkNode);
	}

	public ZkClient getZkClient() {
		return zkClient;
	}

	public void setZkClient(ZkClient zkClient) {
		this.zkClient = zkClient;
	}

	public String getZkNode() {
		return zkNode;
	}

	public void setZkNode(String zkNode) {
		this.zkNode = zkNode;
	}

}

@@ -1312,39 +1312,101 @@
  %7B%0D%0A%09%09%09%09
-nodeList = getChildren(
+LOG.debug(%22handleDataChange (nodePath : %22 + nodePath + %22, nodeObj : %22 + nodeObj + %22)%22
 );%0D%0A%09%09%09%7D
@@ -1497,39 +1497,74 @@
  %7B%0D%0A%09%09%09%09
-nodeList = getChildren(
+LOG.warn(%22handleDataDeleted (nodePath : %22 + nodePath + %22)%22
 );%0D%0A%09%09%09%7D
@@ -1697,32 +1697,68 @@
 ws Exception %7B%0D%0A
+%09%09%09%09LOG.debug(%22handleNewSession%22);%0D%0A
 %09%09%09%09nodeList = g
@@ -1851,20 +1851,17 @@
 rowable 
-arg0
+t
 ) throws
@@ -1878,39 +1878,40 @@
  %7B%0D%0A%09%09%09%09
-nodeList = getChildren(
+LOG.error(t.getMessage()
 );%0D%0A%09%09%09%7D
@@ -1978,12 +1978,13 @@
 ate 
-arg0
+state
 ) th
@@ -2005,39 +2005,70 @@
  %7B%0D%0A%09%09%09%09
-nodeList = getChildren(
+LOG.debug(%22handleStateChanged (state : %22 + state + %22)%22
 );%0D%0A%09%09%09%7D
@@ -2267,24 +2267,144 @@
 xception %7B%0D%0A
+%09%09%09%09LOG.debug(%22handleChildChange (parentPath : %22 + parentPath + %22, currentChildList%22 + currentChildList%0D%0A%09%09%09%09%09%09+ %22)%22);%0D%0A
 %09%09%09%09nodeList

optimize code