This is an automated email from the ASF dual-hosted git repository.

zhangyonglun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new c62767a  add p6spy driver support for proxy backend (#6352)
c62767a is described below

commit c62767a0e7d2faf404bda67a363fe231f4ac85ae
Author: xbkaishui <[email protected]>
AuthorDate: Mon Jul 20 12:45:09 2020 +0800

    add p6spy driver support for proxy backend (#6352)
    
    * add p6spy driver support for proxy backend
    
    * fix ci error
    
    * fix cr suggestions
    
    * include distribution file
    
    * remove apache license
    
    * remove spy properties file
    
    * remove spy properties file
    
    * revert change datasource type
---
 .../recognizer/JDBCDriverURLRecognizerEngine.java  |  7 +-
 .../recognizer/impl/P6SpyDriverRecognizer.java     | 81 ++++++++++++++++++++++
 .../spi/JDBCDriverComposeURLRecognizer.java        | 32 +++++++++
 ...ion.jdbc.recognizer.spi.JDBCDriverURLRecognizer |  1 +
 .../JDBCDriverURLRecognizerEngineTest.java         | 10 +++
 .../recognizer/impl/P6SpyDriverRecognizerTest.java | 41 +++++++++++
 6 files changed, 171 insertions(+), 1 deletion(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngine.java
index bcb98a7..5d67465 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngine.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngine.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverComposeURLRecognizer;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer;
 
 import java.util.Collection;
@@ -47,8 +48,12 @@ public final class JDBCDriverURLRecognizerEngine {
      * @return JDBC driver URL recognizer
      */
     public static JDBCDriverURLRecognizer getJDBCDriverURLRecognizer(final 
String url) {
-        return JDBC_DRIVER_URL_RECOGNIZERS.stream().filter(each -> 
isMatchURL(url, each)).findAny()
+        JDBCDriverURLRecognizer driverURLRecognizer = 
JDBC_DRIVER_URL_RECOGNIZERS.stream().filter(each -> isMatchURL(url, 
each)).findAny()
                 .orElseThrow(() -> new ShardingSphereException("Cannot resolve 
JDBC url `%s`. Please implements `%s` and add to SPI.", url, 
JDBCDriverURLRecognizer.class.getName()));
+        if (driverURLRecognizer instanceof JDBCDriverComposeURLRecognizer) {
+            return ((JDBCDriverComposeURLRecognizer) 
driverURLRecognizer).getDriverURLRecognizer(url);
+        }
+        return driverURLRecognizer;
     }
     
     private static boolean isMatchURL(final String url, final 
JDBCDriverURLRecognizer jdbcDriverURLRecognizer) {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/impl/P6SpyDriverRecognizer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/impl/P6SpyDriverRecognizer.java
new file mode 100644
index 0000000..4ccb3ef
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/impl/P6SpyDriverRecognizer.java
@@ -0,0 +1,81 @@
+/*
+ * 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.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl;
+
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverComposeURLRecognizer;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * P6spy Driver JDBC URL recognizer for, this is a compose recognizer.
+ */
+public final class P6SpyDriverRecognizer implements 
JDBCDriverComposeURLRecognizer {
+    
+    protected static final String DRIVER_CLASS_NAME = 
"com.p6spy.engine.spy.P6SpyDriver";
+    
+    @Override
+    public JDBCDriverURLRecognizer getDriverURLRecognizer(final String url) {
+        String realUrl = extractRealUrl(url);
+        JDBCDriverURLRecognizer driverURLRecognizer = 
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(realUrl);
+        return new JDBCDriverURLRecognizer() {
+            @Override
+            public Collection<String> getURLPrefixes() {
+                return driverURLRecognizer.getURLPrefixes();
+            }
+            
+            @Override
+            public String getDriverClassName() {
+                return DRIVER_CLASS_NAME;
+            }
+            
+            @Override
+            public String getDatabaseType() {
+                return driverURLRecognizer.getDatabaseType();
+            }
+        };
+    }
+    
+    /**
+     * Parses out the real JDBC connection URL by removing "p6spy:".
+     *
+     * @param url the connection URL
+     * @return the parsed URL
+     */
+    private String extractRealUrl(final String url) {
+        return url.replace("p6spy:", "");
+    }
+    
+    @Override
+    public String getDatabaseType() {
+        throw new ShardingSphereException("Unsupported getDatabaseType 
method!");
+    }
+    
+    @Override
+    public Collection<String> getURLPrefixes() {
+        return Collections.singletonList("jdbc:p6spy:");
+    }
+    
+    @Override
+    public String getDriverClassName() {
+        return DRIVER_CLASS_NAME;
+    }
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/spi/JDBCDriverComposeURLRecognizer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/spi/JDBCDriverComposeURLRecognizer.java
new file mode 100644
index 0000000..3c7d0fd
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/spi/JDBCDriverComposeURLRecognizer.java
@@ -0,0 +1,32 @@
+/*
+ * 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.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi;
+
+/**
+ * JDBC compose driver URL recognizer.
+ */
+public interface JDBCDriverComposeURLRecognizer extends 
JDBCDriverURLRecognizer {
+    
+    /**
+     * get url recognizer base on url.
+     *
+     * @param url jdbc url
+     * @return jdbc url recognizer
+     */
+    JDBCDriverURLRecognizer getDriverURLRecognizer(String url);
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer
index d056dbd..ef113f7 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer
@@ -20,3 +20,4 @@ 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.Postg
 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.OracleRecognizer
 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.SQLServerRecognizer
 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.H2Recognizer
+org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.P6SpyDriverRecognizer
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngineTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngineTest.java
index ed071cb..98b9037 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngineTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/JDBCDriverURLRecognizerEngineTest.java
@@ -17,15 +17,18 @@
 
 package org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer;
 
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.H2Recognizer;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.MySQLRecognizer;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.OracleRecognizer;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.PostgreSQLRecognizer;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl.SQLServerRecognizer;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.spi.JDBCDriverURLRecognizer;
 import org.junit.Test;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 
 public final class JDBCDriverURLRecognizerEngineTest {
@@ -55,6 +58,13 @@ public final class JDBCDriverURLRecognizerEngineTest {
         
assertThat(JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer("jdbc:h2:xxx"),
 instanceOf(H2Recognizer.class));
     }
     
+    @Test
+    public void assertGetJDBCDriverURLRecognizerForP6spy() {
+        JDBCDriverURLRecognizer driverURLRecognizer = 
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer("jdbc:p6spy:mysql:xxx");
+        assertEquals(driverURLRecognizer.getDatabaseType(), new 
MySQLDatabaseType().getName());
+        assertEquals(driverURLRecognizer.getDriverClassName(), 
"com.p6spy.engine.spy.P6SpyDriver");
+    }
+    
     @Test(expected = ShardingSphereException.class)
     public void assertGetJDBCDriverURLRecognizerFailure() {
         JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer("xxx");
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/impl/P6SpyDriverRecognizerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/impl/P6SpyDriverRecognizerTest.java
new file mode 100644
index 0000000..5815b77
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/recognizer/impl/P6SpyDriverRecognizerTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.shardingsphere.proxy.backend.communication.jdbc.recognizer.impl;
+
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class P6SpyDriverRecognizerTest {
+    
+    private final P6SpyDriverRecognizer recognizer = new 
P6SpyDriverRecognizer();
+    
+    @Test
+    public void getURLPrefixes() {
+        assertThat(recognizer.getURLPrefixes(), 
is(Collections.singletonList("jdbc:p6spy:")));
+    }
+    
+    @Test
+    public void getDriverClassName() {
+        assertThat(recognizer.getDriverClassName(), 
is(P6SpyDriverRecognizer.DRIVER_CLASS_NAME));
+    }
+    
+}

Reply via email to