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 | |
---|
19 | package org.apache.hadoop.io.serializer; |
---|
20 | |
---|
21 | import java.util.ArrayList; |
---|
22 | import java.util.List; |
---|
23 | |
---|
24 | import org.apache.commons.logging.Log; |
---|
25 | import org.apache.commons.logging.LogFactory; |
---|
26 | import org.apache.hadoop.conf.Configuration; |
---|
27 | import org.apache.hadoop.conf.Configured; |
---|
28 | import org.apache.hadoop.util.ReflectionUtils; |
---|
29 | import org.apache.hadoop.util.StringUtils; |
---|
30 | |
---|
31 | /** |
---|
32 | * <p> |
---|
33 | * A factory for {@link Serialization}s. |
---|
34 | * </p> |
---|
35 | */ |
---|
36 | public 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 | } |
---|