/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.core.metadata.datasource.dialect;

import com.google.common.base.Splitter;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.metadata.datasource.DataSourceMetaData;
import lombok.Getter;

import java.net.URI;

/**
 * Data source meta data for H2.
 *
 * @author panjuan
 */
@Getter
public final class H2DataSourceMetaData implements DataSourceMetaData {
    
    private static final String DEFAULT_HOST = "localhost";
    
    private final String hostName;
    
    private final int port;
    
    private final String schemeName;
    
    public H2DataSourceMetaData(final String url) {
        URI uri = getURI(url);
        hostName = uri.getHost();
        port = uri.getPort();
        schemeName = uri.getPath().isEmpty() ? "" : uri.getPath().substring(1);
    }
    
    private URI getURI(final String url) {
        String cleanUrl = url.substring(5);
        if (cleanUrl.contains("h2:~")) {
            cleanUrl = Splitter.on(";").splitToList(cleanUrl).get(0);
            cleanUrl = cleanUrl.replace(":", "://").replace("~", DEFAULT_HOST);
        } else if (cleanUrl.contains("h2:mem")) {
            cleanUrl = Splitter.on(";").splitToList(cleanUrl).get(0);
            String[] parts = cleanUrl.split(":");
            if (3 == parts.length) {
                cleanUrl = parts[0] + "://" + parts[1] + "/" + parts[2];
            }
        } else {
            throw new ShardingException("The URL of JDBC is not supported.");
        }
        URI result = URI.create(cleanUrl);
        if (null == result.getHost()) {
            throw new ShardingException("The URL of JDBC is not supported.");
        }
        return result;
    }
    
    @Override
    public boolean isInSameDatabaseInstance(final DataSourceMetaData dataSourceMetaData) {
        return hostName.equals(dataSourceMetaData.getHostName()) && port == dataSourceMetaData.getPort() && schemeName.equals(dataSourceMetaData.getSchemeName());
    }
}

@@ -678,48 +678,8 @@
 t;%0A%0A
-import com.google.common.base.Splitter;%0A
 impo
@@ -842,15 +842,58 @@
 ava.
-net.URI
+util.regex.Matcher;%0Aimport java.util.regex.Pattern
 ;%0A%0A/
@@ -1064,22 +1064,19 @@
 c final 
-Str
 in
-g
+t
  DEFAULT
@@ -1080,26 +1080,17 @@
 ULT_
-HOS
+POR
 T = 
-%22localhost%22
+-1
 ;%0A  
@@ -1216,634 +1216,262 @@
    p
-ublic H2DataSourceMetaData(final String url) %7B%0A        URI uri = getURI(url);%0A        hostName = uri.getHost();%0A        port = uri.getPort();%0A        schemeName = uri.getPath().isEmpty() ? %22%22 : uri.getPath().substring(1);%0A    %7D%0A    %0A    private URI getURI(final String url) %7B%0A        String cleanUrl = url.substring(5);%0A        if (cleanUrl.contains(%22h2:~%22)) %7B%0A            cleanUrl = Splitter.on(%22;%22).splitToList(cleanUrl).get(0);%0A            cleanUrl = cleanUrl.replace(%22:%22, %22://%22).replace(%22~%22, DEFAULT_HOST);%0A        %7D else if (cleanUrl.contains(%22h2:mem%22)) %7B%0A            cleanUrl = Splitter.on(%22;%22).splitToList(cleanUrl).get
+rivate final Pattern pattern = Pattern.compile(%22jdbc:h2:(mem%7C~)%5B:/%5D(%5C%5Cw+);?%5C%5Cw*%22);%0A    %0A    public H2DataSourceMetaData(final String url) %7B%0A        Matcher matcher = pattern.matcher(url);%0A        if (matcher.find()) %7B%0A            hostName = matcher.group
 (0);
@@ -1487,355 +1487,86 @@
     
-String%5B%5D pa
+po
 rt
-s
  = 
-cleanUrl.split(%22:%22);%0A            if (3 == parts.length) %7B%0A                cleanUrl = parts%5B0%5D + %22://%22 + parts%5B1%5D + %22/%22 + parts%5B2%5D;%0A            %7D%0A        %7D else %7B%0A            throw new ShardingException(%22The URL of JDBC is not supported.%22);%0A        %7D%0A        URI result = URI.create(cleanUrl);%0A        if (null == result.getHost())
+DEFAULT_PORT;%0A            schemeName = matcher.group(1);%0A        %7D else
  %7B%0A 
@@ -1656,31 +1656,8 @@
   %7D%0A
-        return result;%0A
     

modify H2DataSourceMetaData