// Path: monica-cluster/src/main/java/monica/cluster/loadbalance/LoadBalanceHandler.java
// public class LoadBalanceHandler {
// 	private String TEMP_PROVIDER_CACHE = "temp_providers";
// 	private String SERVER_STRING = "server_string";
// 
// 	private LoadBalance defaultAlgorithm() {
// 		return new RandomLoadBalance();
// 	}
// 
// 	public void handle() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// 		List<String> providers = (List) RegistryContext.clientCache.get(TEMP_PROVIDER_CACHE);
// 		final String CLIENT_KEY = "client";
// 		YamlClient client = (YamlClient) ConfigurationContext.propMap.get(CLIENT_KEY);
// 		String loadBalanceName = client.getLoadbalance();
// 		LoadBalance balance = (LoadBalance) this.getClass().getClassLoader().loadClass(loadBalanceName).newInstance();
// 		int index = balance.doSelect(providers);
// 		String selectedString = providers.get(index);
// 		// providers.clear();
// 		// providers.add(selectedString);
// 	}
// }
// 
// Path: monica-cluster/src/main/java/monica/cluster/router/RouteHandler.java
// public class RouteHandler {
// 	private List<Rule> rulesList = new CopyOnWriteArrayList<Rule>();
// 	private final String RULES_PATH = "/routers";
// 
// 	public RouteHandler() {
// 		super();
// 		rulesInit();
// 	}
// 
// 	private void rulesInit() {
// 		final CuratorFramework zkClient = (CuratorFramework) RegistryContext.clientCache.get("curatorClient");
// 		try {
// 			if (null != zkClient.checkExists().forPath(RULES_PATH)) {
// 				List<String> childList = zkClient.getChildren().forPath(RULES_PATH);
// 				for (String rule : childList) {
// 					StringToObjectParser stringParser = new StringToObjectParser();
// 					rulesList.add(stringParser.parseRuleStringToRule(URLDecoder.decode(rule, "UTF-8")));
// 				}
// 			}
// 			filterRulesForConsumer();
// 		} catch (Exception e) {
// 			e.printStackTrace();
// 		}
// 	}
// 
// 	public void handle() {
// 		rulesList.sort(new Comparator() {
// 			public int compare(Object o1, Object o2) {
// 				return ((Rule) o1).compareTo(((Rule) o2));
// 			}
// 
// 		});
// 		for (Rule s : rulesList) {
// 			try {
// 				IpParser.class.getMethod(s.getSource().getOps().name().toLowerCase() + "Consumer", s.getClass());
// 			} catch (NoSuchMethodException e) {
// 				// TODO Auto-generated catch block
// 				e.printStackTrace();
// 			} catch (SecurityException e) {
// 				// TODO Auto-generated catch block
// 				e.printStackTrace();
// 			}
// 		}
// 
// 	}
// 
// 	/**
// 	 * conditions: localhost.IP, localhost.ip.net.* , , 127.0.0.1
// 	 */
// 
// 	private void filterRulesForConsumer() throws UnknownHostException {
// 		for (Rule rule : rulesList) {
// 			if (!isMatched(rule)) {
// 				rulesList.remove(rule);
// 			}
// 		}
// 	}
// 
// 	private boolean isMatched(Rule rule) throws UnknownHostException {
// 		String localIp = InetAddress.getLocalHost().getHostAddress();
// 		String netSegment = localIp.substring(0, localIp.lastIndexOf(".")) + ".*";
// 		// not allowed loopback address
// 		String loopbackIp = "127.0.0.1";
// 		String emptyIsAll = " ";
// 		if (null != rule) {
// 			String srcString = rule.getSource().toString();// .substring(rule.indexOf("'")+1,rule.indexOf("=>"));
// 			// System.out.println("srcString ------------ "+srcString+"
// 			// "+netSegment);
// 			if (srcString.contains(localIp) || srcString.contains(netSegment) || srcString.contains(loopbackIp)
// 					|| srcString.contains(emptyIsAll)) {
// 				return true;
// 			}
// 		}
// 		return false;
// 	}
// 
// 	public static void main(String args[]) {
// 		try {
// 			ConfigurationContext.loadYamlClientConfig();
// 		} catch (FileNotFoundException e) {
// 			// TODO Auto-generated catch block
// 			e.printStackTrace();
// 		} catch (IOException e) {
// 			// TODO Auto-generated catch block
// 			e.printStackTrace();
// 		}
// 		new ZookeeperMonicaClient().start();
// 		RouteHandler router = new RouteHandler();
// 		router.handle();
// 		System.out.println("rules.size ------------  " + router.rulesList.size());
// 	}
// }
// 
// Path: monica-registry/src/main/java/monica/registry/context/RegistryContext.java
// public class RegistryContext {
// 	// caheKey: curatorClient providers server usefulProviders
// 	public static ConcurrentHashMap<String, Object> clientCache = new ConcurrentHashMap<String, Object>();
// }


import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import monica.cluster.loadbalance.LoadBalanceHandler;
import monica.cluster.router.RouteHandler;
import monica.registry.context.RegistryContext;

package monica.coordinator.impl;




/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
public class ServiceBuilder {
	private final String TEMP_PROVIDERS = "temp_providers";
	private final String PROVIDER_CACHE = "providers";
	private final String SERVER_IP_CACHE = "server_ip";
	private final String SERVER_PORT_CACHE = "server_port";

