1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36
37
38
39
40
41 public abstract class SimilaritySupplier implements Supplier<Similarity> {
42
43
44 private static final Log LOG =
45 LogFactory.getLog(SimilaritySupplier.class.getName());
46
47
48
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
58
59
60
61
62
63
64
65
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
108
109
110 public abstract Similarity get();
111 }