Skip to content

Commit

Permalink
First pass at a ZKClient bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Randgalt committed Sep 3, 2012
1 parent 3f264e7 commit 2b097fa
Show file tree
Hide file tree
Showing 17 changed files with 1,803 additions and 1 deletion.
13 changes: 13 additions & 0 deletions build.gradle
Expand Up @@ -94,6 +94,19 @@ project(':curator-recipes')
}
}

project(':curator-x-zkclient-bridge')
{
dependencies
{
compile project(':curator-client')
compile project(':curator-framework')
compile 'com.github.sgroschupf:zkclient:0.1'
testCompile 'commons-io:commons-io:1.4'
testCompile 'org.mockito:mockito-core:1.8.0'
testCompile 'junit:junit:4.7'
}
}

project(':curator-x-discovery')
{
dependencies
Expand Down
@@ -0,0 +1,206 @@
/*
* Copyright 2012 Netflix, Inc.
*
* 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.netflix.curator.x.zkclientbridge;

import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.api.CuratorEvent;
import com.netflix.curator.framework.api.CuratorListener;
import org.I0Itec.zkclient.IZkConnection;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.List;

public class CuratorZKClientBridge implements IZkConnection
{
private final CuratorFramework curator;

public CuratorZKClientBridge(CuratorFramework curator)
{
this.curator = curator;
}

@Override
public void connect(final Watcher watcher)
{
if ( watcher != null )
{
curator.getCuratorListenable().addListener
(
new CuratorListener()
{
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception
{
if ( event.getWatchedEvent() != null )
{
watcher.process(event.getWatchedEvent());
}
}
}
);

curator.sync("/", null); // force an event so that ZKClient can see the connect
}
}

@Override
public void close() throws InterruptedException
{
curator.close();
}

@Override
public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException
{
try
{
return curator.create().withMode(mode).forPath(path, data);
}
catch ( Exception e )
{
adjustException(e);
}
return null; // will never execute
}

@Override
public void delete(String path) throws InterruptedException, KeeperException
{
try
{
curator.delete().forPath(path);
}
catch ( Exception e )
{
adjustException(e);
}
}

@Override
public boolean exists(String path, boolean watch) throws KeeperException, InterruptedException
{
try
{
return watch ? (curator.checkExists().watched().forPath(path) != null) : (curator.checkExists().forPath(path) != null);
}
catch ( Exception e )
{
adjustException(e);
}
return false; // will never execute
}

@Override
public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedException
{
try
{
return watch ? curator.getChildren().forPath(path) : curator.getChildren().watched().forPath(path);
}
catch ( Exception e )
{
adjustException(e);
}
return null; // will never execute
}

@Override
public byte[] readData(String path, Stat stat, boolean watch) throws KeeperException, InterruptedException
{
try
{
if ( stat != null )
{
return watch ? curator.getData().storingStatIn(stat).watched().forPath(path) : curator.getData().storingStatIn(stat).forPath(path);
}
else
{
return watch ? curator.getData().watched().forPath(path) : curator.getData().forPath(path);
}
}
catch ( Exception e )
{
adjustException(e);
}
return null; // will never execute
}

@Override
public void writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException
{
try
{
curator.setData().withVersion(expectedVersion).forPath(path, data);
}
catch ( Exception e )
{
adjustException(e);
}
}

@Override
public ZooKeeper.States getZookeeperState()
{
try
{
return curator.getZookeeperClient().getZooKeeper().getState();
}
catch ( Exception e )
{
throw new RuntimeException(e);
}
}

@Override
public long getCreateTime(String path) throws KeeperException, InterruptedException
{
try
{
Stat stat = curator.checkExists().forPath(path);
return (stat != null) ? stat.getCtime() : 0;
}
catch ( Exception e )
{
adjustException(e);
}
return 0;
}

@Override
public String getServers()
{
throw new UnsupportedOperationException();
}

private void adjustException(Exception e) throws KeeperException, InterruptedException
{
if ( e instanceof KeeperException )
{
throw (KeeperException)e;
}

if ( e instanceof InterruptedException )
{
throw (InterruptedException)e;
}

throw new RuntimeException(e);
}
}

0 comments on commit 2b097fa

Please sign in to comment.