camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [02/10] camel git commit: CAMEL-9851: zookeeper create operation should create sub paths.
Date Fri, 15 Apr 2016 06:54:08 GMT
CAMEL-9851: zookeeper create operation should create sub paths.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f02727ea
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f02727ea
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f02727ea

Branch: refs/heads/master
Commit: f02727ea1623dc236602c0d1e4347e7bd4c29d55
Parents: 8463350
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Apr 15 08:51:31 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Apr 15 08:51:31 2016 +0200

----------------------------------------------------------------------
 .../zookeeper/operations/CreateOperation.java   |  6 +-
 .../zookeeper/operations/ZooKeeperHelper.java   | 58 ++++++++++++++++++++
 .../operations/CreateOperationTest.java         | 21 +++++++
 3 files changed, 83 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f02727ea/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/CreateOperation.java
----------------------------------------------------------------------
diff --git a/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/CreateOperation.java
b/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/CreateOperation.java
index 5fb6587..730f6f2 100644
--- a/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/CreateOperation.java
+++ b/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/CreateOperation.java
@@ -18,14 +18,14 @@ package org.apache.camel.component.zookeeper.operations;
 
 import java.util.List;
 
-import static java.lang.String.format;
-
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Stat;
 
+import static java.lang.String.format;
+
 /**
  * <code>CreateOperation</code> is a basic Zookeeper operation used to create
  * and set the data contained in a given node
@@ -49,6 +49,8 @@ public class CreateOperation extends ZooKeeperOperation<String> {
     @Override
     public OperationResult<String> getResult() {
         try {
+            // ensure parent nodes is created first as persistent (cannot be ephemeral without
children)
+            ZooKeeperHelper.mkdirs(connection, node, false, CreateMode.PERSISTENT);
             String created = connection.create(node, data, permissions, createMode);
             if (LOG.isDebugEnabled()) {
                 LOG.debug(format("Created node '%s' using mode '%s'", created, createMode));

http://git-wip-us.apache.org/repos/asf/camel/blob/f02727ea/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/ZooKeeperHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/ZooKeeperHelper.java
b/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/ZooKeeperHelper.java
new file mode 100644
index 0000000..0a022e1
--- /dev/null
+++ b/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/operations/ZooKeeperHelper.java
@@ -0,0 +1,58 @@
+/**
+ * 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.camel.component.zookeeper.operations;
+
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.common.PathUtils;
+
+public final class ZooKeeperHelper {
+
+    private ZooKeeperHelper() {
+    }
+
+    public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode, CreateMode
createMode) throws Exception {
+        PathUtils.validatePath(path);
+
+        int pos = 1; // skip first slash, root is guaranteed to exist
+        do {
+            pos = path.indexOf("/", pos + 1);
+
+            if (pos == -1) {
+                if (makeLastNode) {
+                    pos = path.length();
+                } else {
+                    break;
+                }
+            }
+
+            String subPath = path.substring(0, pos);
+            if (zookeeper.exists(subPath, false) == null) {
+                try {
+                    zookeeper.create(subPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
+                } catch (KeeperException.NodeExistsException e) {
+                    // ignore... someone else has created it since we checked
+                }
+            }
+
+        }
+        while (pos < path.length());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/f02727ea/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/operations/CreateOperationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/operations/CreateOperationTest.java
b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/operations/CreateOperationTest.java
index e0137e1..48624c8 100644
--- a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/operations/CreateOperationTest.java
+++ b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/operations/CreateOperationTest.java
@@ -48,6 +48,27 @@ public class CreateOperationTest extends ZooKeeperTestSupport {
     }
 
     @Test
+    public void createBasicSubPath() throws Exception {
+        CreateOperation create = new CreateOperation(connection, "/sub/sub2");
+
+        OperationResult<String> result = create.get();
+        assertEquals("/sub/sub2", result.getResult());
+
+        verifyNodeContainsData("/sub/sub2", null);
+    }
+
+    @Test
+    public void createBasicSubPathWithData() throws Exception {
+        CreateOperation create = new CreateOperation(connection, "/sub/sub3");
+        create.setData(testPayload.getBytes());
+
+        OperationResult<String> result = create.get();
+        assertEquals("/sub/sub3", result.getResult());
+
+        verifyNodeContainsData("/sub/sub3", testPayloadBytes);
+    }
+
+    @Test
     public void createBasicWithData() throws Exception {
         CreateOperation create = new CreateOperation(connection, "/two");
         create.setData(testPayload.getBytes());


Mime
View raw message