hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r712102 - in /hadoop/core/trunk: CHANGES.txt src/core/org/apache/hadoop/util/ReflectionUtils.java src/test/org/apache/hadoop/util/TestReflectionUtils.java
Date Fri, 07 Nov 2008 10:12:11 GMT
Author: ddas
Date: Fri Nov  7 02:12:05 2008
New Revision: 712102

URL: http://svn.apache.org/viewvc?rev=712102&view=rev
Log:
HADOOP-4187. Does a runtime lookup for JobConf/JobConfigurable, and if found, invokes the
appropriate configure method. Contributed by Sharad Agarwal.

Modified:
    hadoop/core/trunk/CHANGES.txt
    hadoop/core/trunk/src/core/org/apache/hadoop/util/ReflectionUtils.java
    hadoop/core/trunk/src/test/org/apache/hadoop/util/TestReflectionUtils.java

Modified: hadoop/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/CHANGES.txt?rev=712102&r1=712101&r2=712102&view=diff
==============================================================================
--- hadoop/core/trunk/CHANGES.txt (original)
+++ hadoop/core/trunk/CHANGES.txt Fri Nov  7 02:12:05 2008
@@ -60,6 +60,9 @@
 
     HADOOP-4572. Change INode and its sub-classes to package private. (szetszwo)
 
+    HADOOP-4187. Does a runtime lookup for JobConf/JobConfigurable, and if found,
+    invokes the appropriate configure method. (Sharad Agarwal via ddas)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/core/trunk/src/core/org/apache/hadoop/util/ReflectionUtils.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/core/org/apache/hadoop/util/ReflectionUtils.java?rev=712102&r1=712101&r2=712102&view=diff
==============================================================================
--- hadoop/core/trunk/src/core/org/apache/hadoop/util/ReflectionUtils.java (original)
+++ hadoop/core/trunk/src/core/org/apache/hadoop/util/ReflectionUtils.java Fri Nov  7 02:12:05
2008
@@ -19,6 +19,7 @@
 package org.apache.hadoop.util;
 
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.io.*;
 import java.lang.management.*;
 import java.util.Map;
@@ -26,7 +27,6 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.hadoop.conf.*;
-import org.apache.hadoop.mapred.*;
 
 /**
  * General reflection utils
@@ -53,10 +53,36 @@
       if (theObject instanceof Configurable) {
         ((Configurable) theObject).setConf(conf);
       }
-      if (conf instanceof JobConf && 
-          theObject instanceof JobConfigurable) {
-        ((JobConfigurable)theObject).configure((JobConf) conf);
+      setJobConf(theObject, conf);
+    }
+  }
+  
+  /**
+   * This code is to support backward compatibility and break the compile  
+   * time dependency of core on mapred.
+   * This should be made deprecated along with the mapred package HADOOP-1230. 
+   * Should be removed when mapred package is removed.
+   */
+  private static void setJobConf(Object theObject, Configuration conf) {
+    //If JobConf and JobConfigurable are in classpath, AND
+    //theObject is of type JobConfigurable AND
+    //conf is of type JobConf then
+    //invoke configure on theObject
+    try {
+      Class<?> jobConfClass = 
+        conf.getClassByName("org.apache.hadoop.mapred.JobConf");
+      Class<?> jobConfigurableClass = 
+        conf.getClassByName("org.apache.hadoop.mapred.JobConfigurable");
+       if (jobConfClass.isAssignableFrom(conf.getClass()) &&
+            jobConfigurableClass.isAssignableFrom(theObject.getClass())) {
+        Method configureMethod = 
+          jobConfigurableClass.getMethod("configure", jobConfClass);
+        configureMethod.invoke(theObject, conf);
       }
+    } catch (ClassNotFoundException e) {
+      //JobConf/JobConfigurable not in classpath. no need to configure
+    } catch (Exception e) {
+      throw new RuntimeException("Error in configuring object", e);
     }
   }
 

Modified: hadoop/core/trunk/src/test/org/apache/hadoop/util/TestReflectionUtils.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/test/org/apache/hadoop/util/TestReflectionUtils.java?rev=712102&r1=712101&r2=712102&view=diff
==============================================================================
--- hadoop/core/trunk/src/test/org/apache/hadoop/util/TestReflectionUtils.java (original)
+++ hadoop/core/trunk/src/test/org/apache/hadoop/util/TestReflectionUtils.java Fri Nov  7
02:12:05 2008
@@ -21,6 +21,10 @@
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.HashMap;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.JobConfigurable;
 
 import junit.framework.TestCase;
 
@@ -107,5 +111,26 @@
     
   public static class NoDefaultCtor {
     public NoDefaultCtor(int x) {}
+  }
+  
+  /**
+   * This is to test backward compatibility of ReflectionUtils for 
+   * JobConfigurable objects. 
+   * This should be made deprecated along with the mapred package HADOOP-1230. 
+   * Should be removed when mapred package is removed.
+   */
+  public void testSetConf() {
+    JobConfigurableOb ob = new JobConfigurableOb();
+    ReflectionUtils.setConf(ob, new Configuration());
+    assertFalse(ob.configured);
+    ReflectionUtils.setConf(ob, new JobConf());
+    assertTrue(ob.configured);
+  }
+  
+  private static class JobConfigurableOb implements JobConfigurable {
+    boolean configured;
+    public void configure(JobConf job) {
+      configured = true;
+    }
   }
 }



Mime
View raw message