You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/12/05 12:49:54 UTC

[GitHub] [shardingsphere] zhfeng opened a new pull request, #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

zhfeng opened a new pull request, #22677:
URL: https://github.com/apache/shardingsphere/pull/22677

   fromclasspath with mutiple ClassLoaders
   
   Fixes #22658
   Changes proposed in this pull request:
     - Add `getResourceAsStream` to load configuration file from classpath by using mutiple ClassLoaders which could be helpful to fix https://github.com/quarkiverse/quarkus-shardingsphere-jdbc/issues/74
   
   ---
   
   Before committing this PR, I'm sure that I have checked the following options:
   - [x] My code follows the [code of conduct](https://shardingsphere.apache.org/community/en/involved/conduct/code/) of this project.
   - [x] I have self-reviewed the commit code.
   - [ ] I have (or in comment I request) added corresponding labels for the pull request.
   - [ ] I have passed maven check locally : `./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e`.
   - [ ] I have made corresponding changes to the documentation.
   - [ ] I have added corresponding unit tests for my changes.
   


-- 
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


[GitHub] [shardingsphere] sandynz commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
sandynz commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1043204063


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };

Review Comment:
   OK



-- 
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


[GitHub] [shardingsphere] zhfeng commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
zhfeng commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1039606092


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {
+            if (null != cl) {
+                
+                // try to find the resource as passed
+                InputStream returnValue = cl.getResourceAsStream(resource);
+                
+                // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
+                if (null == returnValue) {
+                    returnValue = cl.getResourceAsStream("/" + resource);
+                }

Review Comment:
   `getClass().getClassLoader()` needs `/`



-- 
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


[GitHub] [shardingsphere] zhfeng commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
zhfeng commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1040439971


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {
+            if (null != cl) {
+                
+                // try to find the resource as passed
+                InputStream returnValue = cl.getResourceAsStream(resource);
+                
+                // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
+                if (null == returnValue) {
+                    returnValue = cl.getResourceAsStream("/" + resource);
+                }

Review Comment:
   sorry, I think it is not correctly. It looks like when using `getClass().getClassLoader()`, it always uses the relative path. But if there is a custom classloader, it could need the leading of `/`.  Perhaps in case of some application containers, such as `Tomcat`



-- 
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


[GitHub] [shardingsphere] zhfeng commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
zhfeng commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1040412398


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,22 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoaders = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(), getClass().getClassLoader(), ClassLoader.getSystemClassLoader(),
+        };
+        for (ClassLoader each : classLoaders) {
+            if (null != each) {
+                InputStream result = each.getResourceAsStream(resource);
+                if (null == result) {
+                    result = each.getResourceAsStream("/" + resource);
+                }

Review Comment:
   I added  `,` in the arrays due to the checkstyle failures
   ```
   [ERROR] src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:[79] (coding) ArrayTrailingComma: 数组尾元素后应有逗号','。
   ```
   So it need to change the `checkstyle.xml` to remove `<module name="ArrayTrailingComma" />` ?
   
   yeah, I run `mvn splotless:apply` and it looks good.



-- 
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


[GitHub] [shardingsphere] zhfeng commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
zhfeng commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1039604307


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };

Review Comment:
   It looks like that `TCCL` could be the common cases here.



-- 
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


[GitHub] [shardingsphere] zhfeng commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
zhfeng commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1040441101


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,22 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoaders = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(), getClass().getClassLoader(), ClassLoader.getSystemClassLoader(),
+        };
+        for (ClassLoader each : classLoaders) {
+            if (null != each) {
+                InputStream result = each.getResourceAsStream(resource);
+                if (null == result) {
+                    result = each.getResourceAsStream("/" + resource);
+                }

Review Comment:
   And I think we can not bind `/` with class loader.  See my above explaination.



-- 
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


[GitHub] [shardingsphere] sandynz commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
sandynz commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1039577568


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {

Review Comment:
   Variable name `cl` should be `each`
   



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {
+            if (null != cl) {
+                
+                // try to find the resource as passed
+                InputStream returnValue = cl.getResourceAsStream(resource);
+                
+                // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
+                if (null == returnValue) {
+                    returnValue = cl.getResourceAsStream("/" + resource);
+                }

Review Comment:
   Did you test which class loader need '/'?
   If the most frequent used one need '/', could we try to load with '/' firstly?



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {
+            if (null != cl) {
+                
+                // try to find the resource as passed
+                InputStream returnValue = cl.getResourceAsStream(resource);
+                
+                // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
+                if (null == returnValue) {
+                    returnValue = cl.getResourceAsStream("/" + resource);
+                }
+                

Review Comment:
   Empty lines in method could be removed



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };

Review Comment:
   `classLoader` could be `classLoaders`



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };

Review Comment:
   Did you test which class loader satisify most cases or the most common case?
   
   Could we put `getClass().getClassLoader()` at first?



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {
+            if (null != cl) {
+                
+                // try to find the resource as passed
+                InputStream returnValue = cl.getResourceAsStream(resource);

Review Comment:
   `returnValue` should be `result`



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };
+        
+        for (ClassLoader cl : classLoader) {
+            if (null != cl) {
+                
+                // try to find the resource as passed

Review Comment:
   Comments in method could be removed



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,30 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoader = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(),
+                getClass().getClassLoader(),
+                ClassLoader.getSystemClassLoader(),
+        };

Review Comment:
   It's better to put them in one line for shorter code



-- 
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


[GitHub] [shardingsphere] sandynz commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
sandynz commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1040377447


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,22 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoaders = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(), getClass().getClassLoader(), ClassLoader.getSystemClassLoader(),
+        };
+        for (ClassLoader each : classLoaders) {
+            if (null != each) {
+                InputStream result = each.getResourceAsStream(resource);
+                if (null == result) {
+                    result = each.getResourceAsStream("/" + resource);
+                }

Review Comment:
   1, Could we remove the last `,` after `ClassLoader.getSystemClassLoader()`?
   
   2, And did you try to run `mvn spotless:apply` in this module? Seems it might change the code formatting.
   



##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,22 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoaders = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(), getClass().getClassLoader(), ClassLoader.getSystemClassLoader(),
+        };
+        for (ClassLoader each : classLoaders) {
+            if (null != each) {
+                InputStream result = each.getResourceAsStream(resource);
+                if (null == result) {
+                    result = each.getResourceAsStream("/" + resource);
+                }

Review Comment:
   Could we bind resource path with class loader? Maybe it's more efficient and readable.
   e.g. `TCCL` just use `resource`, `getClass().getClassLoader()` just use `"/" + resource`



-- 
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


[GitHub] [shardingsphere] zhfeng commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
zhfeng commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1040443812


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,22 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoaders = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(), getClass().getClassLoader(), ClassLoader.getSystemClassLoader(),
+        };
+        for (ClassLoader each : classLoaders) {
+            if (null != each) {
+                InputStream result = each.getResourceAsStream(resource);
+                if (null == result) {
+                    result = each.getResourceAsStream("/" + resource);
+                }

Review Comment:
   IIRC all `ClassLoader` uses relative path to load the resources. But in some case of custom classloader, it might need the leading of `/`. So it makes sense to try `resource` at first and if it fails, try `"/" + resource`.



-- 
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


[GitHub] [shardingsphere] sandynz commented on a diff in pull request #22677: Fix #22658 to make sure ShardingSphereDriver can load configuration file

Posted by GitBox <gi...@apache.org>.
sandynz commented on code in PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677#discussion_r1043205332


##########
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURL.java:
##########
@@ -72,4 +72,22 @@ public byte[] toConfigurationBytes() {
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
+    
+    private InputStream getResourceAsStream(final String resource) throws IOException {
+        ClassLoader[] classLoaders = new ClassLoader[]{
+                Thread.currentThread().getContextClassLoader(), getClass().getClassLoader(), ClassLoader.getSystemClassLoader(),
+        };
+        for (ClassLoader each : classLoaders) {
+            if (null != each) {
+                InputStream result = each.getResourceAsStream(resource);
+                if (null == result) {
+                    result = each.getResourceAsStream("/" + resource);
+                }

Review Comment:
   OK



-- 
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


[GitHub] [shardingsphere] sandynz merged pull request #22677: Make sure ShardingSphereDriver can load configuration file from classpath with mutiple ClassLoaders

Posted by GitBox <gi...@apache.org>.
sandynz merged PR #22677:
URL: https://github.com/apache/shardingsphere/pull/22677


-- 
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