You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "strongduanmu (via GitHub)" <gi...@apache.org> on 2023/02/16 05:52:24 UTC

[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #24183: Abstract the spi of shardingsphere driver url

strongduanmu commented on code in PR #24183:
URL: https://github.com/apache/shardingsphere/pull/24183#discussion_r1108040055


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURLManager.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.driver.jdbc.core.driver;
+
+import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+
+/**
+ * ShardingSphere driver URL manager.
+ */
+public final class ShardingSphereDriverURLManager {
+    
+    /**
+     * Get config content from url.
+     * 
+     * @param url the driver url
+     * @return the config content
+     */
+    public static byte[] getContent(final String url) {
+        for (ShardingsphereDriverURLProvider each : ShardingSphereServiceLoader.getServiceInstances(ShardingsphereDriverURLProvider.class)) {
+            if (each.accept(url)) {
+                return each.getContent(url);
+            }
+        }
+        throw new IllegalArgumentException("No suitable driver url provider for " + url);
+    }
+    

Review Comment:
   Please remove useless black line.



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURLManager.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.driver.jdbc.core.driver;
+
+import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+
+/**
+ * ShardingSphere driver URL manager.
+ */
+public final class ShardingSphereDriverURLManager {
+    
+    /**
+     * Get config content from url.
+     * 
+     * @param url the driver url
+     * @return the config content
+     */
+    public static byte[] getContent(final String url) {
+        for (ShardingsphereDriverURLProvider each : ShardingSphereServiceLoader.getServiceInstances(ShardingsphereDriverURLProvider.class)) {
+            if (each.accept(url)) {
+                return each.getContent(url);
+            }
+        }
+        throw new IllegalArgumentException("No suitable driver url provider for " + url);

Review Comment:
   Can we add a new Exception with error code? Just like ColumnLabelNotFoundException.



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathDriverURLProvider.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.driver.jdbc.core.driver.spi;
+
+import com.google.common.base.Preconditions;
+import lombok.SneakyThrows;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingsphereDriverURLProvider;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Objects;
+
+/**
+ * Absolute path driver URL provider.
+ */
+public final class AbsolutePathDriverURLProvider implements ShardingsphereDriverURLProvider {
+    
+    private static final String PATH_TYPE = "absolutepath:";
+    
+    @Override
+    public boolean accept(final String url) {
+        return StringUtils.isNotBlank(url) && url.contains(PATH_TYPE);
+    }
+    
+    @Override
+    @SneakyThrows(IOException.class)
+    public byte[] getContent(final String url) {
+        String configuredFile = url.substring("jdbc:shardingsphere:".length(), url.contains("?") ? url.indexOf("?") : url.length());
+        String file = configuredFile.substring(PATH_TYPE.length());
+        Preconditions.checkArgument(!file.isEmpty(), "Configuration file is required in ShardingSphere driver URL.");
+        try (InputStream stream = Files.newInputStream(new File(file).toPath())) {
+            Objects.requireNonNull(stream, String.format("Can not find configuration file `%s`.", file));
+            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
+            StringBuilder builder = new StringBuilder();
+            String line;
+            while ((line = reader.readLine()) != null) {

Review Comment:
   Please put null on the left condition.



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingsphereDriverURLProvider.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.driver.jdbc.core.driver;
+
+/**
+ * Shardingsphere driver URL provider.
+ */
+public interface ShardingsphereDriverURLProvider {

Review Comment:
   Can we add this new SPI interface to dev manual document?



-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org