View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * Simple implementation with a factory.
32   * @author Sascha Kohlmann
33   */
34  public abstract class AbstractKeyGenerator<K extends WritableComparable>
35          extends Configured implements KeyGenerator<K> {
36  
37      /** For logging. */
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       * Creates a new instance.
51       * @param conf the configuration with the configuration data for the
52       *            implementation
53       * @return a new instance
54       * @throws EosException if an error occurs
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  }