LUCENE-4044: dont use instances just class names

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene2510@1365106 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-07-24 15:01:12 +00:00
parent 0e80c044b2
commit 5b1664349a
7 changed files with 40 additions and 23 deletions

View File

@ -28,7 +28,7 @@ import java.util.ServiceLoader;
* Helper class for loading named SPIs from classpath (e.g. Tokenizers, TokenStreams).
* @lucene.internal
*/
public final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
private final Map<String,Class<? extends S>> services;
private final Class<S> clazz;
@ -93,19 +93,25 @@ public final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
return ('0' <= c && c <= '9');
}
// TODO: do we even need this method?
public S newInstance(String name) {
final Class<? extends S> service = lookupClass(name);
try {
return service.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("SPI class of type "+clazz.getName()+" with name '"+name+"' cannot be instantiated. " +
"This is likely due to a misconfiguration of the java class '" + service.getName() + "': ", e);
}
}
public Class<? extends S> lookupClass(String name) {
final Class<? extends S> service = services.get(name.toLowerCase(Locale.ROOT));
if (service != null) {
try {
return service.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("SPI class of type "+clazz.getName()+" with name '"+name+"' cannot be instantiated. " +
"This is likely due to a misconfiguration of the java class '" + service.getName() + "': ", e);
}
return service;
} else {
throw new IllegalArgumentException("A SPI class of type "+clazz.getName()+" with name '"+name+"' does not exist. "+
"You need to add the corresponding JAR file supporting this SPI to your classpath."+
"The current classpath supports the following names: "+availableServices());
"You need to add the corresponding JAR file supporting this SPI to your classpath."+
"The current classpath supports the following names: "+availableServices());
}
}

View File

@ -36,6 +36,10 @@ public abstract class CharFilterFactory extends AbstractAnalysisFactory {
return loader.newInstance(name);
}
public static Class<? extends CharFilterFactory> lookupClass(String name) {
return loader.lookupClass(name);
}
/** returns a list of all available charfilter names */
public static Set<String> availableCharFilters() {
return loader.availableServices();

View File

@ -36,6 +36,10 @@ public abstract class TokenFilterFactory extends AbstractAnalysisFactory {
return loader.newInstance(name);
}
public static Class<? extends TokenFilterFactory> lookupClass(String name) {
return loader.lookupClass(name);
}
/** returns a list of all available tokenfilter names */
public static Set<String> availableTokenFilters() {
return loader.availableServices();

View File

@ -36,6 +36,10 @@ public abstract class TokenizerFactory extends AbstractAnalysisFactory {
return loader.newInstance(name);
}
public static Class<? extends TokenizerFactory> lookupClass(String name) {
return loader.lookupClass(name);
}
/** returns a list of all available tokenizer names */
public static Set<String> availableTokenizers() {
return loader.availableServices();

View File

@ -299,14 +299,14 @@
<fieldType name="uima_sentences" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="org.apache.solr.uima.analysis.UIMAAnnotationsTokenizerFactory"
<tokenizer class="solr.UIMAAnnotationsTokenizerFactory"
descriptorPath="/uima/AggregateSentenceAE.xml" tokenType="org.apache.uima.SentenceAnnotation"/>
</analyzer>
</fieldType>
<fieldType name="uima_nouns" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="org.apache.solr.uima.analysis.UIMATypeAwareAnnotationsTokenizerFactory"
<tokenizer class="solr.UIMATypeAwareAnnotationsTokenizerFactory"
descriptorPath="/uima/AggregateSentenceAE.xml" tokenType="org.apache.uima.TokenAnnotation"
featurePath="posTag"/>
<filter class="solr.TypeTokenFilterFactory" types="uima/stoptypes.txt" />

View File

@ -37,19 +37,18 @@ public abstract class AnalysisPluginLoader<S extends AbstractAnalysisFactory> ex
@Override
protected S create(ResourceLoader loader, String name, String className, Node node) throws Exception {
S instance = null;
Class<? extends S> clazz = null;
Matcher m = legacyPattern.matcher(className);
if (m.matches()) {
try {
instance = createSPI(m.group(4));
clazz = lookupSPI(m.group(4));
} catch (IllegalArgumentException ex) {
// ok
}
}
if (instance != null) {
// necessary because SolrResourceLoader manages its own list of 'awaiting ResourceLoaderAware'
className = instance.getClass().getName();
if (clazz != null) {
className = clazz.getName();
}
return super.create(loader, name, className, node);
@ -58,5 +57,5 @@ public abstract class AnalysisPluginLoader<S extends AbstractAnalysisFactory> ex
private static final Pattern legacyPattern =
Pattern.compile("((org\\.apache\\.solr\\.analysis\\.)|(solr\\.))([\\p{L}_$][\\p{L}\\p{N}_$]+?)(TokenFilter|Filter|Tokenizer|CharFilter)Factory");
protected abstract S createSPI(String name);
protected abstract Class<? extends S> lookupSPI(String name);
}

View File

@ -268,8 +268,8 @@ public final class FieldTypePluginLoader
("[schema.xml] analyzer/charFilter", CharFilterFactory.class, false, false) {
@Override
protected CharFilterFactory createSPI(String name) {
return CharFilterFactory.forName(name);
protected Class<? extends CharFilterFactory> lookupSPI(String name) {
return CharFilterFactory.lookupClass(name);
}
@Override
@ -306,8 +306,8 @@ public final class FieldTypePluginLoader
("[schema.xml] analyzer/tokenizer", TokenizerFactory.class, false, false) {
@Override
protected TokenizerFactory createSPI(String name) {
return TokenizerFactory.forName(name);
protected Class<? extends TokenizerFactory> lookupSPI(String name) {
return TokenizerFactory.lookupClass(name);
}
@Override
@ -349,8 +349,8 @@ public final class FieldTypePluginLoader
{
@Override
protected TokenFilterFactory createSPI(String name) {
return TokenFilterFactory.forName(name);
protected Class<? extends TokenFilterFactory> lookupSPI(String name) {
return TokenFilterFactory.lookupClass(name);
}
@Override