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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 2c054c5  [SCB-1636] improve the implementation of ResourceUtil
2c054c5 is described below

commit 2c054c505fd3d1d43bb244c4a47759b7992621bc
Author: yhs0092 <[email protected]>
AuthorDate: Tue Jan 14 09:31:26 2020 +0800

    [SCB-1636] improve the implementation of ResourceUtil
---
 .../foundation/common/utils/ResourceUtil.java      | 54 ++++++++++++++--------
 ...esourceUtilsTest.java => ResourceUtilTest.java} | 15 ++++--
 .../serviceregistry/swagger/SwaggerLoader.java     |  2 +-
 .../serviceregistry/swagger/TestSwaggerLoader.java |  4 +-
 4 files changed, 49 insertions(+), 26 deletions(-)

diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ResourceUtil.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ResourceUtil.java
index 199decf..4aa7e33 100644
--- 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ResourceUtil.java
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ResourceUtil.java
@@ -31,52 +31,66 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
-import java.util.function.Function;
+import java.util.function.Predicate;
 import java.util.stream.Stream;
 
-public class ResourceUtil {
+public final class ResourceUtil {
+
+  private ResourceUtil() {
+  }
+
   /**
-   * Search the specified directories in classpath, and returns a list of URIs 
pointing to the matched resources.
+   * Search the specified location in classpath, and returns the resources 
with the specified suffix.
+   */
+  public static List<URI> findResourcesBySuffix(String resourceLocation, 
String fileNameSuffix)
+      throws IOException, URISyntaxException {
+    return findResources(resourceLocation, path -> 
path.toString().endsWith(fileNameSuffix));
+  }
+
+  /**
+   * Search the specified location in classpath, all the resources found are 
collected and returned.
+   */
+  public static List<URI> findResources(String resourceLocation) throws 
IOException, URISyntaxException {
+    return findResources(resourceLocation, p -> true);
+  }
+
+  /**
+   * Search the specified location in classpath, which can be a directory or 
the exact file location,
+   * and returns a list of URIs pointing to the matched resources.
    *
-   * @param directory in which directory the resources are collected
+   * @param resourceLocation in which location the resources are searched
    * @param filter to pick out those matched resources
    */
-  public static List<URI> loadResources(String directory, Function<Path, 
Boolean> filter)
+  public static List<URI> findResources(String resourceLocation, 
Predicate<Path> filter)
       throws IOException, URISyntaxException {
     ArrayList<URI> result = new ArrayList<>();
 
-    Enumeration<URL> dirURLs = 
JvmUtils.findClassLoader().getResources(directory);
+    Enumeration<URL> dirURLs = 
JvmUtils.findClassLoader().getResources(resourceLocation);
     while (dirURLs.hasMoreElements()) {
       URL dirURL = dirURLs.nextElement();
+
       if (dirURL.getProtocol().equals("file")) {
         Path dirPath = Paths.get(dirURL.toURI());
-        collectYamlFromPath(dirPath, filter, result);
+        collectResourcesFromPath(dirPath, filter, result);
         continue;
       }
 
       try (FileSystem fileSystem = FileSystems.newFileSystem(dirURL.toURI(), 
Collections.emptyMap())) {
-        Path dirPath = fileSystem.getPath(directory);
-        collectYamlFromPath(dirPath, filter, result);
+        Path dirPath = fileSystem.getPath(resourceLocation);
+        collectResourcesFromPath(dirPath, filter, result);
       }
     }
 
     return result;
   }
 
-  /**
-   * A convenient method to get a filter to match the resource files by file 
path suffix.
-   */
-  public static Function<Path, Boolean> matchSuffix(String suffix) {
-    return path -> path.toString().endsWith(suffix);
-  }
-
-  private static void collectYamlFromPath(Path dirPath, Function<Path, 
Boolean> filter, Collection<URI> container)
+  private static void collectResourcesFromPath(Path path, Predicate<Path> 
filter, Collection<URI> resources)
       throws IOException {
-    try (Stream<Path> dirContentTraversalStream = Files.walk(dirPath)) {
+    try (Stream<Path> dirContentTraversalStream = Files.walk(path)) {
       dirContentTraversalStream
-          .filter(filter::apply)
+          .filter(filter)
           .map(Path::toUri)
-          .forEach(container::add);
+          .forEach(resources::add);
     }
   }
 }
diff --git 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/ResourceUtilsTest.java
 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/ResourceUtilTest.java
similarity index 74%
rename from 
foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/ResourceUtilsTest.java
rename to 
foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/ResourceUtilTest.java
index 3bf0c66..f2a579b 100644
--- 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/ResourceUtilsTest.java
+++ 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/ResourceUtilTest.java
@@ -25,7 +25,7 @@ import java.util.List;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class ResourceUtilsTest {
+public class ResourceUtilTest {
 
   /**
    * This case is coupled with the Spring dependency, but in order to check 
the ability to read resources inside the
@@ -33,7 +33,7 @@ public class ResourceUtilsTest {
    */
   @Test
   public void loadResources_in_jar() throws IOException, URISyntaxException {
-    List<URI> uris = ResourceUtil.loadResources("META-INF", p -> 
p.toString().endsWith("spring.factories"));
+    List<URI> uris = ResourceUtil.findResources("META-INF", p -> 
p.toString().endsWith("spring.factories"));
     Assert.assertEquals(1, uris.size());
     Assert.assertTrue(uris.get(0).toString().startsWith("jar:file:"));
     
Assert.assertTrue(uris.get(0).toString().endsWith("!/META-INF/spring.factories"));
@@ -41,7 +41,16 @@ public class ResourceUtilsTest {
 
   @Test
   public void loadResources_in_disk() throws IOException, URISyntaxException {
-    List<URI> uris = ResourceUtil.loadResources("META-INF/spring", 
ResourceUtil.matchSuffix(".xml"));
+    List<URI> uris = ResourceUtil.findResourcesBySuffix("META-INF/spring", 
".xml");
+    Assert.assertEquals(1, uris.size());
+    URI uri = uris.get(0);
+    Assert.assertTrue("unexpected uri: " + uri, 
uri.toString().startsWith("file:"));
+    Assert.assertTrue("unexpected uri: " + uri, 
uri.toString().endsWith("META-INF/spring/config.bean.xml"));
+  }
+
+  @Test
+  public void loadResources_exact_file_in_disk() throws IOException, 
URISyntaxException {
+    List<URI> uris = 
ResourceUtil.findResources("META-INF/spring/config.bean.xml");
     Assert.assertEquals(1, uris.size());
     URI uri = uris.get(0);
     Assert.assertTrue("unexpected uri: " + uri, 
uri.toString().startsWith("file:"));
diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/swagger/SwaggerLoader.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/swagger/SwaggerLoader.java
index 2d6b8ba..d04c23d 100644
--- 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/swagger/SwaggerLoader.java
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/swagger/SwaggerLoader.java
@@ -74,7 +74,7 @@ public class SwaggerLoader {
   public void registerSwaggersInLocation(String microserviceName, String 
swaggersLocation) {
     LOGGER.info("register schemas in location [{}], microserviceName=[{}]", 
swaggersLocation, microserviceName);
     try {
-      List<URI> resourceUris = ResourceUtil.loadResources(swaggersLocation, 
ResourceUtil.matchSuffix(".yaml"));
+      List<URI> resourceUris = 
ResourceUtil.findResourcesBySuffix(swaggersLocation, ".yaml");
       if (resourceUris.isEmpty()) {
         LOGGER.error("register swagger in not exist location: \"{}\".", 
swaggersLocation);
         return;
diff --git 
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/swagger/TestSwaggerLoader.java
 
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/swagger/TestSwaggerLoader.java
index 46caee4..857ec14 100644
--- 
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/swagger/TestSwaggerLoader.java
+++ 
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/swagger/TestSwaggerLoader.java
@@ -33,7 +33,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.function.Function;
+import java.util.function.Predicate;
 
 import javax.xml.ws.Holder;
 
@@ -238,7 +238,7 @@ public class TestSwaggerLoader extends TestRegistryBase {
     }.getMockInstance();
     new MockUp<ResourceUtil>() {
       @Mock
-      List<URI> loadResources(String directory, Function<Path, Boolean> 
filter) {
+      List<URI> findResources(String directory, Predicate<Path> filter) {
         return Collections.singletonList(uri);
       }
     };

Reply via email to