	public ServiceBuilder servicesInit() throws Exception {

// Path: monica-cluster/src/main/java/monica/cluster/loadbalance/LoadBalanceHandler.java
// public class LoadBalanceHandler {
// 	private String TEMP_PROVIDER_CACHE = "temp_providers";
// 	private String SERVER_STRING = "server_string";
// 
// 	private LoadBalance defaultAlgorithm() {
// 		return new RandomLoadBalance();
// 	}
// 
// 	public void handle() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// 		List<String> providers = (List) RegistryContext.clientCache.get(TEMP_PROVIDER_CACHE);
// 		final String CLIENT_KEY = "client";
// 		YamlClient client = (YamlClient) ConfigurationContext.propMap.get(CLIENT_KEY);
// 		String loadBalanceName = client.getLoadbalance();
// 		LoadBalance balance = (LoadBalance) this.getClass().getClassLoader().loadClass(loadBalanceName).newInstance();
// 		int index = balance.doSelect(providers);
// 		String selectedString = providers.get(index);
// 		// providers.clear();
// 		// providers.add(selectedString);
// 	}
// }
// 
// Path: monica-cluster/src/main/java/monica/cluster/router/RouteHandler.java
// public class RouteHandler {
// 	private List<Rule> rulesList = new CopyOnWriteArrayList<Rule>();
// 	private final String RULES_PATH = "/routers";
// 
// 	public RouteHandler() {
// 		super();
// 		rulesInit();
// 	}
// 
// 	private void rulesInit() {
// 		final CuratorFramework zkClient = (CuratorFramework) RegistryContext.clientCache.get("curatorClient");
// 		try {
// 			if (null != zkClient.checkExists().forPath(RULES_PATH)) {
// 				List<String> childList = zkClient.getChildren().forPath(RULES_PATH);
// 				for (String rule : childList) {
// 					StringToObjectParser stringParser = new StringToObjectParser();
// 					rulesList.add(stringParser.parseRuleStringToRule(URLDecoder.decode(rule, "UTF-8")));
// 				}
// 			}
// 			filterRulesForConsumer();
// 		} catch (Exception e) {
// 			e.printStackTrace();
// 		}
// 	}
// 
// 	public void handle() {
// 		rulesList.sort(new Comparator() {
// 			public int compare(Object o1, Object o2) {
// 				return ((Rule) o1).compareTo(((Rule) o2));
// 			}
// 
// 		});
// 		for (Rule s : rulesList) {
// 			try {
// 				IpParser.class.getMethod(s.getSource().getOps().name().toLowerCase() + "Consumer", s.getClass());
// 			} catch (NoSuchMethodException e) {
// 				// TODO Auto-generated catch block
// 				e.printStackTrace();
// 			} catch (SecurityException e) {
// 				// TODO Auto-generated catch block
// 				e.printStackTrace();
// 			}
// 		}
// 
// 	}
// 
// 	/**
// 	 * conditions: localhost.IP, localhost.ip.net.* , , 127.0.0.1
// 	 */
// 
// 	private void filterRulesForConsumer() throws UnknownHostException {
// 		for (Rule rule : rulesList) {
// 			if (!isMatched(rule)) {
// 				rulesList.remove(rule);
// 			}
// 		}
// 	}
// 
// 	private boolean isMatched(Rule rule) throws UnknownHostException {
// 		String localIp = InetAddress.getLocalHost().getHostAddress();
// 		String netSegment = localIp.substring(0, localIp.lastIndexOf(".")) + ".*";
// 		// not allowed loopback address
// 		String loopbackIp = "127.0.0.1";
// 		String emptyIsAll = " ";
// 		if (null != rule) {
// 			String srcString = rule.getSource().toString();// .substring(rule.indexOf("'")+1,rule.indexOf("=>"));
// 			// System.out.println("srcString ------------ "+srcString+"
// 			// "+netSegment);
// 			if (srcString.contains(localIp) || srcString.contains(netSegment) || srcString.contains(loopbackIp)
// 					|| srcString.contains(emptyIsAll)) {
// 				return true;
// 			}
// 		}
// 		return false;
// 	}
// 
// 	public static void main(String args[]) {
// 		try {
// 			ConfigurationContext.loadYamlClientConfig();
// 		} catch (FileNotFoundException e) {
// 			// TODO Auto-generated catch block
// 			e.printStackTrace();
// 		} catch (IOException e) {
// 			// TODO Auto-generated catch block
// 			e.printStackTrace();
// 		}
// 		new ZookeeperMonicaClient().start();
// 		RouteHandler router = new RouteHandler();
// 		router.handle();
// 		System.out.println("rules.size ------------  " + router.rulesList.size());
// 	}
// }
// 
// Path: monica-registry/src/main/java/monica/registry/context/RegistryContext.java
// public class RegistryContext {
// 	// caheKey: curatorClient providers server usefulProviders
// 	public static ConcurrentHashMap<String, Object> clientCache = new ConcurrentHashMap<String, Object>();
// }


// Path: monica-coordinator/src/main/java/monica/coordinator/impl/ServiceBuilder.java
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import monica.cluster.loadbalance.LoadBalanceHandler;
import monica.cluster.router.RouteHandler;
import monica.registry.context.RegistryContext;

package monica.coordinator.impl;




/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
public class ServiceBuilder {
	private final String TEMP_PROVIDERS = "temp_providers";
	private final String PROVIDER_CACHE = "providers";
	private final String SERVER_IP_CACHE = "server_ip";
	private final String SERVER_PORT_CACHE = "server_port";

	public ServiceBuilder servicesInit() throws Exception {

		final CuratorFramework zkClient = (CuratorFramework) RegistryContext.clientCache.get("curatorClient");
