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.store.Directory;
23  
24  import net.sf.eos.EosException;
25  import net.sf.eos.config.ConfigurableSupplier;
26  import net.sf.eos.config.Configuration;
27  import net.sf.eos.config.ConfigurationException;
28  import net.sf.eos.config.ConfigurationKey;
29  import net.sf.eos.config.Configured;
30  import net.sf.eos.config.FactoryMethod;
31  
32  public abstract class DirectorySupplier extends Configured
33          implements ConfigurableSupplier<Directory> {
34  
35      /** For logging. */
36      private static final Log LOG =
37          LogFactory.getLog(DirectorySupplier.class.getName());
38  
39      /** The configuration key name for the classname of the factory.
40       * @see #newInstance(Configuration) */
41      @SuppressWarnings("nls")
42      @ConfigurationKey(type=CLASSNAME,
43                              description="Configuration key of the Lucene directory "
44                                          + " provider factory.")
45      public final static String DIRECTORY_SUPPLIER_IMPL_CONFIG_NAME = 
46          "net.sf.eos.lucene.DirectorySupplier.impl";
47  
48      /**
49       * Creates a new instance of a of the factory. If the
50       * {@code Configuration} contains a key
51       * {@link #DIRECTORY_SUPPLIER_IMPL_CONFIG_NAME} a new instance of the
52       * classname in the value will instantiate. The 
53       * {@link LocalFsDirectorySupplier} will instantiate if there is no
54       * value setted.
55       * @param config the configuration
56       * @return a new instance
57       * @throws EosException if it is not possible to instantiate an instance
58       * @see LocalFsDirectorySupplier
59       */
60      @FactoryMethod(key=DIRECTORY_SUPPLIER_IMPL_CONFIG_NAME,
61                     implementation=LocalFsDirectorySupplier.class)
62      public final static DirectorySupplier newInstance(final Configuration config)
63              throws EosException {
64  
65          final Thread t = Thread.currentThread();
66          ClassLoader classLoader = t.getContextClassLoader();
67          if (classLoader == null) {
68              classLoader = SearcherSupplier.class.getClassLoader();
69          }
70  
71          final String clazzName =
72              config.get(DIRECTORY_SUPPLIER_IMPL_CONFIG_NAME,
73                      LocalFsDirectorySupplier.class.getName());
74  
75          try {
76              final Class<? extends DirectorySupplier> clazz =
77                  (Class<? extends DirectorySupplier>) Class
78                      .forName(clazzName, true, classLoader);
79              try {
80  
81                  final DirectorySupplier factory = clazz.newInstance();
82                  if (LOG.isDebugEnabled()) {
83                      LOG.debug("DirectorySupplier instance: "
84                                + factory.getClass().getName());
85                  }
86                  return factory;
87  
88              } catch (final InstantiationException e) {
89                  throw new EosException(e);
90              } catch (final IllegalAccessException e) {
91                  throw new EosException(e);
92              }
93          } catch (final ClassNotFoundException e) {
94              throw new EosException(e);
95          }
96      }
97  
98      /**
99       * Creates a new directory for the configuration at creation time.
100      * @return a new Lucene {@code Directory} instance.
101      * @throws ConfigurationException may thrown if misconfigured
102      */
103     public Directory get() {
104         final Configuration conf = this.getConfiguration();
105         return get(conf);
106     }
107 
108     /**
109      * Use the given configuration to create a new {@code Directory} instance.
110      * @param conf the configuration to use for {@code Directory} creating
111      * @return a new Lucence {@code Directory}
112      * @throws ConfigurationException may thrown if misconfigured
113      */
114     public abstract Directory get(final Configuration conf);
115 }