anmolnar commented on code in PR #2321:
URL: https://github.com/apache/zookeeper/pull/2321#discussion_r2632570260


##########
zookeeper-server/src/main/java/org/apache/zookeeper/client/ConnectStringParser.java:
##########
@@ -49,27 +50,71 @@ public final class ConnectStringParser {
      * @throws IllegalArgumentException
      *             for an invalid chroot path.
      */
-    public ConnectStringParser(String connectString) {
-        // parse out chroot, if any
-        int off = connectString.indexOf('/');
-        if (off >= 0) {
-            String chrootPath = connectString.substring(off);
-            // ignore "/" chroot spec, same as null
-            if (chrootPath.length() == 1) {
-                this.chrootPath = null;
-            } else {
-                PathUtils.validatePath(chrootPath);
-                this.chrootPath = chrootPath;
-            }
-            connectString = connectString.substring(0, off);
+    public ConnectStringParser(final String connectString) {
+        if (StringUtils.isBlank(connectString)) {
+            throw new IllegalArgumentException("Connect string cannot be null 
or empty");
+        }
+
+        if (connectString.startsWith(DNS_SRV_PREFIX)) {
+            parseDnsSrvFormat(connectString);

Review Comment:
   I don't think this is the right abstraction. You basically just adding an 
optional prefix (type://) to the beginning of the connection string, everything 
else remains the same. Don't make all these changes and don't introduce new 
methods, just parse the optional prefix if present and store it in a property.



##########
zookeeper-server/src/main/java/org/apache/zookeeper/client/ConnectStringParser.java:
##########
@@ -49,27 +50,71 @@ public final class ConnectStringParser {
      * @throws IllegalArgumentException
      *             for an invalid chroot path.
      */
-    public ConnectStringParser(String connectString) {
-        // parse out chroot, if any
-        int off = connectString.indexOf('/');
-        if (off >= 0) {
-            String chrootPath = connectString.substring(off);
-            // ignore "/" chroot spec, same as null
-            if (chrootPath.length() == 1) {
-                this.chrootPath = null;
-            } else {
-                PathUtils.validatePath(chrootPath);
-                this.chrootPath = chrootPath;
-            }
-            connectString = connectString.substring(0, off);
+    public ConnectStringParser(final String connectString) {
+        if (StringUtils.isBlank(connectString)) {
+            throw new IllegalArgumentException("Connect string cannot be null 
or empty");
+        }
+
+        if (connectString.startsWith(DNS_SRV_PREFIX)) {
+            parseDnsSrvFormat(connectString);
         } else {
-            this.chrootPath = null;
+            parseHostPortFormat(connectString);
         }
+    }
 
-        List<String> hostsList = split(connectString, ",");
+    public String getChrootPath() {
+        return chrootPath;
+    }
+
+    public ArrayList<InetSocketAddress> getServerAddresses() {
+        return serverAddresses;
+    }
+
+
+    /**
+     * Gets the connection type for the given connect string.
+     *
+     * @param connectString the connection string to analyze
+     * @return ConnectionType.DNS_SRV if it's a DNS SRV connect string, 
ConnectionType.HOST_PORT otherwise
+     */
+    public static ConnectionType getConnectionType(final String connectString) 
{

Review Comment:
   You added a new static method which only calls String.startsWith() on the 
connection string in order to decide which HostProvider to instantiate, but you 
already have this information within the ConnectionStringParser class which 
doesn't make sense to me.
   
   Encapsulate everything in the parser:
   ```java
     String connString = read_it_from_somewhere();
     ConnectionStringParser parser = ConnectionStringParser.parse(connString);
     HostProvider provider = HostProviderFactory.create(parser);
     ...
   ```
   
   In the factory:
   ```java
     switch (parser.getConnType()) {
       case DNS_SRV:
         return new DnsSrvHostProvider(parser.getOrigConnString(), ...);
       default:
         return new StaticHostProvider(parser.getServerList(), ...);
       ...
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to