Selaa lähdekoodia

ZOOKEEPER-1619. Allow spaces in URL (Edward Ribeiro via phunt)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1438351 13f79535-47bb-0310-9956-ffa450edef68
Patrick D. Hunt 12 vuotta sitten
vanhempi
commit
3d81f925c5

+ 3 - 0
CHANGES.txt

@@ -471,6 +471,9 @@ IMPROVEMENTS:
 
   ZOOKEEPER-1535. ZK Shell/Cli re-executes last command on exit (Edward Ribeiro via camille)
 
+  ZOOKEEPER-1619. Allow spaces in URL (Edward Ribeiro via phunt)
+
+
 Release 3.4.0 - 
 
 Non-backward compatible changes:

+ 6 - 3
src/java/main/org/apache/zookeeper/client/ConnectStringParser.java

@@ -18,10 +18,13 @@
 
 package org.apache.zookeeper.client;
 
+import org.apache.zookeeper.common.PathUtils;
+
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
+import java.util.List;
 
-import org.apache.zookeeper.common.PathUtils;
+import static org.apache.zookeeper.common.StringUtils.split;
 
 /**
  * A parser for ZooKeeper Client connect strings.
@@ -62,7 +65,7 @@ public final class ConnectStringParser {
             this.chrootPath = null;
         }
 
-        String hostsList[] = connectString.split(",");
+        List<String> hostsList = split(connectString,",");
         for (String host : hostsList) {
             int port = DEFAULT_PORT;
             int pidx = host.lastIndexOf(':');
@@ -84,4 +87,4 @@ public final class ConnectStringParser {
     public ArrayList<InetSocketAddress> getServerAddresses() {
         return serverAddresses;
     }
-}
+}

+ 44 - 0
src/java/main/org/apache/zookeeper/common/StringUtils.java

@@ -0,0 +1,44 @@
+/* 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.zookeeper.common;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Collections;
+
+public class StringUtils {
+
+    private StringUtils() {/** non instantiable and non inheritable **/}
+
+    /**
+     * This method returns an immutable List<String>, but different from String's split()
+     * it trims the results in the input String, and removes any empty string from
+     * the resulting List.
+     *
+     */
+    public static List<String> split(String value, String separator) {
+        String[] splits = value.split(separator);
+        List<String> results = new ArrayList<String>();
+        for (int i = 0; i < splits.length; i++) {
+            splits[i] = splits[i].trim();
+            if (splits[i].length() > 0) {
+               results.add(splits[i]);
+            }
+        }
+        return Collections.unmodifiableList(results);
+    }
+}

+ 43 - 0
src/java/test/org/apache/zookeeper/test/StringUtilTest.java

@@ -0,0 +1,43 @@
+/**
+ * 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.zookeeper.test;
+
+
+import org.apache.zookeeper.common.StringUtils;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static junit.framework.Assert.assertEquals;
+
+public class StringUtilTest {
+
+    @Test
+    public void testStrings() {
+
+        String s1 = "   a  ,   b  , ";
+        assertEquals("[a, b]", StringUtils.split(s1, ",").toString());
+
+        String s2 = "";
+        assertEquals(0, StringUtils.split(s2, ",").size());
+
+        String s3 = "1, , 2";
+        assertEquals("[1, 2]", StringUtils.split(s3, ",").toString());
+
+    }
+}