summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/ZooKeeperHelper.java
blob: e92df3d91d30f6237e33f26c84216797656bafa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.tuscany.sca.endpoint.zookeeper;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.ZooKeeperServer.DataTreeBuilder;
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;

/**
 * 
 */
public class ZooKeeperHelper implements Watcher {
    public ZooKeeper connect(String host, int port, int timeout) throws IOException {
        return new ZooKeeper(host + ":" + port, timeout, this);
    }

    public String getName(String uri) {
        String name = uri.replace("$", "$$");
        return name.replace('/', '$');
    }

    private byte[] getBytes(String str) {
        try {
            return str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public String join(ZooKeeper zooKeeper, String domainURI, String nodeURI) throws KeeperException,
        InterruptedException {
        String domain = "/" + getName(domainURI);
        Stat stat = zooKeeper.exists(domain, false);
        String path = domain;
        if (stat == null) {
            path = zooKeeper.create(domain, getBytes(domainURI), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        path = path + "/" + getName(nodeURI);
        stat = zooKeeper.exists(path, false);
        if (stat != null) {
            return zooKeeper.create(path, getBytes(nodeURI), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        } else {
            return path;
        }
    }

    public String getPath(String... uris) {
        StringBuffer buffer = new StringBuffer();
        for (String uri : uris) {
            buffer.append('/').append(getName(uri));
        }
        return buffer.toString();
    }

    public void put(ZooKeeper zooKeeper, String domainURI, String nodeURI, String endpointURI, byte[] endpoint)
        throws KeeperException, InterruptedException {
        String path = join(zooKeeper, domainURI, nodeURI);
        path = path + "/" + getName(endpointURI);
        Stat stat = zooKeeper.exists(path, false);
        if (stat == null) {
            zooKeeper.create(path, endpoint, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        } else {
            zooKeeper.setData(path, endpoint, -1);
        }
    }

    public void remove(ZooKeeper zooKeeper, String domainURI, String nodeURI, String endpointURI)
        throws KeeperException, InterruptedException {
        String path = getPath(domainURI, nodeURI, endpointURI);
        zooKeeper.delete(path, -1);
    }

    public void remove(ZooKeeper zooKeeper, String domainURI, String nodeURI) throws InterruptedException,
        KeeperException {
        String node = getPath(domainURI, nodeURI);
        zooKeeper.delete(node, -1);
    }

    public byte[] get(ZooKeeper zooKeeper, String domainURI, String nodeURI, String endpointURI)
        throws KeeperException, InterruptedException {
        String path = getPath(domainURI, nodeURI, endpointURI);
        return zooKeeper.getData(path, false, new Stat());
    }

    public List<byte[]> get(ZooKeeper zooKeeper, String domainURI, String nodeURI) throws KeeperException,
        InterruptedException {
        String node = getPath(domainURI, nodeURI);
        List<String> endpoints = zooKeeper.getChildren(node, false);
        List<byte[]> data = new ArrayList<byte[]>();
        for (String endpoint : endpoints) {
            Stat stat = new Stat();
            data.add(zooKeeper.getData(endpoint, false, stat));
        }
        return data;
    }

    public List<byte[]> get(ZooKeeper zooKeeper, String domainURI) throws KeeperException, InterruptedException {
        String path = getPath(domainURI);
        List<String> nodes = zooKeeper.getChildren(path, false);
        List<byte[]> data = new ArrayList<byte[]>();
        for (String node : nodes) {
            List<String> endpoints = zooKeeper.getChildren(node, false);
            for (String endpoint : endpoints) {
                Stat stat = new Stat();
                data.add(zooKeeper.getData(endpoint, false, stat));
            }
        }
        return data;
    }

    public ZooKeeper create(String connectString, int timeout, long sessionId, byte[] password) throws IOException {
        return new ZooKeeper(connectString, timeout, this, sessionId, password);
    }

    public void process(WatchedEvent event) {
        System.out.println(event);
    }

    public ZooKeeperServer createServer(File dataDir, File snapDir, int tickTime) throws IOException {
        FileTxnSnapLog log = new FileTxnSnapLog(dataDir, snapDir);
        DataTreeBuilder builder = new ZooKeeperServer.BasicDataTreeBuilder();
        return new ZooKeeperServer(log, tickTime, builder);
    }

    public void put(ZooKeeper zooKeeper, String key, Object value) throws KeeperException, InterruptedException {
        byte[] data = serialize(value);
        List<ACL> acls = Collections.emptyList();
        zooKeeper.create(key, data, acls, CreateMode.PERSISTENT);
    }

    public byte[] serialize(Object value) {
        return null;
    }
    


    public static void main(final String[] args) throws Exception {
        final String options[] =
            args.length != 0 ? args
                : new String[] {"9999", System.getProperty("java.io.tmpdir") + File.separator + "zookeeper"};
        Thread thread = new Thread() {
            public void run() {
                ZooKeeperServerMain.main(options);
            }
        };
        thread.start();
        Thread.sleep(1000);
        ZooKeeper zooKeeper = new ZooKeeperHelper().connect("localhost", 9999, 500);
        System.out.println(zooKeeper.getSessionId());
        try {
            String data = new String(zooKeeper.getData("/x", false, null));
            System.out.println(data);
        } catch (KeeperException e) {
            if (e.code() == Code.NONODE) {
                zooKeeper.create("/x", "X".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
        }
        zooKeeper.create("/x/y", "XY".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        Thread.sleep(1000);
        String data = new String(zooKeeper.getData("/x/y", true, null));
        System.out.println(data);
        zooKeeper.close();
        Thread.sleep(500);
        System.exit(0);
    }
}