Branch 3.5 add copy directory from source to destination

/**
 * Java Zookeeper
 * Edaijia Java access zookeeper library
 *
 * @category  Libraries
 * @package   EdjZk
 * @author    Yu Chao<yuchao@edaijia-inc.cn>
 * @copyright Edaijia GPL 
 * @license   http://www.gnu.org/licenses/licenses.html The GNU License
 */
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package zkcopy;

/**
 *
 * @author yuchao
 */
import java.io.IOException;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.*;
import java.util.*;

/**
 *
 * @author yuchao
 */
//no implements Watcher
public class ZkCopy {
    
    private ZooKeeper zooKeeper = null;
    private String znode = "/Edaijia";
    private List<ACL> acls = null;
    private String sourcePath = null;
    private String destPath = null;

    /**
     * 
     * @return 
     */
    public ZooKeeper getZooKeeper() {
        return this.zooKeeper;
    }

    /**
     * 
     * @param hosts
     * @param znode
     * @throws IOException
     * @throws InterruptedException
     * @throws KeeperException 
     */
    public void connect(String hosts, String znode) throws IOException,
            InterruptedException, KeeperException {
        this.zooKeeper = new ZooKeeper(hosts, 2000, null);//(Watcher) this);
        this.znode = znode;
        this.acls = new ArrayList<>();
        this.zooKeeper.exists(znode, true);
    }
    
    /**
     * 
     * @param znode 
     */
    public void setZnode(String znode){
        this.znode = znode;
    }
    
    /**
     * 
     * @param acls 
     */
    public void setAcls(List<ACL> acls){
        this.acls = acls;
    }
    
    /**
     * 
     * @param sourcePath 
     */
    public void setSourcePath(String sourcePath){
        this.sourcePath = sourcePath;
    }
    
    /**
     * 
     * @param destPath 
     */
    public void setDestPath(String destPath){
        this.destPath = destPath;
    }

    /**
     * 
     * @param data 
     */
    public void setData(byte[] data) {
        try {
            Stat s = this.zooKeeper.exists(znode, false);
            
            this.zooKeeper.setData(znode, data, s.getVersion());
        } catch (KeeperException | InterruptedException e) {
        }
    }
    
    /**
     * 
     * @param path
     * @return 
     */
    public byte[] getData(String path) {
        byte[] data = null;
        try {
            Stat s = this.zooKeeper.exists(path, false);
            data = this.zooKeeper.getData(path, true, s);
        } catch (KeeperException | InterruptedException e) {
        }
        return data;
    }
    
    /**
     * 
     * @param sourcePath
     * @return 
     */
    public List<String> getChildren(String sourcePath) {
        if(znode.equals("")){
            this.znode = sourcePath;
        }
        List<String> children = null;
        try {
            Stat cs = this.zooKeeper.exists(sourcePath, false);
            if(cs != null){
                children = this.zooKeeper.getChildren(sourcePath, true, cs);
            }
        } catch (KeeperException | InterruptedException e) {
            
        }
        return children;

    }
    
    /**
     * 
     * @param destPath
     * @param data
     * @throws Exception 
     */
    public void createZnode(String destPath, byte[] data) 
            throws Exception {
        
        Stat es = this.zooKeeper.exists(destPath, false);
        if(es == null){

        //this.zooKeeper.addAuthInfo("digest", "admin:admin".getBytes());
        this.zooKeeper.create(destPath, data,
                acls, CreateMode.PERSISTENT);
        }

    }
    
    /**
     * 
     * @param sourcePath
     * @param destPath
     * @return
     * @throws Exception 
     */
    public boolean copyZnode(String sourcePath, String destPath) 
            throws Exception{
        
        if(sourcePath.equals(""))
        {
            return false;
        }
        if(destPath.equals(""))
        {
            return false;
        }
        //String source   = this.sourcePath + "/" + child;
        //String dest     = this.destPath + "/" + child;

        byte[] sourcedata = this.getData(sourcePath);
        System.out.println("From "+sourcePath+" To "+destPath+" Set "
                +new String(sourcedata,"UTF-8"));
        this.createZnode(destPath, sourcedata);

        return true;
    }
    
    /**
     * 
     * @param sourcePath
     * @param destPath
     * @throws Exception 
     */
    public void recursionPath(String sourcePath,String destPath) 
            throws Exception {
        this.sourcePath = sourcePath;
        this.destPath   = destPath;
        
        List<String> subPath = this.getChildren(sourcePath);
        System.out.println("list size "+subPath.size());
        System.out.println("list array "+subPath.toString());
        
        String sour = null;
        String dest = null;

        if(subPath != null){
            
            
            for(String subpath:subPath){
                sour = this.sourcePath + "/" + subpath;
                dest = this.destPath + "/" + subpath;
                System.out.println("subpath = "+sour+" To "+dest);
               
                this.copyZnode(sour, dest);
            }
            for(String subpath:subPath){
                sour = this.sourcePath + "/" + subpath;
                dest = this.destPath + "/" + subpath;
                this.recursionPath(sour,dest);
            }
        }
    }

    

    public static void main(String[] args) throws Exception {
        //TODO Auto-generated method stub
        List<ACL> acls = new ArrayList<>();
        /*
        //new一个acl
        //添加第一个id，采用用户名密码形式
        Id id1 = new Id("digest",
                DigestAuthenticationProvider.generateDigest("admin:admin"));
        ACL acl1 = new ACL(ZooDefs.Perms.ALL, id1);
        acls.add(acl1);
        //添加第二个id，所有用户可读权限
        /*/
        Id id2 = new Id("world", "anyone");
        ACL acl2 = new ACL(ZooDefs.Perms.ALL, id2);
        acls.add(acl2);
        
        
        String hostPort = "121.40.173.217:2181";
        String sourcePath = "/Edaijia/devel/config";
        String destPath = "/Edaijia/produ/config";
        System.out.println(hostPort+"=="+sourcePath+"=="+destPath); 
        
        /*
        //System.out.println("args0 = "+args[0]);
        //System.out.println("args1 = "+args[1]);
        String hostPort     = args[0];
        String sourcePath   = args[1];
        String destPath     = args[2];
        System.out.println("===="+args.toString());
        */
        // zk用admin认证，创建/test ZNode。
        int randomint = 0;
        randomint = (int)(Math.random()*100);
        
        String znode = sourcePath+"/chao"+ randomint;
        String data = "data" + (String.valueOf(randomint));

        
        ZkCopy zkcopy = new ZkCopy();
        zkcopy.connect(hostPort, znode);
        zkcopy.setSourcePath(sourcePath);
        zkcopy.setDestPath(destPath);
        zkcopy.setAcls(acls);
        
        zkcopy.recursionPath(sourcePath, destPath);   
    }
}