/*
 * Drizzle-JDBC
 *
 * Copyright (c) 2009-2011, Marcus Eriksson
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
 * conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials provided with the distribution.
 *  Neither the name of the driver nor the names of its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.skysql.jdbc;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Parses a jdbc url.
 * <p/>
 * User: marcuse Date: Apr 21, 2009 Time: 9:32:34 AM
 */
public class JDBCUrl {
    private final DBType dbType;
    private final String username;
    private final String password;
    private final String hostname;
    private final int port;
    private final String database;

    public enum DBType {
        DRIZZLE, MYSQL
    }

    private JDBCUrl(DBType dbType, String username, String password, String hostname, int port, String database) {
        this.dbType = dbType;
        this.username = username;
        this.password = password;
        this.hostname = hostname;
        this.port = port;
        this.database = database;
    }

    /*
    Parse ConnectorJ compatible urls
    jdbc:mysql://host:port/database

     */
    private static JDBCUrl parseConnectorJUrl(String url) {
        Pattern p = Pattern.compile("^jdbc:mysql://([A-Za-z0-9._]+)?(:\\d+)?(/\\w+)?");
        Matcher m = p.matcher(url);
        if (m.find()){
           String hostname = m.group(1);
           String port = m.group(2);
           if(port == null) {
               port = "3306";
           }
           else {
               port = port.substring(1);
           }
           String database="";
           if ( m.group(3)!= null) {
               database=m.group(3).substring(1);
           }
           return new JDBCUrl(DBType.MYSQL, "", "", hostname, Integer.valueOf(port), database);
        }
        return null;
    }

    public static JDBCUrl parse(final String url) {
        final DBType dbType;
        final String username;
        final String password;
        final String hostname;
        final int port;
        final String database;        

        if(url.startsWith("jdbc:mysql://")) {
            return parseConnectorJUrl(url);
        }
        final Pattern p = Pattern.compile("^jdbc:(drizzle|mysql:thin)://((\\w+)(:(\\w*))?@)?([^/:]+)(:(\\d+))?(/(\\w+))?");
        final Matcher m = p.matcher(url);
        if (m.find()) {
            if (m.group(1).equals("mysql:thin")) {
                dbType = DBType.MYSQL;
            } else {
                dbType = DBType.DRIZZLE;
            }

            username = (m.group(3) == null ? "" : m.group(3));
            password = (m.group(5) == null ? "" : m.group(5));
            hostname = (m.group(6) == null ? "" : m.group(6));
            if (m.group(8) != null) {
                port = Integer.parseInt(m.group(8));
            } else {
                if (dbType == DBType.DRIZZLE) {
                    port = 3306;
                } else {
                    port = 3306;
                }
            }
            database = m.group(10);
            return new JDBCUrl(dbType, username, password, hostname, port, database);
        } else {
            return null;
        }
    }
    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getHostname() {
        return hostname;
    }

    public int getPort() {
        return port;
    }

    public String getDatabase() {
        return database;
    }

    public DBType getDBType() {
        return this.dbType;
    }

}
@@ -1956,16 +1956,53 @@
 atabase;
+%0A    private final String hostlist%5B%5D;
 %0A%0A    pu
@@ -2161,16 +2161,35 @@
 database
+, String hostlist%5B%5D
 ) %7B%0A    
@@ -2368,32 +2368,59 @@
 ase = database;%0A
+%09this.hostlist = hostlist;%0A
     %7D%0A%0A    /*%0A  
@@ -2623,16 +2623,17 @@
 a-z0-9._
+,
 %5D+)?(:%5C%5C
@@ -2744,24 +2744,190 @@
 m.group(1);%0A
+%09   String hostlist%5B%5D = null;%0A%09   if (hostname.indexOf(',') != -1)%09// We have a failover list%0A%09   %7B%0A%09%09hostlist = hostname.split(%22,%22);%0A%09%09hostname = hostlist%5B0%5D;%0A%09   %7D%0A
            S
@@ -3311,32 +3311,42 @@
 (port), database
+, hostlist
 );%0A        %7D%0A   
@@ -4656,16 +4656,22 @@
 database
+, null
 );%0A     
@@ -5109,9 +5109,76 @@
 %0A    %7D%0A%0A
-%7D
+    public String%5B%5D getHostlist() %7B%0A%09return this.hostlist;%0A    %7D%0A%0A%7D%0A

Add parsing support for failover host lists. This does not enable any failover yet but is a stepping stone to supporting connection filover.