/*
 * 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;
import java.util.List;

/**
 * Data source meta data for Oracle.
 *
 * @author panjuan
 */
@Getter
public final class OracleDataSourceMetaData implements DataSourceMetaData {
    
    private static final int DEFAULT_PORT = 1521;
    
    private final String hostName;
    
    private final int port;
    
    private final String schemeName;
    
    public OracleDataSourceMetaData(final String url) {
        URI uri = getURI(url);
        hostName = uri.getHost();
        port = -1 == uri.getPort() ? DEFAULT_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("oracle:thin:@//")) {
            cleanUrl = cleanUrl.replace("oracle:thin:@//", "oracle://");
        } else if (cleanUrl.contains("oracle:thin:@")) {
            cleanUrl = cleanUrl.replace("oracle:thin:@", "oracle://");
        }
        List<String> parts = Splitter.on(":").splitToList(cleanUrl);
        if (4 == parts.size()) {
            cleanUrl = parts.get(0) + ":" + parts.get(1) + ":" + parts.get(2) + "/" + parts.get(3);
        }
        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();
    }
}

@@ -705,23 +705,22 @@
 n.base.S
-plitter
+trings
 ;%0Aimport
@@ -881,15 +881,26 @@
 ava.
-net.URI
+util.regex.Matcher
 ;%0Aim
@@ -914,20 +914,29 @@
 va.util.
-List
+regex.Pattern
 ;%0A%0A/**%0A 
@@ -1265,491 +1265,230 @@
    p
-ublic OracleDataSourceMetaData(final String url) %7B%0A        URI uri = getURI(url);%0A        hostName = uri.getHost();%0A        port = -1 == uri.getPort() ? DEFAULT_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(%22oracle:thin:@//%22)) %7B%0A            cleanUrl = cleanUrl.replace(%22oracle:thin:@//%22, %22oracle://%22
+rivate final Pattern pattern = Pattern.compile(%22jdbc:oracle:thin:@/%7B0,2%7D(%5Ba-zA-Z0-9%5C%5C-%5C%5C.%5D+):?(%5B0-9%5D*)%5B:/%5D(%5C%5Cw+)%22);%0A    %0A    public OracleDataSourceMetaData(final String url) %7B%0A        Matcher matcher = pattern.matcher(url
 );%0A 
@@ -1497,126 +1497,69 @@
     
- 
-%7D else if (cleanUrl.contains(%22oracle:thin:@%22)) %7B%0A            cleanUrl = cleanUrl.replace(%22oracle:thin:@%22, %22oracle://%22
+ if (matcher.find()) %7B%0A            hostName = matcher.group(1
 );%0A 
@@ -1568,210 +1568,109 @@
     
- %7D%0A
      
-   List%3CString%3E pa
+po
 rt
-s
  = S
-plitter.on(%22:%22).splitToList(cleanUrl);%0A        if (4 == parts.size()) %7B%0A            cleanUrl = parts.get(0) + %22:%22 + parts.get(1) + %22:%22 + parts.get(2) + %22/%22 + parts.get(3
+trings.isNullOrEmpty(matcher.group(2)) ? DEFAULT_PORT : Integer.valueOf(matcher.group(2)
 );%0A 
@@ -1680,90 +1680,57 @@
     
-%7D%0A
     
-    URI result = URI.create(cleanUrl);%0A        if (null == result.getHost())
+schemeName = matcher.group(3);%0A        %7D else
  %7B%0A 
@@ -1820,31 +1820,8 @@
   %7D%0A
-        return result;%0A
     

modify OracleDataSourceMetaData