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