Copilot commented on code in PR #13209: URL: https://github.com/apache/gravitino/pull/13209#discussion_r4024167471
########## spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/plugin/SparkCatalogExtensions.java: ########## @@ -0,0 +1,93 @@ +/* + * 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.spark.connector.plugin; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import javax.annotation.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Resolves Spark catalog classes contributed through {@link SparkCatalogExtension}. */ +public class SparkCatalogExtensions { + + private static final Logger LOG = LoggerFactory.getLogger(SparkCatalogExtensions.class); + + private static volatile Map<String, String> catalogClassNamesByProvider; + + private SparkCatalogExtensions() {} + + /** + * Looks up the Spark catalog class an extension registered for a provider. + * + * @param provider the Gravitino catalog provider + * @return the catalog class name, or null when no extension serves the provider + */ + @Nullable + public static String catalogClassName(String provider) { + return extensions().get(provider.toLowerCase(Locale.ROOT)); + } + + private static Map<String, String> extensions() { + Map<String, String> loaded = catalogClassNamesByProvider; + if (loaded == null) { + synchronized (SparkCatalogExtensions.class) { + loaded = catalogClassNamesByProvider; + if (loaded == null) { + loaded = load(); + catalogClassNamesByProvider = loaded; + } + } + } + return loaded; + } + + private static Map<String, String> load() { + Map<String, String> byProvider = new HashMap<>(); + Iterator<SparkCatalogExtension> iterator = + ServiceLoader.load( + SparkCatalogExtension.class, SparkCatalogExtensions.class.getClassLoader()) + .iterator(); Review Comment: Using `SparkCatalogExtensions.class.getClassLoader()` limits discovery to the connector's defining loader, so an extension jar added through Spark's user/context classloader (for example with `--jars`) may have neither its provider class nor its `META-INF/services` entry visible here. The test does not catch this because the fake provider is on the same test classpath. Load the service with the context classloader (or the no-argument `ServiceLoader.load`) so the external-jar SPI described by #13208 is actually discoverable. This issue also appears on line 86 of the same file. -- 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]
