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  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.lucene.document.Document;
23  
24  import net.sf.eos.EosException;
25  import net.sf.eos.config.Configuration;
26  import net.sf.eos.config.ConfigurationKey;
27  import net.sf.eos.config.Configured;
28  import net.sf.eos.config.FactoryMethod;
29  import net.sf.eos.document.EosDocument;
30  
31  /**
32   * To support different strategies of Lucene document creation this
33   * factory decoupled the creation of the document creator from hard coded
34   * classnames. Set the classname of a factory different from
35   * <i>{@linkplain DefaultLuceneDocumentCreator default}</i> implementation.
36   * {@link #DOCUMENT_CREATOR_IMPL_CONFIG_NAME} contains the name of the
37   * configuration key.
38   *
39   * <p>Implementations must have a default constructor and must implement
40   * {@link #createLuceneForEosDocument(EosDocument)}.</p>
41   * @author Sascha Kohlmann
42   */
43  public abstract class LuceneDocumentCreator extends Configured {
44  
45      /** For logging. */
46      private static final Log LOG = 
47          LogFactory.getLog(LuceneDocumentCreator.class.getName());
48  
49      /** The configuration key name for the classname of the creator.
50       * @see #newInstance(Configuration) */
51      @SuppressWarnings("nls")
52      @ConfigurationKey(type=CLASSNAME,
53                        description="Configuration key of the Lucene document "
54                                    + " creator.")
55      public final static String DOCUMENT_CREATOR_IMPL_CONFIG_NAME = 
56          "net.sf.eos.lucene.LuceneDocumentCreator.impl";
57  
58      /**
59       * Creates a new instance of a of the creator. If the
60       * {@code Configuration} contains a key
61       * {@link #DOCUMENT_CREATOR_IMPL_CONFIG_NAME} a new instance of the
62       * classname in the value will instantiate. The 
63       * {@link DefaultLuceneDocumentCreator} will instantiate if there is no
64       * value setted.
65       * @param config the configuration
66       * @return a new instance
67       * @throws EosException if it is not possible to instantiate an instance
68       */
69      @FactoryMethod(key=DOCUMENT_CREATOR_IMPL_CONFIG_NAME,
70                         implementation=DefaultLuceneDocumentCreator.class)
71      public final static LuceneDocumentCreator
72              newInstance(final Configuration config) throws EosException {
73  
74          final Thread t = Thread.currentThread();
75          ClassLoader classLoader = t.getContextClassLoader();
76          if (classLoader == null) {
77              classLoader = LuceneDocumentCreator.class.getClassLoader();
78          }
79  
80          final String clazzName = config.get(DOCUMENT_CREATOR_IMPL_CONFIG_NAME,
81                  DefaultLuceneDocumentCreator.class.getName());
82  
83          try {
84              final Class<? extends LuceneDocumentCreator> clazz =
85                  (Class<? extends LuceneDocumentCreator>) Class
86                      .forName(clazzName, true, classLoader);
87              try {
88  
89                  final LuceneDocumentCreator creator = clazz.newInstance();
90                  if (LOG.isDebugEnabled()) {
91                      LOG.debug("LuceneDocumentCreator instance: "
92                                + creator.getClass().getName());
93                  }
94                  return creator;
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      * Creates a Lucene {@code Document} for a given
108      * {@code EosDocument}.
109      * @param doc the document to transform
110      * @return a Lucene {@code Document} for indexing
111      * @throws EosException if transformation fails
112      */
113     public abstract Document createLuceneForEosDocument(final EosDocument doc)
114         throws EosException;
115 }