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.lucene;
17  
18  import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
19  import net.sf.eos.EosException;
20  import net.sf.eos.Supplier;
21  import net.sf.eos.config.Configuration;
22  import net.sf.eos.config.ConfigurationKey;
23  import net.sf.eos.config.FactoryMethod;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.lucene.analysis.Analyzer;
28  
29  
30  /**
31   * To support different strategies of Lucene analyzers this
32   * factory decoupled the creation of the analyzer from hard coded classnames.
33   * Set the classname of a factory different from
34   * <i>{@linkplain WhitespaceAnalyzerSupplier default}</i> implementation.
35   * {@link #ANALYZER_PROVIDER_IMPL_CONFIG_NAME} contains the name of the
36   * configuration key.
37   *
38   * <p>Implementations must have a default constructor and must implement
39   * {@link #get()}.</p>
40   * @author Sascha Kohlmann
41   */
42  public abstract class AnalyzerSupplier implements Supplier<Analyzer> {
43  
44      /** For logging. */
45      private static final Log LOG =
46          LogFactory.getLog(AnalyzerSupplier.class.getName());
47  
48      /** The configuration key name for the classname of the factory.
49       * @see #newInstance(Configuration) */
50      @SuppressWarnings("nls")
51      @ConfigurationKey(type=CLASSNAME,
52                              description="Configuration key of the Lucene analyzer "
53                                          + " factory.")
54      public final static String ANALYZER_PROVIDER_IMPL_CONFIG_NAME = 
55          "net.sf.eos.lucene.AnalyzerProvider.impl";
56  
57      /**
58       * Creates a new instance of a of the provider. If the
59       * {@code Configuration} contains a key
60       * {@link #ANALYZER_PROVIDER_IMPL_CONFIG_NAME} a new instance of the
61       * classname in the value will instantiate. The 
62       * {@link WhitespaceAnalyzerSupplier} will instantiate if there is no
63       * value setted.
64       * @param config the configuration
65       * @return a new instance
66       * @throws EosException if it is not possible to instantiate an instance
67       */
68      @FactoryMethod(key=ANALYZER_PROVIDER_IMPL_CONFIG_NAME)
69      public final static AnalyzerSupplier newInstance(final Configuration config)
70              throws EosException {
71  
72          final Thread t = Thread.currentThread();
73          ClassLoader classLoader = t.getContextClassLoader();
74          if (classLoader == null) {
75              classLoader = AnalyzerSupplier.class.getClassLoader();
76          }
77  
78          final String clazzName =
79              config.get(ANALYZER_PROVIDER_IMPL_CONFIG_NAME,
80                         WhitespaceAnalyzerSupplier.class.getName());
81  
82          try {
83              final Class<? extends AnalyzerSupplier> clazz =
84                  (Class<? extends AnalyzerSupplier>) Class
85                      .forName(clazzName, true, classLoader);
86              try {
87  
88                  final AnalyzerSupplier factory = clazz.newInstance();
89                  if (LOG.isDebugEnabled()) {
90                      LOG.debug("AnalyzerProvider instance: "
91                                + factory.getClass().getName());
92                  }
93                  return factory;
94  
95              } catch (final InstantiationException e) {
96                  throw new EosException(e);
97              } catch (final IllegalAccessException e) {
98                  throw new EosException(e);
99              }
100         } catch (final ClassNotFoundException e) {
101             throw new EosException(e);
102         }
103     }
104 
105     /**
106      * Returns a new {@code Analyzer} instance.
107      * @return a new {@code Analyzer} instance
108      */
109     public abstract Analyzer get();
110 }