yuqi1129 commented on code in PR #5020:
URL: https://github.com/apache/gravitino/pull/5020#discussion_r1802540870


##########
catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/fs/FileSystemUtils.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.gravitino.catalog.hadoop.fs;
+
+import static 
org.apache.gravitino.catalog.hadoop.HadoopCatalogPropertiesMetadata.BUILTIN_HDFS_FS_PROVIDER;
+import static 
org.apache.gravitino.catalog.hadoop.HadoopCatalogPropertiesMetadata.BUILTIN_LOCAL_FS_PROVIDER;
+
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import com.google.common.collect.Streams;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class FileSystemUtils {
+
+  private FileSystemUtils() {}
+
+  public static Map<String, FileSystemProvider> getFileSystemProviders(String 
fileSystemProviders) {
+    Map<String, FileSystemProvider> resultMap = Maps.newHashMap();
+    ServiceLoader<FileSystemProvider> allFileSystemProviders =
+        ServiceLoader.load(FileSystemProvider.class);
+
+    Set<String> providersInUses =
+        fileSystemProviders != null
+            ? Arrays.stream(fileSystemProviders.split(","))
+                .map(String::trim)
+                .collect(java.util.stream.Collectors.toSet())
+            : Sets.newHashSet();
+
+    // Add built-in file system providers to the use list automatically.
+    providersInUses.add(BUILTIN_LOCAL_FS_PROVIDER);
+    providersInUses.add(BUILTIN_HDFS_FS_PROVIDER);
+
+    // Only get the file system providers that are in the user list and check 
if the scheme is
+    // unique.
+    Streams.stream(allFileSystemProviders.iterator())
+        .filter(fileSystemProvider -> 
providersInUses.contains(fileSystemProvider.name()))
+        .forEach(
+            fileSystemProvider -> {
+              if (resultMap.containsKey(fileSystemProvider.scheme())) {
+                throw new UnsupportedOperationException(
+                    String.format(
+                        "File system provider: '%s' with scheme '%s' already 
exists in the use provider list "
+                            + "Please make sure the file system provider 
scheme is unique.",
+                        fileSystemProvider.getClass().getName(), 
fileSystemProvider.scheme()));
+              }
+              resultMap.put(fileSystemProvider.scheme(), fileSystemProvider);
+            });
+
+    // If not all file system providers in providersInUses was found, throw an 
exception.
+    Set<String> notFoundProviders =
+        Sets.difference(
+                providersInUses,
+                resultMap.values().stream()
+                    .map(FileSystemProvider::name)
+                    .collect(Collectors.toSet()))
+            .immutableCopy();
+    if (!notFoundProviders.isEmpty()) {
+      throw new UnsupportedOperationException(
+          String.format(
+              "File system providers %s not found in the classpath. Please 
make sure the file system "
+                  + "provider is in the classpath.",
+              notFoundProviders));
+    }
+
+    return resultMap;
+  }
+
+  public static FileSystemProvider getFileSystemProviderByName(
+      Map<String, FileSystemProvider> fileSystemProviders, String 
fileSystemProviderName) {
+    return fileSystemProviders.entrySet().stream()
+        .filter(entry -> 
entry.getValue().name().equals(fileSystemProviderName))
+        .map(Map.Entry::getValue)
+        .findFirst()
+        .orElse(null);

Review Comment:
   Sorry, forgot to change here accordingly. 



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to