leonardBang commented on code in PR #23036:
URL: https://github.com/apache/flink/pull/23036#discussion_r1271318526


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/factories/TableFactoryUtil.java:
##########
@@ -196,4 +204,92 @@ public ClassLoader getUserClassLoader() {
                                                 }))
                 .collect(Collectors.toList());
     }
+
+    /**
+     * Finds and creates a {@link CatalogStore} using the provided {@link 
Configuration} and user
+     * classloader.
+     *
+     * <p>The configuration format should be as follows:
+     *
+     * <pre>{@code
+     * table.catalog-store.kind: {identifier}
+     * table.catalog-store.{identifier}.{param1}: xxx
+     * table.catalog-store.{identifier}.{param2}: xxx
+     * }</pre>
+     */
+    public static CatalogStore findAndCreateCatalogStore(
+            Configuration configuration, ClassLoader classLoader) {
+        String identifier = 
configuration.getString(CommonCatalogOptions.TABLE_CATALOG_STORE_KIND);
+        String catalogStoreOptionPrefix =
+                CommonCatalogOptions.TABLE_CATALOG_STORE_OPTION_PREFIX + 
identifier + ".";
+        Map<String, String> options =
+                new DelegatingConfiguration(configuration, 
catalogStoreOptionPrefix).toMap();
+        CatalogStoreFactory catalogStoreFactory =
+                FactoryUtil.discoverFactory(classLoader, 
CatalogStoreFactory.class, identifier);
+        CatalogStoreFactory.Context context =
+                new FactoryUtil.DefaultCatalogStoreContext(options, 
configuration, classLoader);
+        catalogStoreFactory.open(context);
+        return CatalogStoreWithFactory.of(
+                catalogStoreFactory.createCatalogStore(context), 
catalogStoreFactory);
+    }
+
+    /**
+     * A wrapper class for {@link CatalogStore} that includes a {@link 
CatalogStoreFactory}.
+     *
+     * <p>This class can be used by users to close both the {@link 
CatalogStore} and {@link
+     * CatalogStoreFactory} instances.
+     */
+    private static class CatalogStoreWithFactory implements CatalogStore {
+
+        private CatalogStore catalogStore;
+
+        private CatalogStoreFactory factory;
+
+        public static CatalogStore of(CatalogStore catalogStore, 
CatalogStoreFactory factory) {
+            return new CatalogStoreWithFactory(catalogStore, factory);
+        }
+
+        private CatalogStoreWithFactory(CatalogStore catalogStore, 
CatalogStoreFactory factory) {
+            this.catalogStore = catalogStore;
+            this.factory = factory;
+        }
+
+        @Override
+        public void storeCatalog(String catalogName, CatalogDescriptor catalog)
+                throws CatalogException {
+            catalogStore.storeCatalog(catalogName, catalog);
+        }
+
+        @Override
+        public void removeCatalog(String catalogName, boolean 
ignoreIfNotExists)
+                throws CatalogException {
+            catalogStore.removeCatalog(catalogName, ignoreIfNotExists);
+        }
+
+        @Override
+        public Optional<CatalogDescriptor> getCatalog(String catalogName) 
throws CatalogException {
+            return catalogStore.getCatalog(catalogName);
+        }
+
+        @Override
+        public Set<String> listCatalogs() throws CatalogException {
+            return catalogStore.listCatalogs();
+        }
+
+        @Override
+        public boolean contains(String catalogName) throws CatalogException {
+            return catalogStore.contains(catalogName);
+        }
+
+        @Override
+        public void open() throws CatalogException {
+            catalogStore.open();
+        }
+
+        @Override
+        public void close() throws CatalogException {
+            catalogStore.close();
+            factory.close();

Review Comment:
   The only usage is here, could we use something like 
`CatalogStoreFactoryHolder` which hold the resource(i.e. CatalogStoreFactory).



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