diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index f2b24463780..c5112fd7337 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -493,6 +493,9 @@ Other Changes * SOLR-3343: Moved FastWriter, FileUtils, RegexFileFilter, RTimer and SystemIdResolver from org.apache.solr.common to org.apache.solr.util (Chris Male) +* SOLR-3357: ResourceLoader.newInstance now accepts a Class representation of the expected +instance type (Chris Male) + Documentation ---------------------- diff --git a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/ClusteringComponent.java b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/ClusteringComponent.java index 6f19d52e872..c5b345ac9fe 100644 --- a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/ClusteringComponent.java +++ b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/ClusteringComponent.java @@ -200,7 +200,7 @@ public class ClusteringComponent extends SearchComponent implements SolrCoreAwar className = CarrotClusteringEngine.class.getName(); } SolrResourceLoader loader = core.getResourceLoader(); - ClusteringEngine clusterer = (ClusteringEngine) loader.newInstance(className); + ClusteringEngine clusterer = loader.newInstance(className, ClusteringEngine.class); if (clusterer != null) { String name = clusterer.init(engineNL, core); if (name != null) { diff --git a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java index 7c2ecfd957b..df62be8f59c 100644 --- a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java +++ b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java @@ -283,14 +283,7 @@ public class CarrotClusteringEngine extends SearchClusteringEngine { // Make sure the requested Carrot2 clustering algorithm class is available String carrotAlgorithmClassName = initParams.get(CarrotParams.ALGORITHM); - Class algorithmClass = core.getResourceLoader().findClass(carrotAlgorithmClassName); - if (!IClusteringAlgorithm.class.isAssignableFrom(algorithmClass)) { - throw new IllegalArgumentException("Class provided as " - + CarrotParams.ALGORITHM + " must implement " - + IClusteringAlgorithm.class.getName()); - } - this.clusteringAlgorithmClass = (Class) algorithmClass; - + this.clusteringAlgorithmClass = core.getResourceLoader().findClass(carrotAlgorithmClassName, IClusteringAlgorithm.class); return result; } diff --git a/solr/contrib/dataimporthandler-extras/src/java/org/apache/solr/handler/dataimport/TikaEntityProcessor.java b/solr/contrib/dataimporthandler-extras/src/java/org/apache/solr/handler/dataimport/TikaEntityProcessor.java index 25c6f73df86..d07d17914ca 100644 --- a/solr/contrib/dataimporthandler-extras/src/java/org/apache/solr/handler/dataimport/TikaEntityProcessor.java +++ b/solr/contrib/dataimporthandler-extras/src/java/org/apache/solr/handler/dataimport/TikaEntityProcessor.java @@ -121,7 +121,7 @@ public class TikaEntityProcessor extends EntityProcessorBase { if(parser.equals(AUTO_PARSER)){ tikaParser = new AutoDetectParser(tikaConfig); } else { - tikaParser = (Parser) context.getSolrCore().getResourceLoader().newInstance(parser); + tikaParser = context.getSolrCore().getResourceLoader().newInstance(parser, Parser.class); } try { tikaParser.parse(is, contentHandler, metadata , new ParseContext()); diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java index 9612d17e01d..4bcd1a43d02 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java @@ -863,13 +863,13 @@ public class DocBuilder { static Class loadClass(String name, SolrCore core) throws ClassNotFoundException { try { return core != null ? - core.getResourceLoader().findClass(name) : + core.getResourceLoader().findClass(name, Object.class) : Class.forName(name); } catch (Exception e) { try { String n = DocBuilder.class.getPackage().getName() + "." + name; return core != null ? - core.getResourceLoader().findClass(n) : + core.getResourceLoader().findClass(n, Object.class) : Class.forName(n); } catch (Exception e1) { throw new ClassNotFoundException("Unable to load " + name + " or " + DocBuilder.class.getPackage().getName() + "." + name, e); diff --git a/solr/core/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java b/solr/core/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java index c970e94fa74..ff254ae19b3 100644 --- a/solr/core/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java +++ b/solr/core/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java @@ -67,7 +67,7 @@ public class DelimitedPayloadTokenFilterFactory extends BaseTokenFilterFactory i } else if (encoderClass.equals("identity")){ encoder = new IdentityEncoder(); } else { - encoder = (PayloadEncoder) loader.newInstance(encoderClass); + encoder = loader.newInstance(encoderClass, PayloadEncoder.class); } String delim = args.get(DELIMITER_ATTR); diff --git a/solr/core/src/java/org/apache/solr/analysis/FSTSynonymFilterFactory.java b/solr/core/src/java/org/apache/solr/analysis/FSTSynonymFilterFactory.java index 1a9c1b5b2d5..5a142a21f7a 100644 --- a/solr/core/src/java/org/apache/solr/analysis/FSTSynonymFilterFactory.java +++ b/solr/core/src/java/org/apache/solr/analysis/FSTSynonymFilterFactory.java @@ -155,7 +155,7 @@ final class FSTSynonymFilterFactory extends BaseTokenFilterFactory implements Re } private static TokenizerFactory loadTokenizerFactory(ResourceLoader loader, String cname, Map args){ - TokenizerFactory tokFactory = (TokenizerFactory) loader.newInstance(cname); + TokenizerFactory tokFactory = loader.newInstance(cname, TokenizerFactory.class); tokFactory.init(args); if (tokFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokFactory).inform(loader); diff --git a/solr/core/src/java/org/apache/solr/analysis/SlowSynonymFilterFactory.java b/solr/core/src/java/org/apache/solr/analysis/SlowSynonymFilterFactory.java index 83a41e7db6c..f497af8b838 100644 --- a/solr/core/src/java/org/apache/solr/analysis/SlowSynonymFilterFactory.java +++ b/solr/core/src/java/org/apache/solr/analysis/SlowSynonymFilterFactory.java @@ -169,7 +169,7 @@ final class SlowSynonymFilterFactory extends BaseTokenFilterFactory implements R } private static TokenizerFactory loadTokenizerFactory(ResourceLoader loader, String cname, Map args){ - TokenizerFactory tokFactory = (TokenizerFactory)loader.newInstance( cname ); + TokenizerFactory tokFactory = loader.newInstance(cname, TokenizerFactory.class); tokFactory.init( args ); if (tokFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokFactory).inform(loader); diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index 0b8084cab25..02b906fa29b 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -976,14 +976,7 @@ public class CoreContainer protected CoreAdminHandler createMultiCoreHandler(final String adminHandlerClass) { // :TODO: why create a new SolrResourceLoader? why not use this.loader ??? SolrResourceLoader loader = new SolrResourceLoader(solrHome, libLoader, null); - Object obj = loader.newAdminHandlerInstance(CoreContainer.this, adminHandlerClass); - if ( !(obj instanceof CoreAdminHandler)) - { - throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, - "adminHandlerClass is not of type "+ CoreAdminHandler.class ); - - } - return (CoreAdminHandler) obj; + return loader.newAdminHandlerInstance(CoreContainer.this, adminHandlerClass); } public CoreAdminHandler getMultiCoreHandler() { diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java index 10becc42910..f2fd3b131dc 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java @@ -331,7 +331,7 @@ public final class SolrCore implements SolrInfoMBean { DirectoryFactory dirFactory; PluginInfo info = solrConfig.getPluginInfo(DirectoryFactory.class.getName()); if (info != null) { - dirFactory = (DirectoryFactory) getResourceLoader().newInstance(info.className); + dirFactory = getResourceLoader().newInstance(info.className, DirectoryFactory.class); dirFactory.init(info.initArgs); } else { dirFactory = new StandardDirectoryFactory(); @@ -344,7 +344,7 @@ public final class SolrCore implements SolrInfoMBean { IndexReaderFactory indexReaderFactory; PluginInfo info = solrConfig.getPluginInfo(IndexReaderFactory.class.getName()); if (info != null) { - indexReaderFactory = (IndexReaderFactory) resourceLoader.newInstance(info.className); + indexReaderFactory = resourceLoader.newInstance(info.className, IndexReaderFactory.class); indexReaderFactory.init(info.initArgs); } else { indexReaderFactory = new StandardIndexReaderFactory(); @@ -407,14 +407,11 @@ public final class SolrCore implements SolrInfoMBean { *@return the desired instance *@throws SolrException if the object could not be instantiated */ - private T createInstance(String className, Class cast, String msg) { - Class clazz = null; + private T createInstance(String className, Class cast, String msg) { + Class clazz = null; if (msg == null) msg = "SolrCore Object"; try { - clazz = getResourceLoader().findClass(className); - if (cast != null && !cast.isAssignableFrom(clazz)) { - throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " is not a " +cast.getName()); - } + clazz = getResourceLoader().findClass(className, cast); //most of the classes do not have constructors which takes SolrCore argument. It is recommended to obtain SolrCore by implementing SolrCoreAware. // So invariably always it will cause a NoSuchMethodException. So iterate though the list of available constructors Constructor[] cons = clazz.getConstructors(); @@ -424,7 +421,7 @@ public final class SolrCore implements SolrInfoMBean { return (T)con.newInstance(this); } } - return (T) getResourceLoader().newInstance(className);//use the empty constructor + return getResourceLoader().newInstance(className, cast);//use the empty constructor } catch (SolrException e) { throw e; } catch (Exception e) { @@ -432,14 +429,11 @@ public final class SolrCore implements SolrInfoMBean { } } - private T createReloadedUpdateHandler(String className, Class class1, String msg, UpdateHandler updateHandler) { - Class clazz = null; + private UpdateHandler createReloadedUpdateHandler(String className, String msg, UpdateHandler updateHandler) { + Class clazz = null; if (msg == null) msg = "SolrCore Object"; try { - clazz = getResourceLoader().findClass(className); - if (class1 != null && !class1.isAssignableFrom(clazz)) { - throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " is not a " +class1.getName()); - } + clazz = getResourceLoader().findClass(className, UpdateHandler.class); //most of the classes do not have constructors which takes SolrCore argument. It is recommended to obtain SolrCore by implementing SolrCoreAware. // So invariably always it will cause a NoSuchMethodException. So iterate though the list of available constructors Constructor justSolrCoreCon = null; @@ -447,14 +441,14 @@ public final class SolrCore implements SolrInfoMBean { for (Constructor con : cons) { Class[] types = con.getParameterTypes(); if(types.length == 2 && types[0] == SolrCore.class && types[1] == UpdateHandler.class){ - return (T)con.newInstance(this, updateHandler); + return (UpdateHandler) con.newInstance(this, updateHandler); } } - throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " could not find proper constructor for " +class1.getName()); + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " could not find proper constructor for " + UpdateHandler.class.getName()); } catch (SolrException e) { throw e; } catch (Exception e) { - throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " failed to instantiate " +class1.getName(), e); + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Error Instantiating "+msg+", "+className+ " failed to instantiate " + UpdateHandler.class.getName(), e); } } @@ -482,7 +476,7 @@ public final class SolrCore implements SolrInfoMBean { } private UpdateHandler createUpdateHandler(String className, UpdateHandler updateHandler) { - return createReloadedUpdateHandler(className, UpdateHandler.class, "Update Handler", updateHandler); + return createReloadedUpdateHandler(className, "Update Handler", updateHandler); } private QueryResponseWriter createQueryResponseWriter(String className) { @@ -642,7 +636,7 @@ public final class SolrCore implements SolrInfoMBean { final PluginInfo info = solrConfig.getPluginInfo(CodecFactory.class.getName()); final CodecFactory factory; if (info != null) { - factory = (CodecFactory) schema.getResourceLoader().newInstance(info.className); + factory = schema.getResourceLoader().newInstance(info.className, CodecFactory.class); factory.init(info.initArgs); } else { factory = new DefaultCodecFactory(); @@ -925,7 +919,7 @@ public final class SolrCore implements SolrInfoMBean { } private void addIfNotPresent(Map registry, String name, Class c){ if(!registry.containsKey(name)){ - T searchComp = (T) resourceLoader.newInstance(c.getName()); + T searchComp = resourceLoader.newInstance(c.getName(), c); if (searchComp instanceof NamedListInitializedPlugin){ ((NamedListInitializedPlugin)searchComp).init( new NamedList() ); } diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java index ed3ed9d1985..4af1dcbb073 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java +++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java @@ -30,6 +30,7 @@ import java.net.URLClassLoader; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import org.apache.solr.handler.admin.CoreAdminHandler; import org.apache.solr.handler.component.ShardHandlerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -378,13 +379,13 @@ public class SolrResourceLoader implements ResourceLoader * @param subpackages the packages to be tried if the cnams starts with solr. * @return the loaded class. An exception is thrown if it fails */ - public Class findClass(String cname, String... subpackages) { + public Class findClass(String cname, Class expectedType, String... subpackages) { if (subpackages == null || subpackages.length == 0 || subpackages == packages) { subpackages = packages; String c = classNameCache.get(cname); if(c != null) { try { - return Class.forName(c, true, classLoader); + return Class.forName(c, true, classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e) { //this is unlikely log.error("Unable to load cached class-name : "+ c +" for shortname : "+cname + e); @@ -392,10 +393,10 @@ public class SolrResourceLoader implements ResourceLoader } } - Class clazz = null; + Class clazz = null; // first try cname == full name try { - return Class.forName(cname, true, classLoader); + return Class.forName(cname, true, classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e) { String newName=cname; if (newName.startsWith(project)) { @@ -405,7 +406,7 @@ public class SolrResourceLoader implements ResourceLoader try { String name = base + '.' + subpackage + newName; log.trace("Trying class name " + name); - return clazz = Class.forName(name,true,classLoader); + return clazz = Class.forName(name,true,classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e1) { // ignore... assume first exception is best. } @@ -425,14 +426,14 @@ public class SolrResourceLoader implements ResourceLoader } } - public Object newInstance(String cname, String ... subpackages) { - Class clazz = findClass(cname,subpackages); + public T newInstance(String cname, Class expectedType, String ... subpackages) { + Class clazz = findClass(cname, expectedType, subpackages); if( clazz == null ) { throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Can not find class: "+cname + " in " + classLoader); } - Object obj = null; + T obj = null; try { obj = clazz.newInstance(); } @@ -458,17 +459,17 @@ public class SolrResourceLoader implements ResourceLoader return obj; } - public Object newAdminHandlerInstance(final CoreContainer coreContainer, String cname, String ... subpackages) { - Class clazz = findClass(cname,subpackages); + public CoreAdminHandler newAdminHandlerInstance(final CoreContainer coreContainer, String cname, String ... subpackages) { + Class clazz = findClass(cname, CoreAdminHandler.class, subpackages); if( clazz == null ) { throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Can not find class: "+cname + " in " + classLoader); } - Object obj = null; + CoreAdminHandler obj = null; try { - Constructor ctor = clazz.getConstructor(CoreContainer.class); - obj = ctor.newInstance(coreContainer); + Constructor ctor = clazz.getConstructor(CoreContainer.class); + obj = ctor.newInstance(coreContainer); } catch (Exception e) { throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, @@ -489,17 +490,17 @@ public class SolrResourceLoader implements ResourceLoader - public Object newInstance(String cName, String [] subPackages, Class[] params, Object[] args){ - Class clazz = findClass(cName,subPackages); + public T newInstance(String cName, Class expectedType, String [] subPackages, Class[] params, Object[] args){ + Class clazz = findClass(cName, expectedType, subPackages); if( clazz == null ) { throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Can not find class: "+cName + " in " + classLoader); } - Object obj = null; + T obj = null; try { - Constructor constructor = clazz.getConstructor(params); + Constructor constructor = clazz.getConstructor(params); obj = constructor.newInstance(args); } catch (Exception e) { diff --git a/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java b/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java index ee7b12d4fc0..8645a1334d5 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/SpellCheckComponent.java @@ -525,7 +525,7 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwar if (className == null) className = IndexBasedSpellChecker.class.getName(); SolrResourceLoader loader = core.getResourceLoader(); - SolrSpellChecker checker = (SolrSpellChecker) loader.newInstance(className); + SolrSpellChecker checker = loader.newInstance(className, SolrSpellChecker.class); if (checker != null) { String dictionary = checker.init(spellchecker, core); if (dictionary != null) { diff --git a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java index 01cc2b45155..12ed9aad92b 100644 --- a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java +++ b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java @@ -117,14 +117,9 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa args.remove(PARAM_PRECISION_STEP); try { - Class c = schema.getResourceLoader().findClass(exchangeRateProviderClass); - Object clazz = c.newInstance(); - if (clazz instanceof ExchangeRateProvider) { - provider = (ExchangeRateProvider) clazz; - provider.init(args); - } else { - throw new SolrException(ErrorCode.BAD_REQUEST, "exchangeRateProvider "+exchangeRateProviderClass+" needs to implement ExchangeRateProvider"); - } + Class c = schema.getResourceLoader().findClass(exchangeRateProviderClass, ExchangeRateProvider.class); + provider = c.newInstance(); + provider.init(args); } catch (Exception e) { throw new SolrException(ErrorCode.BAD_REQUEST, "Error instansiating exhange rate provider "+exchangeRateProviderClass+". Please check your FieldType configuration", e); } diff --git a/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java b/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java index 1d5e0712573..86bc90a4574 100644 --- a/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java +++ b/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java @@ -61,7 +61,7 @@ public final class FieldTypePluginLoader public FieldTypePluginLoader(final IndexSchema schema, final Map fieldTypes, final Collection schemaAware) { - super("[schema.xml] fieldType", true, true); + super("[schema.xml] fieldType", FieldType.class, true, true); this.schema = schema; this.fieldTypes = fieldTypes; this.schemaAware = schemaAware; @@ -78,7 +78,7 @@ public final class FieldTypePluginLoader String className, Node node ) throws Exception { - FieldType ft = (FieldType)loader.newInstance(className); + FieldType ft = loader.newInstance(className, FieldType.class); ft.setTypeName(name); String expression = "./analyzer[@type='query']"; @@ -228,8 +228,7 @@ public final class FieldTypePluginLoader if (analyzerName != null) { try { // No need to be core-aware as Analyzers are not in the core-aware list - final Class clazz = loader.findClass - (analyzerName).asSubclass(Analyzer.class); + final Class clazz = loader.findClass(analyzerName, Analyzer.class); try { // first try to use a ctor with version parameter @@ -265,7 +264,7 @@ public final class FieldTypePluginLoader = new ArrayList(); AbstractPluginLoader charFilterLoader = new AbstractPluginLoader - ( "[schema.xml] analyzer/charFilter", false, false ) { + ("[schema.xml] analyzer/charFilter", CharFilterFactory.class, false, false) { @Override protected void init(CharFilterFactory plugin, Node node) throws Exception { @@ -298,7 +297,7 @@ public final class FieldTypePluginLoader = new ArrayList(1); AbstractPluginLoader tokenizerLoader = new AbstractPluginLoader - ( "[schema.xml] analyzer/tokenizer", false, false ) { + ("[schema.xml] analyzer/tokenizer", TokenizerFactory.class, false, false) { @Override protected void init(TokenizerFactory plugin, Node node) throws Exception { if( !tokenizers.isEmpty() ) { @@ -335,7 +334,7 @@ public final class FieldTypePluginLoader = new ArrayList(); AbstractPluginLoader filterLoader = - new AbstractPluginLoader( "[schema.xml] analyzer/filter", false, false ) + new AbstractPluginLoader("[schema.xml] analyzer/filter", TokenFilterFactory.class, false, false) { @Override protected void init(TokenFilterFactory plugin, Node node) throws Exception { diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java index 205d73a1d7e..610c56f5740 100644 --- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java +++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java @@ -663,7 +663,7 @@ public final class IndexSchema { return null; } else { SimilarityFactory similarityFactory; - final Object obj = loader.newInstance(((Element) node).getAttribute("class"), "search.similarities."); + final Object obj = loader.newInstance(((Element) node).getAttribute("class"), Object.class, "search.similarities."); if (obj instanceof SimilarityFactory) { // configure a factory, get a similarity back SolrParams params = SolrParams.toSolrParams(DOMUtil.childNodesToNamedList(node)); diff --git a/solr/core/src/java/org/apache/solr/search/CacheConfig.java b/solr/core/src/java/org/apache/solr/search/CacheConfig.java index 5b59b9ac90d..06826d927ac 100644 --- a/solr/core/src/java/org/apache/solr/search/CacheConfig.java +++ b/solr/core/src/java/org/apache/solr/search/CacheConfig.java @@ -39,7 +39,7 @@ import javax.xml.xpath.XPathConstants; public class CacheConfig { private String nodeName; - private Class clazz; + private Class clazz; private Map args; private CacheRegenerator regenerator; @@ -51,7 +51,7 @@ public class CacheConfig { public CacheConfig() {} - public CacheConfig(Class clazz, Map args, CacheRegenerator regenerator) { + public CacheConfig(Class clazz, Map args, CacheRegenerator regenerator) { this.clazz = clazz; this.args = args; this.regenerator = regenerator; @@ -95,9 +95,9 @@ public class CacheConfig { SolrResourceLoader loader = solrConfig.getResourceLoader(); config.cacheImpl = config.args.get("class"); config.regenImpl = config.args.get("regenerator"); - config.clazz = loader.findClass(config.cacheImpl); + config.clazz = loader.findClass(config.cacheImpl, SolrCache.class); if (config.regenImpl != null) { - config.regenerator = (CacheRegenerator) loader.newInstance(config.regenImpl); + config.regenerator = loader.newInstance(config.regenImpl, CacheRegenerator.class); } return config; @@ -105,7 +105,7 @@ public class CacheConfig { public SolrCache newInstance() { try { - SolrCache cache = (SolrCache)clazz.newInstance(); + SolrCache cache = clazz.newInstance(); persistence[0] = cache.init(args, persistence[0], regenerator); return cache; } catch (Exception e) { diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java index d7051ed8483..9b35f28e072 100755 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -344,7 +344,7 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin { } dist = new NGramDistance(ngram); } else { - dist = (StringDistance) fp.req.getCore().getResourceLoader().newInstance(distClass); + dist = fp.req.getCore().getResourceLoader().newInstance(distClass, StringDistance.class); } return new StringDistanceFunction(str1, str2, dist); } diff --git a/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java index 91e7993aab0..445d83eabdf 100644 --- a/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/AbstractLuceneSpellChecker.java @@ -109,14 +109,14 @@ public abstract class AbstractLuceneSpellChecker extends SolrSpellChecker { } else if (compClass.equalsIgnoreCase(FREQ_COMP)){ comp = new SuggestWordFrequencyComparator(); } else{//must be a FQCN - comp = (Comparator) core.getResourceLoader().newInstance(compClass); + comp = (Comparator) core.getResourceLoader().newInstance(compClass, Comparator.class); } } else { comp = SuggestWordQueue.DEFAULT_COMPARATOR; } String strDistanceName = (String)config.get(STRING_DISTANCE); if (strDistanceName != null) { - sd = (StringDistance) core.getResourceLoader().newInstance(strDistanceName); + sd = core.getResourceLoader().newInstance(strDistanceName, StringDistance.class); //TODO: Figure out how to configure options. Where's Spring when you need it? Or at least BeanUtils... } else { sd = new LevensteinDistance(); diff --git a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java index f68b99a5fa1..a38b80cc672 100644 --- a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java +++ b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java @@ -110,13 +110,13 @@ public class DirectSolrSpellChecker extends SolrSpellChecker { else if (compClass.equalsIgnoreCase(FREQ_COMP)) comp = new SuggestWordFrequencyComparator(); else //must be a FQCN - comp = (Comparator) core.getResourceLoader().newInstance(compClass); + comp = (Comparator) core.getResourceLoader().newInstance(compClass, Comparator.class); } StringDistance sd = DirectSpellChecker.INTERNAL_LEVENSHTEIN; String distClass = (String) config.get(STRING_DISTANCE); if (distClass != null && !distClass.equalsIgnoreCase(INTERNAL_DISTANCE)) - sd = (StringDistance) core.getResourceLoader().newInstance(distClass); + sd = core.getResourceLoader().newInstance(distClass, StringDistance.class); float minAccuracy = DEFAULT_ACCURACY; Float accuracy = (Float) config.get(ACCURACY); diff --git a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java index 5286dd9d451..d15d437c983 100644 --- a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java +++ b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java @@ -96,7 +96,7 @@ public class Suggester extends SolrSpellChecker { lookupImpl = FSTLookupFactory.class.getName(); } - factory = (LookupFactory) core.getResourceLoader().newInstance(lookupImpl); + factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class); lookup = factory.create(config, core); String store = (String)config.get(STORE_DIR); diff --git a/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java b/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java index 8754f176e43..4f79f782bc8 100644 --- a/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java +++ b/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java @@ -180,7 +180,7 @@ public class SolrIndexConfig { private MergePolicy buildMergePolicy(IndexSchema schema) { String mpClassName = mergePolicyInfo == null ? defaultMergePolicyClassName : mergePolicyInfo.className; - MergePolicy policy = (MergePolicy) schema.getResourceLoader().newInstance(mpClassName); + MergePolicy policy = schema.getResourceLoader().newInstance(mpClassName, MergePolicy.class); if (policy instanceof LogMergePolicy) { LogMergePolicy logMergePolicy = (LogMergePolicy) policy; @@ -213,7 +213,7 @@ public class SolrIndexConfig { private MergeScheduler buildMergeScheduler(IndexSchema schema) { String msClassName = mergeSchedulerInfo == null ? SolrIndexConfig.DEFAULT_MERGE_SCHEDULER_CLASSNAME : mergeSchedulerInfo.className; - MergeScheduler scheduler = (MergeScheduler) schema.getResourceLoader().newInstance(msClassName); + MergeScheduler scheduler = schema.getResourceLoader().newInstance(msClassName, MergeScheduler.class); if (mergeSchedulerInfo != null) SolrPluginUtils.invokeSetters(scheduler, mergeSchedulerInfo.initArgs); diff --git a/solr/core/src/java/org/apache/solr/update/processor/FieldMutatingUpdateProcessor.java b/solr/core/src/java/org/apache/solr/update/processor/FieldMutatingUpdateProcessor.java index 1536d3281ea..a7f5e21c69c 100644 --- a/solr/core/src/java/org/apache/solr/update/processor/FieldMutatingUpdateProcessor.java +++ b/solr/core/src/java/org/apache/solr/update/processor/FieldMutatingUpdateProcessor.java @@ -205,7 +205,7 @@ public abstract class FieldMutatingUpdateProcessor for (String t : typeClasses) { try { - classes.add(loader.findClass(t)); + classes.add(loader.findClass(t, Object.class)); } catch (Exception e) { throw new SolrException(SERVER_ERROR, "Can't resolve typeClass: " + t, e); diff --git a/solr/core/src/java/org/apache/solr/update/processor/SignatureUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/SignatureUpdateProcessorFactory.java index 4c8ecdb2e47..d63e0cbfa3a 100755 --- a/solr/core/src/java/org/apache/solr/update/processor/SignatureUpdateProcessorFactory.java +++ b/solr/core/src/java/org/apache/solr/update/processor/SignatureUpdateProcessorFactory.java @@ -142,7 +142,7 @@ public class SignatureUpdateProcessorFactory currDocSigFields = sigFields; } - Signature sig = (Signature) req.getCore().getResourceLoader().newInstance(signatureClass); + Signature sig = req.getCore().getResourceLoader().newInstance(signatureClass, Signature.class); sig.init(params); for (String field : currDocSigFields) { diff --git a/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java b/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java index ec5bc60371b..c0eaaf7f289 100644 --- a/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java +++ b/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java @@ -43,21 +43,23 @@ public abstract class AbstractPluginLoader private final String type; private final boolean preRegister; private final boolean requireName; + private final Class pluginClassType; /** * @param type is the 'type' name included in error messages. * @param preRegister if true, this will first register all Plugins, then it will initialize them. */ - public AbstractPluginLoader( String type, boolean preRegister, boolean requireName ) + public AbstractPluginLoader(String type, Class pluginClassType, boolean preRegister, boolean requireName ) { this.type = type; + this.pluginClassType = pluginClassType; this.preRegister = preRegister; this.requireName = requireName; } - public AbstractPluginLoader( String type ) + public AbstractPluginLoader(String type, Class pluginClassType) { - this( type, false, true ); + this(type, pluginClassType, false, true); } /** @@ -81,7 +83,7 @@ public abstract class AbstractPluginLoader @SuppressWarnings("unchecked") protected T create( ResourceLoader loader, String name, String className, Node node ) throws Exception { - return (T) loader.newInstance( className, getDefaultPackages() ); + return loader.newInstance(className, pluginClassType, getDefaultPackages()); } /** diff --git a/solr/core/src/java/org/apache/solr/util/plugin/MapPluginLoader.java b/solr/core/src/java/org/apache/solr/util/plugin/MapPluginLoader.java index 77a5bcf0742..dbcd788df4c 100644 --- a/solr/core/src/java/org/apache/solr/util/plugin/MapPluginLoader.java +++ b/solr/core/src/java/org/apache/solr/util/plugin/MapPluginLoader.java @@ -31,9 +31,8 @@ public class MapPluginLoader extends AbstractPlu { private final Map registry; - public MapPluginLoader( String name, Map map ) - { - super( name ); + public MapPluginLoader(String name, Class pluginClassType, Map map) { + super(name, pluginClassType); registry = map; } diff --git a/solr/core/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.java b/solr/core/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.java index 6d03480859b..5d89a660cfe 100644 --- a/solr/core/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.java +++ b/solr/core/src/java/org/apache/solr/util/plugin/NamedListPluginLoader.java @@ -30,9 +30,8 @@ public class NamedListPluginLoader extends { private final Map registry; - public NamedListPluginLoader( String name, Map map ) - { - super( name ); + public NamedListPluginLoader(String name, Class pluginClassType, Map map) { + super(name, pluginClassType); registry = map; } diff --git a/solr/core/src/test/org/apache/solr/analysis/SnowballPorterFilterFactoryTest.java b/solr/core/src/test/org/apache/solr/analysis/SnowballPorterFilterFactoryTest.java index afc9647659e..0c6166edf08 100644 --- a/solr/core/src/test/org/apache/solr/analysis/SnowballPorterFilterFactoryTest.java +++ b/solr/core/src/test/org/apache/solr/analysis/SnowballPorterFilterFactoryTest.java @@ -69,7 +69,7 @@ public class SnowballPorterFilterFactoryTest extends BaseTokenTestCase { return lines; } - public Object newInstance(String cname, String... subpackages) { + public T newInstance(String cname, Class expectedType, String... subpackages) { return null; } diff --git a/solr/core/src/test/org/apache/solr/analysis/TestCollationKeyFilterFactory.java b/solr/core/src/test/org/apache/solr/analysis/TestCollationKeyFilterFactory.java index d6b629934dd..4004f192e9c 100644 --- a/solr/core/src/test/org/apache/solr/analysis/TestCollationKeyFilterFactory.java +++ b/solr/core/src/test/org/apache/solr/analysis/TestCollationKeyFilterFactory.java @@ -166,7 +166,7 @@ public class TestCollationKeyFilterFactory extends BaseTokenTestCase { return null; } - public Object newInstance(String cname, String... subpackages) { + public T newInstance(String cname, Class expectedType, String... subpackages) { return null; } diff --git a/solr/core/src/test/org/apache/solr/analysis/TestMultiWordSynonyms.java b/solr/core/src/test/org/apache/solr/analysis/TestMultiWordSynonyms.java index 3846ef2485d..f10a2901f31 100644 --- a/solr/core/src/test/org/apache/solr/analysis/TestMultiWordSynonyms.java +++ b/solr/core/src/test/org/apache/solr/analysis/TestMultiWordSynonyms.java @@ -73,7 +73,7 @@ public class TestMultiWordSynonyms extends BaseTokenTestCase { return null; } - public Object newInstance(String cname, String... subpackages) { + public T newInstance(String cname, Class expectedType, String... subpackages) { return null; } diff --git a/solr/core/src/test/org/apache/solr/analysis/TestSynonymFilterFactory.java b/solr/core/src/test/org/apache/solr/analysis/TestSynonymFilterFactory.java index 9838f179a03..4cf740c0510 100644 --- a/solr/core/src/test/org/apache/solr/analysis/TestSynonymFilterFactory.java +++ b/solr/core/src/test/org/apache/solr/analysis/TestSynonymFilterFactory.java @@ -108,7 +108,7 @@ public class TestSynonymFilterFactory extends BaseTokenTestCase { return Arrays.asList(text.split("\n")); } - public Object newInstance(String cname, String... subpackages) { + public T newInstance(String cname, Class expectedType, String... subpackages) { return null; } diff --git a/solr/core/src/test/org/apache/solr/analysis/TestSynonymMap.java b/solr/core/src/test/org/apache/solr/analysis/TestSynonymMap.java index 66b3a5c7743..19182f7f4c3 100644 --- a/solr/core/src/test/org/apache/solr/analysis/TestSynonymMap.java +++ b/solr/core/src/test/org/apache/solr/analysis/TestSynonymMap.java @@ -282,7 +282,7 @@ public class TestSynonymMap extends LuceneTestCase { } @Override - public Object newInstance(String cname, String... subpackages) { + public T newInstance(String cname, Class expectedType, String... subpackages) { throw new RuntimeException("stub"); } diff --git a/solr/solrj/src/java/org/apache/solr/common/ResourceLoader.java b/solr/solrj/src/java/org/apache/solr/common/ResourceLoader.java index 381d2617480..2c0b3b71dd7 100644 --- a/solr/solrj/src/java/org/apache/solr/common/ResourceLoader.java +++ b/solr/solrj/src/java/org/apache/solr/common/ResourceLoader.java @@ -1,4 +1,6 @@ -/** +package org.apache.solr.common; + +/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. @@ -15,8 +17,6 @@ * limitations under the License. */ -package org.apache.solr.common; - import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -43,5 +43,5 @@ public interface ResourceLoader */ public List getLines(String resource) throws IOException; - public Object newInstance(String cname, String ... subpackages); + public T newInstance(String cname, Class expectedType, String ... subpackages); } \ No newline at end of file diff --git a/solr/test-framework/src/java/org/apache/solr/analysis/StringMockSolrResourceLoader.java b/solr/test-framework/src/java/org/apache/solr/analysis/StringMockSolrResourceLoader.java index 85ce1247047..045d67bb5ef 100644 --- a/solr/test-framework/src/java/org/apache/solr/analysis/StringMockSolrResourceLoader.java +++ b/solr/test-framework/src/java/org/apache/solr/analysis/StringMockSolrResourceLoader.java @@ -36,7 +36,7 @@ class StringMockSolrResourceLoader implements ResourceLoader { return Arrays.asList(text.split("\n")); } - public Object newInstance(String cname, String... subpackages) { + public T newInstance(String cname, Class expectedType, String... subpackages) { return null; }