GitHub user yuchao86 opened a pull request:

    https://github.com/apache/zookeeper/pull/23

    Branch 3.5 add copy directory from source to destination

    /**
     * Java Zookeeper
     * Edaijia Java access zookeeper library
     *
     * @category  Libraries
     * @package   EdjZk
     * @author    Yu Chao<[email protected]>
     * @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);   
        }
    }


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/apache/zookeeper branch-3.5

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/zookeeper/pull/23.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #23
    
----
commit 47d8b90c64f7634ab6bb69ef239538943f328fc6
Author: Patrick D. Hunt <[email protected]>
Date:   2014-08-01T21:42:37Z

    Branching for 3.5 releases
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1615238 
13f79535-47bb-0310-9956-ffa450edef68

commit ecd7b9f8854b5c60dff6beacae606d2c609c2641
Author: Patrick D. Hunt <[email protected]>
Date:   2014-08-01T22:08:24Z

    Preparing for release 3.5.0
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1615247 
13f79535-47bb-0310-9956-ffa450edef68

commit ee81cff7c752f5a0891ddd7d89ec4f64ee70db67
Author: Patrick D. Hunt <[email protected]>
Date:   2014-08-01T22:14:48Z

    Preparing for release 3.5.0
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1615252 
13f79535-47bb-0310-9956-ffa450edef68

commit a7c2baa5d67b075fbe83c024d0f620a3a732216a
Author: Patrick D. Hunt <[email protected]>
Date:   2014-08-06T04:29:48Z

    Setup for 3.5.1 development activities
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1616091 
13f79535-47bb-0310-9956-ffa450edef68

commit efe070308e8fbdb9fdad915ba1b687f27dbea122
Author: Alexander Shraer <[email protected]>
Date:   2014-08-14T06:14:38Z

    ZOOKEEPER-1994. Auto-backup configuration files; config version becomes 
part of filename (Hongchao Deng via shralex)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1617887 
13f79535-47bb-0310-9956-ffa450edef68

commit 1775bbc78eb6f8402ed458b4a250316838e57adf
Author: Alexander Shraer <[email protected]>
Date:   2014-08-14T07:33:58Z

    ZOOKEEPER-2008. Missing leader election port in system test. (Kfir Lev-Ari 
via Alex Shraer).
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1617893 
13f79535-47bb-0310-9956-ffa450edef68

commit 8ad913d543ca78e385f3e6da9932e155f5c532bb
Author: Alexander Shraer <[email protected]>
Date:   2014-08-14T12:34:23Z

    ZOOKEEPER-2008-Fix. Correcting small issue in committed 2008 patch.
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1617922 
13f79535-47bb-0310-9956-ffa450edef68

commit 67967b24fae8dbc93f40fc8b7501dc7c4cc78e37
Author: Alexander Shraer <[email protected]>
Date:   2014-08-19T20:52:11Z

    ZOOKEEPER-2006. Standalone mode won't take client port from dynamic config. 
(Hongchao Deng via Alex Shraer)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1618978 
13f79535-47bb-0310-9956-ffa450edef68

commit 6277c4bbc28eedfbb39ef84cc89a32d79c6cfad1
Author: Alexander Shraer <[email protected]>
Date:   2014-08-20T17:19:38Z

    ZOOKEEPER-2013. Typos in programmer guide. (Tim Chambers via Alex Shraer)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1619167 
13f79535-47bb-0310-9956-ffa450edef68

commit ca42b061cf50115baf6e7ae9581d222e4e56990e
Author: Michi Mutsuzaki <[email protected]>
Date:   2014-08-21T01:36:47Z

    ZOOKEEPER-2000. Fix ReconfigTest.testPortChange (Alexander Shraer via 
michim)
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1619278 
13f79535-47bb-0310-9956-ffa450edef68

commit 19d0c875cfaaf7032a5fb3cbc5525f25df0a76a4
Author: Michi Mutsuzaki <[email protected]>
Date:   2014-08-24T05:44:25Z

    ZOOKEEPER-2017 New tests for reconfig failure cases (Alexander Shraer and 
Hongchao Deng via michim)
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1620112 
13f79535-47bb-0310-9956-ffa450edef68

commit 1b56170b439647f39eb25eb85fc6b7035aef082a
Author: Alexander Shraer <[email protected]>
Date:   2014-08-29T14:36:46Z

    ZOOKEEPER-1660 Documentation for Dynamic Reconfiguration (Reed 
Wanderman-Milne via shralex)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1621314 
13f79535-47bb-0310-9956-ffa450edef68

commit 3a067661c9f73681743d284dc59b3f0ecf311b24
Author: Alexander Shraer <[email protected]>
Date:   2014-09-10T07:03:01Z

    ZOOKEEPER-2032. Cleaning up resources in ReconfigBackupTest (Hongchao Deng 
via shralex)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1623918 
13f79535-47bb-0310-9956-ffa450edef68

commit 5aac8accd10701064ea2d3d9a633dff62902e166
Author: Michi Mutsuzaki <[email protected]>
Date:   2014-09-17T05:36:02Z

    ZOOKEEPER-2030 dynamicConfigFile should have an absolute path, not a 
relative
    path, to the dynamic configuration file (Alexander Shraer via michim)
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1625463 
13f79535-47bb-0310-9956-ffa450edef68

commit 88c974b03be971b227448857021b948b8e8920a0
Author: Rakesh Radhakrishnan <[email protected]>
Date:   2014-09-27T06:28:07Z

    ZOOKEEPER-2047 testTruncationNullLog fails on windows (flavio via rakeshr)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1627925 
13f79535-47bb-0310-9956-ffa450edef68

commit 266d5ee158bffdd09646a5f6996ddb34ce79a39a
Author: Flavio Paiva Junqueira <[email protected]>
Date:   2014-09-28T09:48:17Z

    ZOOKEEPER-2039. Jute compareBytes incorrect comparison index (Ian Dimayuga 
via fpj)
    
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1628059 
13f79535-47bb-0310-9956-ffa450edef68

commit c3e94d0ea824bdadd160ddaadbdb9d976ef9846c
Author: Rakesh Radhakrishnan <[email protected]>
Date:   2014-09-28T17:19:39Z

    ZOOKEEPER-2026 Startup order in ServerCnxnFactory-ies is wrong (Stevo 
Slavic via rakeshr)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1628088 
13f79535-47bb-0310-9956-ffa450edef68

commit d5cc02a120e3b604820ad9d5d85615871913007d
Author: Michi Mutsuzaki <[email protected]>
Date:   2014-09-29T06:26:34Z

    ZOOKEEPER-1917 Apache Zookeeper logs cleartext admin passwords (fpj via 
michim)
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1628125 
13f79535-47bb-0310-9956-ffa450edef68

commit 6aa47417410c90ca5a773566590d70067ec0a6fa
Author: Rakesh Radhakrishnan <[email protected]>
Date:   2014-09-29T17:17:02Z

    ZOOKEEPER-1948 Enable JMX remote monitoring (Biju Nair via rakeshr)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1628226 
13f79535-47bb-0310-9956-ffa450edef68

commit c6698479021597a1d5dbb5975ccfdb042df2babf
Author: Rakesh Radhakrishnan <[email protected]>
Date:   2014-10-13T03:12:50Z

    ZOOKEEPER-1917 Apache Zookeeper logs cleartext admin passwords (michim via 
rakeshr)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1631278 
13f79535-47bb-0310-9956-ffa450edef68

commit ced70e1ed9eb681f6156250338e54ce954f6c88d
Author: Michi Mutsuzaki <[email protected]>
Date:   2014-10-16T04:51:49Z

    ZOOKEEPER-2049 Yosemite build failure: htonll conflict (Till Toenshoff via 
michim)
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1632211 
13f79535-47bb-0310-9956-ffa450edef68

commit 8b9168a1d1f596492db833e01a5c4761dcdb9d49
Author: Rakesh Radhakrishnan <[email protected]>
Date:   2014-10-28T04:37:55Z

    ZOOKEEPER-2052 Unable to delete a node when the node has no children 
(Hongchao Deng and Yip Ng via rakeshr)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1634778 
13f79535-47bb-0310-9956-ffa450edef68

commit bea7fea71e89a79db7acdb97d3fc7446ecc23685
Author: Michi Mutsuzaki <[email protected]>
Date:   2014-11-17T06:48:15Z

    ZOOKEEPER-2079 Stop daemon with "kill" rather than "kill -9" (Guillaume 
ALAUX via michim)
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1640070 
13f79535-47bb-0310-9956-ffa450edef68

commit 5fea943f84a62ae5c878125d4f8e3f98916d7342
Author: Flavio Paiva Junqueira <[email protected]>
Date:   2014-11-19T22:38:28Z

    ZOOKEEPER-2060 Trace bug in NettyServerCnxnFactory (Ian via fpj)
    
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1640636 
13f79535-47bb-0310-9956-ffa450edef68

commit 44dbb64e6196114407649c2ca5a7a2a3973f678a
Author: Flavio Paiva Junqueira <[email protected]>
Date:   2014-11-29T15:54:58Z

    ZOOKEEPER-2064 Prevent resource leak in various classes (Ted Yu via fpj)
    
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/zookeeper/branches/branch-3.5@1642440 
13f79535-47bb-0310-9956-ffa450edef68

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to