package com.nepxion.coroutine.registry.zookeeper;

/**
 * <p>Title: Nepxion Coroutine</p>
 * <p>Description: Nepxion Coroutine For Distribution</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: Nepxion</p>
 * @author Haojun Ren
 * @email 1394997@qq.com
 * @version 1.0
 */

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.nepxion.coroutine.common.constant.CoroutineConstants;
import com.nepxion.coroutine.common.delegate.CoroutineDelegateImpl;
import com.nepxion.coroutine.registry.RegistryEntity;
import com.nepxion.coroutine.registry.RegistryExecutor;
import com.nepxion.coroutine.registry.RegistryInitializer;
import com.nepxion.coroutine.registry.RegistryLauncher;
import com.nepxion.coroutine.registry.zookeeper.common.ZookeeperException;

public class ZookeeperRegistryLauncher extends CoroutineDelegateImpl implements RegistryLauncher {
    private RegistryInitializer registryInitializer;
    private RegistryExecutor registryExecutor;

    private static final Lock lock = new ReentrantLock();

    @Override
    public void start() throws Exception {
        String address = properties.getString(CoroutineConstants.ZOOKEEPER_ADDRESS_ATTRIBUTE_NAME);

        start(address);
    }

    @Override
    public void start(String address) throws Exception {
        try {
            lock.lock();

            RegistryEntity registryEntity = new RegistryEntity();
            registryEntity.setAddress(address);

            if (registryInitializer != null) {
                throw new ZookeeperException("Registry initializer isn't null, it has been initialized already");
            }

            // 启动Zookeeper连接
            registryInitializer = new ZookeeperRegistryInitializer();
            registryInitializer.start(registryEntity, properties);

            registryExecutor = new ZookeeperRegistryExecutor();
            registryExecutor.setRegistryInitializer(registryInitializer);
            registryExecutor.setProperties(properties);
        } finally {
            lock.unlock();
        }
    }

    @Override
    public void stop() throws Exception {
        try {
            lock.lock();

            if (registryInitializer == null) {
                throw new ZookeeperException("Registry initializer is null");
            }

            // 停止Zookeeper连接
            registryInitializer.stop();
        } finally {
            lock.unlock();
        }
    }

    @Override
    public RegistryExecutor getRegistryExecutor() {
        return registryExecutor;
    }
}
@@ -280,98 +280,8 @@
 */%0A%0A
-import java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReentrantLock;%0A%0A
 impo
@@ -637,83 +637,8 @@
 her;
-%0Aimport com.nepxion.coroutine.registry.zookeeper.common.ZookeeperException;
 %0A%0Apu
@@ -758,143 +758,221 @@
 stry
-Initializer registryInitializer;%0A    private RegistryExecutor registryExecutor;%0A%0A    private static final Lock lock = new ReentrantLock
+Entity registryEntity = new RegistryEntity();%0A    private RegistryInitializer registryInitializer = new ZookeeperRegistryInitializer();%0A    private RegistryExecutor registryExecutor = new ZookeeperRegistryExecutor
 ();%0A
@@ -1244,434 +1244,45 @@
     
-try %7B%0A            lock.lock();%0A%0A            RegistryEntity registryEntity = new RegistryEntity();%0A            registryEntity.setAddress(address);%0A%0A            if (registryInitializer != null) %7B%0A                throw new ZookeeperException(%22Registry initializer isn't null, it has been initialized already%22);%0A            %7D%0A%0A            // %E5%90%AF%E5%8A%A8Zookeeper%E8%BF%9E%E6%8E%A5%0A            registryInitializer = new ZookeeperRegistryInitializer();%0A    
+registryEntity.setAddress(address);%0A%0A
     
@@ -1345,76 +1345,8 @@
 );%0A%0A
-            registryExecutor = new ZookeeperRegistryExecutor();%0A    
     
@@ -1411,36 +1411,32 @@
 lizer);%0A        
-    
 registryExecutor
@@ -1467,65 +1467,8 @@
 s);%0A
-        %7D finally %7B%0A            lock.unlock();%0A        %7D%0A
     
@@ -1538,305 +1538,35 @@
     
-try %7B%0A            lock.lock();%0A%0A            if (registryInitializer == null) %7B%0A                throw new ZookeeperException(%22Registry initializer is null%22);%0A            %7D%0A%0A            // %E5%81%9C%E6%AD%A2Zookeeper%E8%BF%9E%E6%8E%A5%0A            registryInitializer.stop();%0A        %7D finally %7B%0A            lock.unlock();%0A        %7D
+registryInitializer.stop();
 %0A   

重构Zookeeper逻辑，加上线程安全机制