You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by tj...@apache.org on 2012/02/26 13:53:55 UTC

svn commit: r1293825 - in /incubator/hama/trunk/core/src: main/java/org/apache/hama/util/ZKUtil.java test/java/org/apache/hama/util/TestZKUtil.java

Author: tjungblut
Date: Sun Feb 26 12:53:54 2012
New Revision: 1293825

URL: http://svn.apache.org/viewvc?rev=1293825&view=rev
Log:
Add ZKUtil.ZK_SEPARATOR.

Modified:
    incubator/hama/trunk/core/src/main/java/org/apache/hama/util/ZKUtil.java
    incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestZKUtil.java

Modified: incubator/hama/trunk/core/src/main/java/org/apache/hama/util/ZKUtil.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/main/java/org/apache/hama/util/ZKUtil.java?rev=1293825&r1=1293824&r2=1293825&view=diff
==============================================================================
--- incubator/hama/trunk/core/src/main/java/org/apache/hama/util/ZKUtil.java (original)
+++ incubator/hama/trunk/core/src/main/java/org/apache/hama/util/ZKUtil.java Sun Feb 26 12:53:54 2012
@@ -17,54 +17,57 @@
  */
 package org.apache.hama.util;
 
-import java.io.File;
 import java.util.StringTokenizer;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.ZooDefs.Ids; 
+import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.ZooKeeper;
 
 public class ZKUtil {
 
-  public static final Log LOG = LogFactory.getLog(ZKUtil.class); 
+  public static final Log LOG = LogFactory.getLog(ZKUtil.class);
+
+  public static final String ZK_SEPARATOR = "/";
 
   /**
-   * Recursively create ZooKeeper's znode with corresponded path, which 
-   * starts from the root (/).
+   * Recursively create ZooKeeper's znode with corresponded path, which starts
+   * from the root (/).
+   * 
    * @param zk is the target where znode is to be created.
    * @param path of the znode on ZooKeeper server.
    */
   public static void create(ZooKeeper zk, String path) {
-    if(LOG.isDebugEnabled()) LOG.debug("Path to be splitted: "+path);
-    if(!path.startsWith("/"))
-      throw new IllegalArgumentException("Path is not started from root(/): "+
-      path);
-    StringTokenizer token = new StringTokenizer(path, File.separator); 
+    if (LOG.isDebugEnabled())
+      LOG.debug("Path to be splitted: " + path);
+    if (!path.startsWith(ZK_SEPARATOR))
+      throw new IllegalArgumentException("Path is not started from root(/): "
+          + path);
+    StringTokenizer token = new StringTokenizer(path, ZKUtil.ZK_SEPARATOR);
     int count = token.countTokens();
-    if(0 >= count) 
+    if (0 >= count)
       throw new RuntimeException("Can not correctly split the path into.");
     String[] parts = new String[count];
     int pos = 0;
-    while(token.hasMoreTokens()) {
+    while (token.hasMoreTokens()) {
       parts[pos] = token.nextToken();
-      if(LOG.isDebugEnabled()) LOG.debug("Splitted string:"+parts[pos]);
+      if (LOG.isDebugEnabled())
+        LOG.debug("Splitted string:" + parts[pos]);
       pos++;
     }
     StringBuilder builder = new StringBuilder();
-    for(String part: parts) {
+    for (String part : parts) {
       try {
-        builder.append(File.separator+part);
-        if(null == zk.exists(builder.toString(), false)) {
-          zk.create(builder.toString(), null, Ids.OPEN_ACL_UNSAFE, 
-          CreateMode.PERSISTENT);
-        } 
-      } catch(KeeperException ke) {
+        builder.append(ZKUtil.ZK_SEPARATOR + part);
+        if (null == zk.exists(builder.toString(), false)) {
+          zk.create(builder.toString(), null, Ids.OPEN_ACL_UNSAFE,
+              CreateMode.PERSISTENT);
+        }
+      } catch (KeeperException ke) {
         LOG.warn(ke);
-      } catch(InterruptedException ie) {
+      } catch (InterruptedException ie) {
         LOG.warn(ie);
       }
     }

Modified: incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestZKUtil.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestZKUtil.java?rev=1293825&r1=1293824&r2=1293825&view=diff
==============================================================================
--- incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestZKUtil.java (original)
+++ incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestZKUtil.java Sun Feb 26 12:53:54 2012
@@ -17,19 +17,17 @@
  */
 package org.apache.hama.util;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.List;
 import java.util.StringTokenizer;
 
 import junit.framework.TestCase;
-import static org.junit.Assert.*;
 
 import org.apache.zookeeper.CreateMode;
-import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.data.ACL;
 
 public class TestZKUtil extends TestCase {
 
@@ -51,10 +49,10 @@ public class TestZKUtil extends TestCase
         CreateMode createMode) throws KeeperException, InterruptedException {  
       parts[pos] = path; 
       pos++;
-      sb.append(File.separator+path);
+      sb.append(ZKUtil.ZK_SEPARATOR+path);
       StringBuilder builder = new StringBuilder();
       for(int i=0;i<pos;i++) {
-        builder.append(File.separator+parts[i]);
+        builder.append(ZKUtil.ZK_SEPARATOR+parts[i]);
       }
       assertEquals("Make sure path created is consistent.", sb.toString(), builder.toString());
       return path;
@@ -64,7 +62,7 @@ public class TestZKUtil extends TestCase
   public void setUp() throws Exception {
     this.zk = new MockZK("localhost:2181", 3000, null);
     this.path = "/monitor/groom_lab01_61000/metrics/jvm";
-    StringTokenizer token = new StringTokenizer(path, File.separator);
+    StringTokenizer token = new StringTokenizer(path, ZKUtil.ZK_SEPARATOR);
     int count = token.countTokens(); // should be 4
     assertEquals("Make sure token are 4.", count, 4);
     this.parts = new String[count]; //