jackye1995 commented on a change in pull request #3590:
URL: https://github.com/apache/iceberg/pull/3590#discussion_r756381158



##########
File path: core/src/main/java/org/apache/iceberg/CatalogUtil.java
##########
@@ -266,11 +264,76 @@ public static FileIO loadFileIO(
           String.format("Cannot initialize FileIO, %s does not implement 
FileIO.", impl), e);
     }
 
-    if (fileIO instanceof Configurable) {
-      ((Configurable) fileIO).setConf(hadoopConf);
-    }
+    configureHadoopConf(fileIO, hadoopConf);
 
     fileIO.initialize(properties);
     return fileIO;
   }
+
+  /**
+   * Dynamically detects whether an object is a Hadoop Configurable and calls 
setConf.
+   * @param maybeConfigurable an object that may be Configurable
+   * @param conf a Configuration
+   */
+  @SuppressWarnings("unchecked")
+  public static void configureHadoopConf(Object maybeConfigurable, Object 
conf) {
+    Preconditions.checkArgument(maybeConfigurable != null, "Cannot configure: 
null Configurable");
+    if (conf == null) {
+      return;
+    }
+
+    if (maybeConfigurable instanceof Configurable) {
+      // use the Iceberg configurable interface to pass the conf
+      ((Configurable<Object>) maybeConfigurable).setConf(conf);
+      return;
+    }
+
+    // try to use Hadoop's Configurable interface dynamically
+    // use the classloader of the object that may be configurable
+    ClassLoader maybeConfigurableLoader = 
maybeConfigurable.getClass().getClassLoader();
+
+    Class<?> configurableInterface;
+    try {
+      // load the Configurable interface
+      configurableInterface = DynClasses.builder()
+          .loader(maybeConfigurableLoader)
+          .impl("org.apache.hadoop.conf.Configurable")
+          .buildChecked();
+    } catch (ClassNotFoundException e) {
+      // not Configurable because it was loaded and Configurable is not 
present in its classloader
+      return;
+    }
+
+    if (!configurableInterface.isInstance(maybeConfigurable)) {
+      // not Configurable because the object does not implement the 
Configurable interface
+      return;
+    }
+
+    Class<?> configurationClass;
+    try {
+      configurationClass = DynClasses.builder()
+          .loader(maybeConfigurableLoader)
+          .impl("org.apache.hadoop.conf.Configuration")
+          .buildChecked();
+    } catch (ClassNotFoundException e) {
+      // this shouldn't happen because Configurable cannot be loaded without 
first loading Configuration
+      throw new UnsupportedOperationException("Failed to load Configuration 
after loading Configurable", e);

Review comment:
       I see, I just think configuration was not clear to mean Hadoop 
configuration, but if it is shown in the cause then it should be fine




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to