/**
 * Copyright 1999-2014 dangdang.com.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dangdang.config.service.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.ZKPaths;

import com.dangdang.config.service.ConfigProfile;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

/**
 * 基本配置
 * 
 * @author <a href="mailto:wangyuxuan@dangdang.com">Yuxuan Wang</a>
 *
 */
public class ZookeeperConfigProfile extends ConfigProfile {

	/**
	 * zookeeper地址
	 */
	private final String connectStr;

	/**
	 * 项目配置根目录
	 */
	private final String rootNode;

	/**
	 * 重试策略
	 */
	private final RetryPolicy retryPolicy;

	/**
	 * 是否打开本地文件缓存
	 */
	private final boolean openLocalCache;

	/**
	 * 本地缓存文件目录
	 */
	private String localCacheFolder = "/config-service";

	/**
	 * 一致性检查, 主动检查本地数据与zk中心数据的一致性, 防止出现因连接中断而丢失更新消息, 默认开启
	 */
	private boolean consistencyCheck = true;

	/**
	 * 检查频率, in milliseconds
	 */
	private long consistencyCheckRate = 60 * 1000;

	public ZookeeperConfigProfile(final String connectStr, final String rootNode, final boolean openLocalCache) {
		this(connectStr, rootNode, null, openLocalCache, new ExponentialBackoffRetry(100, 2));
	}

	public ZookeeperConfigProfile(final String connectStr, final String rootNode, final String version) {
		this(connectStr, rootNode, version, false, new ExponentialBackoffRetry(100, 2));
	}

	public ZookeeperConfigProfile(final String connectStr, final String rootNode, final String version, final boolean openLocalCache,
			final RetryPolicy retryPolicy) {
		super(version);
		this.connectStr = Preconditions.checkNotNull(connectStr);
		this.rootNode = Preconditions.checkNotNull(rootNode);
		this.openLocalCache = openLocalCache;
		this.retryPolicy = Preconditions.checkNotNull(retryPolicy);
	}

	public String getConnectStr() {
		return connectStr;
	}

	public String getRootNode() {
		return rootNode;
	}

	public RetryPolicy getRetryPolicy() {
		return retryPolicy;
	}

	public final boolean isConsistencyCheck() {
		return consistencyCheck;
	}

	public final void setConsistencyCheck(boolean consistencyCheck) {
		this.consistencyCheck = consistencyCheck;
	}

	public final long getConsistencyCheckRate() {
		return consistencyCheckRate;
	}

	public final void setConsistencyCheckRate(long consistencyCheckRate) {
		this.consistencyCheckRate = consistencyCheckRate;
	}

	public final boolean isOpenLocalCache() {
		return openLocalCache;
	}

	public final String getLocalCacheFolder() {
		return localCacheFolder;
	}

	public final void setLocalCacheFolder(String localCacheFolder) {
		this.localCacheFolder = localCacheFolder;
	}

	public String getVersionedRootNode() {
		if(Strings.isNullOrEmpty(version)){
			return rootNode;
		}
		return ZKPaths.makePath(rootNode, version);
	}

	@Override
	public String toString() {
		return "ConfigProfile [connectStr=" + connectStr + ", rootNode=" + rootNode + ", version=" + version + ", retryPolicy=" + retryPolicy
				+ ", openLocalCache=" + openLocalCache + ", localCacheFolder=" + localCacheFolder + ", consistencyCheck=" + consistencyCheck
				+ ", consistencyCheckRate=" + consistencyCheckRate + "]";
	}

}

@@ -1256,151 +1256,8 @@
 y;%0A%0A
-%09/**%0A%09 * %E6%98%AF%E5%90%A6%E6%89%93%E5%BC%80%E6%9C%AC%E5%9C%B0%E6%96%87%E4%BB%B6%E7%BC%93%E5%AD%98%0A%09 */%0A%09private final boolean openLocalCache;%0A%0A%09/**%0A%09 * %E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%0A%09 */%0A%09private String localCacheFolder = %22/config-service%22;%0A%0A
 %09/**
@@ -2144,48 +2144,8 @@
 e);%0A
-%09%09this.openLocalCache = openLocalCache;%0A
 %09%09th
@@ -2788,270 +2788,8 @@
 %09%7D%0A%0A
-%09public final boolean isOpenLocalCache() %7B%0A%09%09return openLocalCache;%0A%09%7D%0A%0A%09public final String getLocalCacheFolder() %7B%0A%09%09return localCacheFolder;%0A%09%7D%0A%0A%09public final void setLocalCacheFolder(String localCacheFolder) %7B%0A%09%09this.localCacheFolder = localCacheFolder;%0A%09%7D%0A%0A
 %09pub
@@ -2828,16 +2828,17 @@
 ) %7B%0A%09%09if
+ 
 (Strings
@@ -2861,16 +2861,17 @@
 ersion))
+ 
 %7B%0A%09%09%09ret
@@ -2987,16 +2987,25 @@
 return %22
+Zookeeper
 ConfigPr
@@ -3072,33 +3072,8 @@
 + %22,
- version=%22 + version + %22,
  ret
@@ -3100,94 +3100,8 @@
 licy
-%0A%09%09%09%09+ %22, openLocalCache=%22 + openLocalCache + %22, localCacheFolder=%22 + localCacheFolder
  + %22
@@ -3120,17 +3120,21 @@
 yCheck=%22
- 
+%0A%09%09%09%09
 + consis
@@ -3143,21 +3143,17 @@
 ncyCheck
-%0A%09%09%09%09
+ 
 + %22, con

remove local cache configs in zookeeper config profile