mchades commented on code in PR #9416:
URL: https://github.com/apache/gravitino/pull/9416#discussion_r2604927561


##########
catalogs/hive-metastore-common/src/main/java/org/apache/gravitino/hive/client/HiveClientClassLoader.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.hive.client;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.exceptions.GravitinoRuntimeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Isolated client loader for Hive Metastore clients. This class creates an 
isolated classloader
+ * that loads Hive-specific classes from version-specific jar files while 
sharing common classes
+ * with the base classloader.
+ */
+public final class HiveClientClassLoader extends URLClassLoader {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HiveClientClassLoader.class);
+
+  public enum HiveVersion {
+    HIVE2,
+    HIVE3,
+  }
+
+  private final ClassLoader baseClassLoader;
+  private final HiveVersion version;
+
+  /**
+   * Constructs an HiveClientClassLoader.
+   *
+   * @param version The Hive version
+   * @param execJars List of jar file URLs to load
+   * @param baseClassLoader The base classloader for shared classes
+   */
+  private HiveClientClassLoader(
+      HiveVersion version, List<URL> execJars, ClassLoader baseClassLoader) {
+    super(version.toString(), execJars.toArray(new URL[0]), null);
+    Preconditions.checkArgument(version != null, "Hive version cannot be 
null");
+    Preconditions.checkArgument(
+        execJars != null && !execJars.isEmpty(), "Jar URLs cannot be null or 
empty");
+    Preconditions.checkArgument(baseClassLoader != null, "Base classloader 
cannot be null");
+
+    this.version = version;
+    this.baseClassLoader = baseClassLoader;
+  }
+
+  public HiveVersion getHiveVersion() {
+    return version;
+  }
+
+  /**
+   * Creates a new {@link HiveClientClassLoader} instance for the given 
version.
+   *
+   * <p>This method does not perform any caching. Callers are responsible for 
managing and
+   * optionally caching returned instances.
+   *
+   * @param hiveVersion The Hive version to create a loader for.
+   * @param baseLoader The parent classloader to delegate shared classes to.
+   * @return A new {@link HiveClientClassLoader} instance.
+   */
+  public static HiveClientClassLoader createLoader(HiveVersion hiveVersion, 
ClassLoader baseLoader)
+      throws IOException {
+    Path jarDir = getJarDirectory(hiveVersion);
+    if (!Files.exists(jarDir) || !Files.isDirectory(jarDir)) {
+      throw new IOException("Hive jar directory does not exist or is not a 
directory: " + jarDir);
+    }
+
+    List<URL> jars = loadJarUrls(jarDir);
+    if (jars.isEmpty()) {
+      throw new IOException("No jar files found in directory: " + jarDir);
+    }
+
+    return new HiveClientClassLoader(hiveVersion, jars, baseLoader);
+  }
+
+  /**
+   * Gets the jar directory path for the specified Hive version.
+   *
+   * @param version The Hive version
+   * @return The path to the jar directory
+   */
+  private static Path getJarDirectory(HiveVersion version) {
+    String gravitinoHome = System.getenv("GRAVITINO_HOME");
+    if (StringUtils.isEmpty(gravitinoHome)) {
+      Path p = Paths.get(System.getProperty("user.dir"));
+      while (p != null) {
+        if 
(Files.exists(p.resolve("catalogs").resolve("hive-metastore-common"))) {
+          gravitinoHome = p.toString();
+          break;
+        }
+        p = p.getParent();
+      }
+    }
+
+    if (StringUtils.isEmpty(gravitinoHome)) {
+      throw new GravitinoRuntimeException(
+          "GRAVITINO_HOME environment variable is not set and cannot determine 
project root directory");
+    }
+
+    String libsDir = version == HiveVersion.HIVE2 ? "hive-metastore2-libs" : 
"hive-metastore3-libs";
+
+    // try to get path from GRAVITINO_HOME in deployment mode
+    String jarPath = Paths.get(gravitinoHome, "catalogs", "hive", "libs", 
libsDir).toString();
+    if (Files.exists(Paths.get(jarPath))) {
+      return Paths.get(jarPath).toAbsolutePath();
+    }
+    LOG.info("Can not find Hive jar directory for version {} in directory : 
{}", version, jarPath);
+
+    // Try to get project root directory from project root in development mode
+    jarPath = Paths.get(gravitinoHome, "catalogs", libsDir, "build", 
"libs").toString();

Review Comment:
   
FYI:https://github.com/apache/gravitino/blob/4e7a40e5a9a9f6a2437d5133bef1d22b45ab65f6/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java#L1139-L1159



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