1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.eos.hadoop.mapred;
17
18 import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
19 import net.sf.eos.EosException;
20 import net.sf.eos.config.Configuration;
21 import net.sf.eos.config.ConfigurationKey;
22 import net.sf.eos.config.Configured;
23 import net.sf.eos.config.FactoryMethod;
24 import net.sf.eos.sentence.Sentencer;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.io.WritableComparable;
29
30
31
32
33
34 public abstract class AbstractKeyGenerator<K extends WritableComparable>
35 extends Configured implements KeyGenerator<K> {
36
37
38 private static final Log LOG =
39 LogFactory.getLog(AbstractKeyGenerator.class.getName());
40
41 @SuppressWarnings("nls")
42 @ConfigurationKey(type=CLASSNAME,
43 description="Key generator instances support the "
44 + "generation of keys in the map-reduce"
45 + " map task.")
46 public final static String ABSTRACT_KEY_GENERATOR_IMPL_CONFIG_NAME =
47 "net.sf.eos.hadoop.mapred.AbstractKeyGenerator.impl";
48
49
50
51
52
53
54
55
56 @FactoryMethod(key=ABSTRACT_KEY_GENERATOR_IMPL_CONFIG_NAME)
57 public final static AbstractKeyGenerator<? extends WritableComparable>
58 newInstance(final Configuration conf) throws EosException {
59
60 final Thread t = Thread.currentThread();
61 ClassLoader classLoader = t.getContextClassLoader();
62 if (classLoader == null) {
63 classLoader = Sentencer.class.getClassLoader();
64 }
65
66 final String clazzName =
67 conf.get(ABSTRACT_KEY_GENERATOR_IMPL_CONFIG_NAME);
68
69 try {
70 final Class<? extends AbstractKeyGenerator> clazz =
71 (Class<? extends AbstractKeyGenerator>)
72 Class.forName(clazzName, true, classLoader);
73 try {
74
75 final AbstractKeyGenerator instance = clazz.newInstance();
76 if (LOG.isDebugEnabled()) {
77 LOG.debug("AbstractKeyGenerator instance: "
78 + instance.getClass().getName());
79 }
80 instance.configure(conf);
81 return instance;
82
83 } catch (final InstantiationException e) {
84 throw new EosException(e);
85 } catch (final IllegalAccessException e) {
86 throw new EosException(e);
87 }
88 } catch (final ClassNotFoundException e) {
89 throw new EosException(e);
90 }
91 }
92 }