sunxiaojian commented on code in PR #5868:
URL: https://github.com/apache/gravitino/pull/5868#discussion_r1889572342


##########
flink-connector/flink/src/main/java/org/apache/gravitino/flink/connector/catalog/BaseCatalogFactory.java:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.flink.connector.catalog;
+
+import org.apache.flink.table.factories.CatalogFactory;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.flink.connector.PartitionConverter;
+import org.apache.gravitino.flink.connector.PropertiesConverter;
+
+public interface BaseCatalogFactory extends CatalogFactory {
+
+  /**
+   * Define gravitino catalog provider {@link 
org.apache.gravitino.CatalogProvider}.
+   *
+   * @return

Review Comment:
   fixed



##########
flink-connector/flink/src/main/java/org/apache/gravitino/flink/connector/store/GravitinoCatalogStore.java:
##########
@@ -96,43 +105,56 @@ public boolean contains(String catalogName) throws 
CatalogException {
     return gravitinoCatalogManager.contains(catalogName);
   }
 
-  private String getGravitinoCatalogProvider(Configuration configuration) {
+  private BaseCatalogFactory catalogFactory(Map<String, String> configuration) 
{
     String catalogType =
         Preconditions.checkNotNull(
-            configuration.get(CommonCatalogOptions.CATALOG_TYPE),
+            configuration.get(CommonCatalogOptions.CATALOG_TYPE.key()),
             "%s should not be null.",
             CommonCatalogOptions.CATALOG_TYPE);
 
-    switch (catalogType) {
-      case GravitinoHiveCatalogFactoryOptions.IDENTIFIER:
-        return "hive";
-      default:
-        throw new IllegalArgumentException(
-            String.format("The catalog type is not supported:%s", 
catalogType));
-    }
+    return discoverFactories(
+        catalogFactory -> 
(catalogFactory.factoryIdentifier().equalsIgnoreCase(catalogType)),
+        String.format(
+            "Flink catalog type [%s] matched multiple flink catalog factories, 
it should only match one.",
+            catalogType));
   }
 
-  private Catalog.Type getGravitinoCatalogType(Configuration configuration) {
-    String catalogType =
-        Preconditions.checkNotNull(
-            configuration.get(CommonCatalogOptions.CATALOG_TYPE),
-            "%s should not be null.",
-            CommonCatalogOptions.CATALOG_TYPE);
-
-    switch (catalogType) {
-      case GravitinoHiveCatalogFactoryOptions.IDENTIFIER:
-        return Catalog.Type.RELATIONAL;
-      default:
-        throw new IllegalArgumentException(
-            String.format("The catalog type is not supported:%s", 
catalogType));
-    }
+  private BaseCatalogFactory catalogFactory(String provider) {
+    return discoverFactories(
+        catalogFactory ->
+            ((BaseCatalogFactory) catalogFactory)
+                .gravitinoCatalogProvider()
+                .equalsIgnoreCase(provider),
+        String.format(
+            "Gravitino catalog provider [%s] matched multiple flink catalog 
factories, it should only match one.",
+            provider));
   }
 
-  private PropertiesConverter getPropertiesConverter(String provider) {
-    switch (provider) {
-      case "hive":
-        return HivePropertiesConverter.INSTANCE;
+  private BaseCatalogFactory discoverFactories(Predicate<Factory> predicate, 
String errorMessage) {
+    Iterator<Factory> serviceLoaderIterator = 
ServiceLoader.load(Factory.class).iterator();
+    final List<Factory> factories = new ArrayList<>();
+    while (true) {
+      try {
+        if (!serviceLoaderIterator.hasNext()) {
+          break;
+        }
+        Factory catalogFactory = serviceLoaderIterator.next();
+        if (catalogFactory instanceof BaseCatalogFactory && 
predicate.test(catalogFactory)) {
+          factories.add(catalogFactory);
+        }
+      } catch (Throwable t) {
+        if (t instanceof NoClassDefFoundError) {
+          LOG.debug(
+              "NoClassDefFoundError when loading a " + 
Factory.class.getCanonicalName() + ".", t);
+        } else {
+          throw new RuntimeException("Unexpected error when trying to load 
service provider.", t);
+        }
+      }
+    }
+    // It should only match one.
+    if (factories.size() > 1) {
+      throw new RuntimeException(errorMessage);
     }
-    throw new IllegalArgumentException("The provider is not supported:" + 
provider);
+    return (BaseCatalogFactory) factories.get(0);

Review Comment:
   fixed



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