source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/io/serializer/SerializationFactory.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.io.serializer;
20
21import java.util.ArrayList;
22import java.util.List;
23
24import org.apache.commons.logging.Log;
25import org.apache.commons.logging.LogFactory;
26import org.apache.hadoop.conf.Configuration;
27import org.apache.hadoop.conf.Configured;
28import org.apache.hadoop.util.ReflectionUtils;
29import org.apache.hadoop.util.StringUtils;
30
31/**
32 * <p>
33 * A factory for {@link Serialization}s.
34 * </p>
35 */
36public class SerializationFactory extends Configured {
37 
38  private static final Log LOG =
39    LogFactory.getLog(SerializationFactory.class.getName());
40
41  private List<Serialization<?>> serializations = new ArrayList<Serialization<?>>();
42 
43  /**
44   * <p>
45   * Serializations are found by reading the <code>io.serializations</code>
46   * property from <code>conf</code>, which is a comma-delimited list of
47   * classnames.
48   * </p>
49   */
50  public SerializationFactory(Configuration conf) {
51    super(conf);
52    for (String serializerName : conf.getStrings("io.serializations", 
53      new String[]{"org.apache.hadoop.io.serializer.WritableSerialization"})) {
54      add(conf, serializerName);
55    }
56  }
57 
58  @SuppressWarnings("unchecked")
59  private void add(Configuration conf, String serializationName) {
60    try {
61     
62      Class<? extends Serialization> serializionClass =
63        (Class<? extends Serialization>) conf.getClassByName(serializationName);
64      serializations.add((Serialization)
65          ReflectionUtils.newInstance(serializionClass, getConf()));
66    } catch (ClassNotFoundException e) {
67      LOG.warn("Serilization class not found: " +
68          StringUtils.stringifyException(e));
69    }
70  }
71
72  public <T> Serializer<T> getSerializer(Class<T> c) {
73    return getSerialization(c).getSerializer(c);
74  }
75
76  public <T> Deserializer<T> getDeserializer(Class<T> c) {
77    return getSerialization(c).getDeserializer(c);
78  }
79
80  @SuppressWarnings("unchecked")
81  public <T> Serialization<T> getSerialization(Class<T> c) {
82    for (Serialization serialization : serializations) {
83      if (serialization.accept(c)) {
84        return (Serialization<T>) serialization;
85      }
86    }
87    return null;
88  }
89}
Note: See TracBrowser for help on using the repository browser.