hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject svn commit: r1167363 - in /hadoop/common/trunk/hadoop-common-project/hadoop-common: CHANGES.txt src/main/java/org/apache/hadoop/io/serializer/SerializationFactory.java src/test/java/org/apache/hadoop/io/serializer/TestSerializationFactory.java
Date Fri, 09 Sep 2011 21:18:07 GMT
Author: todd
Date: Fri Sep  9 21:18:06 2011
New Revision: 1167363

URL: http://svn.apache.org/viewvc?rev=1167363&view=rev
Log:
HADOOP-7328. When a serializer class is missing, return null, not throw an NPE. Contributed
by Harsh J Chouraria.

Added:
    hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/serializer/TestSerializationFactory.java
Modified:
    hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt
    hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/serializer/SerializationFactory.java

Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt?rev=1167363&r1=1167362&r2=1167363&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt Fri Sep  9 21:18:06
2011
@@ -573,6 +573,9 @@ Release 0.23.0 - Unreleased
     HADOOP-7598. Fix smart-apply-patch.sh to handle patching from a sub
     directory correctly. (Robert Evans via acmurthy) 
 
+    HADOOP-7328. When a serializer class is missing, return null, not throw
+    an NPE. (Harsh J Chouraria via todd)
+
 Release 0.22.0 - Unreleased
 
   INCOMPATIBLE CHANGES

Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/serializer/SerializationFactory.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/serializer/SerializationFactory.java?rev=1167363&r1=1167362&r2=1167363&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/serializer/SerializationFactory.java
(original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/serializer/SerializationFactory.java
Fri Sep  9 21:18:06 2011
@@ -27,10 +27,10 @@ import org.apache.hadoop.classification.
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.conf.Configured;
+import org.apache.hadoop.fs.CommonConfigurationKeys;
 import org.apache.hadoop.io.serializer.avro.AvroReflectSerialization;
 import org.apache.hadoop.io.serializer.avro.AvroSpecificSerialization;
 import org.apache.hadoop.util.ReflectionUtils;
-import org.apache.hadoop.util.StringUtils;
 
 /**
  * <p>
@@ -50,14 +50,15 @@ public class SerializationFactory extend
    * <p>
    * Serializations are found by reading the <code>io.serializations</code>
    * property from <code>conf</code>, which is a comma-delimited list of
-   * classnames. 
+   * classnames.
    * </p>
    */
   public SerializationFactory(Configuration conf) {
     super(conf);
-    for (String serializerName : conf.getStrings("io.serializations", 
-      new String[]{WritableSerialization.class.getName(), 
-        AvroSpecificSerialization.class.getName(), 
+    for (String serializerName : conf.getStrings(
+      CommonConfigurationKeys.IO_SERIALIZATIONS_KEY,
+      new String[]{WritableSerialization.class.getName(),
+        AvroSpecificSerialization.class.getName(),
         AvroReflectSerialization.class.getName()})) {
       add(conf, serializerName);
     }
@@ -67,27 +68,35 @@ public class SerializationFactory extend
   private void add(Configuration conf, String serializationName) {
     try {
       Class<? extends Serialization> serializionClass =
-	(Class<? extends Serialization>) conf.getClassByName(serializationName);
+        (Class<? extends Serialization>) conf.getClassByName(serializationName);
       serializations.add((Serialization)
-	  ReflectionUtils.newInstance(serializionClass, getConf()));
+      ReflectionUtils.newInstance(serializionClass, getConf()));
     } catch (ClassNotFoundException e) {
       LOG.warn("Serialization class not found: ", e);
     }
   }
 
   public <T> Serializer<T> getSerializer(Class<T> c) {
-    return getSerialization(c).getSerializer(c);
+    Serialization<T> serializer = getSerialization(c);
+    if (serializer != null) {
+      return serializer.getSerializer(c);
+    }
+    return null;
   }
 
   public <T> Deserializer<T> getDeserializer(Class<T> c) {
-    return getSerialization(c).getDeserializer(c);
+    Serialization<T> serializer = getSerialization(c);
+    if (serializer != null) {
+      return serializer.getDeserializer(c);
+    }
+    return null;
   }
 
   @SuppressWarnings("unchecked")
   public <T> Serialization<T> getSerialization(Class<T> c) {
     for (Serialization serialization : serializations) {
       if (serialization.accept(c)) {
-	return (Serialization<T>) serialization;
+        return (Serialization<T>) serialization;
       }
     }
     return null;

Added: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/serializer/TestSerializationFactory.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/serializer/TestSerializationFactory.java?rev=1167363&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/serializer/TestSerializationFactory.java
(added)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/serializer/TestSerializationFactory.java
Fri Sep  9 21:18:06 2011
@@ -0,0 +1,44 @@
+/**
+ * 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.hadoop.io.serializer;
+
+import org.junit.Test;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.Writable;
+
+public class TestSerializationFactory {
+
+  @Test
+  public void testSerializerAvailability() {
+    Configuration conf = new Configuration();
+    SerializationFactory factory = new SerializationFactory(conf);
+    // Test that a valid serializer class is returned when its present
+    assertNotNull("A valid class must be returned for default Writable Serde",
+        factory.getSerializer(Writable.class));
+    assertNotNull("A valid class must be returned for default Writable serDe",
+        factory.getDeserializer(Writable.class));
+    // Test that a null is returned when none can be found.
+    assertNull("A null should be returned if there are no serializers found.",
+        factory.getSerializer(TestSerializationFactory.class));
+    assertNull("A null should be returned if there are no deserializers found",
+        factory.getDeserializer(TestSerializationFactory.class));
+  }
+}



Mime
View raw message