HADOOP-7328. When a serializer class is missing, return null, not throw an NPE. Contributed by Harsh J Chouraria.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1167363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fbf0035ee6
commit
592aaa1f06
|
@ -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
|
||||
|
|
|
@ -27,10 +27,10 @@ import org.apache.hadoop.classification.InterfaceAudience;
|
|||
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 extends Configured {
|
|||
* <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 extends Configured {
|
|||
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;
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue