package com.colobu.zkrecipe.leaderelection;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


public class ExampleClient extends LeaderSelectorListenerAdapter implements Closeable {
	private final String name;
	private final LeaderSelector leaderSelector;
	private final AtomicInteger leaderCount = new AtomicInteger();

	public ExampleClient(CuratorFramework client, String path, String name) {
		this.name = name;
		leaderSelector = new LeaderSelector(client, path, this);
		leaderSelector.autoRequeue();
	}

	public void start() throws IOException {
		leaderSelector.start();
	}

	@Override
	public void close() throws IOException {
		leaderSelector.close();
	}
	
	@Override
	public void takeLeadership(CuratorFramework client) throws Exception {
		final int waitSeconds = (int) (5 * Math.random()) + 1;
		System.out.println(name + " is now the leader. Waiting " + waitSeconds + " seconds...");
		System.out.println(name + " has been leader " + leaderCount.getAndIncrement() + " time(s) before.");
		try {
			Thread.sleep(TimeUnit.SECONDS.toMillis(waitSeconds));
		} catch (InterruptedException e) {
			System.err.println(name + " was interrupted.");
			Thread.currentThread().interrupt();
		} finally {
			System.out.println(name + " relinquishing leadership.\n");
		}
	}
}
@@ -241,16 +241,74 @@
 lector;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import j
@@ -531,17 +531,102 @@
 eable %7B%0A
-%09
+%0A    private static Logger logger = LoggerFactory.getLogger(ExampleClient.class);%0A    
 private 
@@ -644,17 +644,20 @@
 g name;%0A
-%09
+    
 private 
@@ -693,17 +693,20 @@
 lector;%0A
-%09
+    
 private 
@@ -761,17 +761,20 @@
 ger();%0A%0A
-%09
+    
 public E
@@ -839,18 +839,24 @@
 name) %7B%0A
-%09%09
+        
 this.nam
@@ -865,18 +865,24 @@
 = name;%0A
-%09%09
+        
 leaderSe
@@ -930,18 +930,24 @@
  this);%0A
-%09%09
+        
 leaderSe
@@ -968,21 +968,27 @@
 ueue();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public v
@@ -1012,34 +1012,40 @@
 s IOException %7B%0A
-%09%09
+        
 leaderSelector.s
@@ -1056,13 +1056,19 @@
 ();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 @Ove
@@ -1065,33 +1065,36 @@
 %0A%0A    @Override%0A
-%09
+    
 public void clos
@@ -1118,18 +1118,24 @@
 ption %7B%0A
-%09%09
+        
 leaderSe
@@ -1154,14 +1154,19 @@
 ();%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 @Ove
@@ -1171,17 +1171,20 @@
 verride%0A
-%09
+    
 public v
@@ -1246,18 +1246,24 @@
 ption %7B%0A
-%09%09
+        
 final in
@@ -1313,213 +1313,230 @@
  1;%0A
-%09%09System.out.println(name + %22 is now the leader. Waiting %22 + waitSeconds + %22 seconds...%22);%0A%09%09System.out.println(name + %22 has been leader %22 + leaderCount.getAndIncrement() + %22 time(s) before.%22);%0A%09%09try %7B%0A%09%09%09
+        logger.info(%22%7B%7D is now the leader. Waiting %7B%7D seconds...%22, this.name, waitSeconds);%0A        logger.info(%22%7B%7D has been leader %7B%7D time(s) before.%22, this.name, leaderCount.getAndIncrement());%0A        try %7B%0A            
 Thre
@@ -1585,18 +1585,24 @@
 onds));%0A
-%09%09
+        
 %7D catch 
@@ -1632,29 +1632,32 @@
 ) %7B%0A
-%09%09%09System.err.println
+            logger.error
 (nam
@@ -1683,14 +1683,26 @@
 ed.%22
-);%0A%09%09%09
+, e);%0A            
 Thre
@@ -1733,18 +1733,24 @@
 rupt();%0A
-%09%09
+        
 %7D finall
@@ -1757,29 +1757,31 @@
 y %7B%0A
-%09%09%09System.out.println
+            logger.info
 (nam
@@ -1814,19 +1814,32 @@
 ship
+ now
 .%5Cn%22);%0A
-%09%09%7D%0A%09
+        %7D%0A    
 %7D%0A%7D

add log