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