List of usage examples for org.apache.lucene.analysis.util TokenFilterFactory lookupClass
public static Class<? extends TokenFilterFactory> lookupClass(String name)
From source file:com.grantingersoll.opengrok.analysis.BaseTokenStreamFactoryTestCase.java
License:Apache License
/** * Returns a fully initialized TokenFilterFactory with the specified name, version, resource loader, * and key-value arguments./*from w ww .j a v a 2 s . c om*/ */ protected TokenFilterFactory tokenFilterFactory(String name, Version matchVersion, ResourceLoader loader, String... keysAndValues) throws Exception { return (TokenFilterFactory) analysisFactory(TokenFilterFactory.lookupClass(name), matchVersion, loader, keysAndValues); }
From source file:org.apache.solr.core.SolrResourceLoader.java
License:Apache License
/** * This method loads a class either with it's FQN or a short-name (solr.class-simplename or class-simplename). * It tries to load the class with the name that is given first and if it fails, it tries all the known * solr packages. This method caches the FQN of a short-name in a static map in-order to make subsequent lookups * for the same class faster. The caching is done only if the class is loaded by the webapp classloader and it * is loaded using a shortname./*from w w w. j a va 2 s .co m*/ * * @param cname The name or the short name of the class. * @param subpackages the packages to be tried if the cname starts with solr. * @return the loaded class. An exception is thrown if it fails */ public <T> Class<? extends T> findClass(String cname, Class<T> 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).asSubclass(expectedType); } catch (ClassNotFoundException e) { //this is unlikely log.error("Unable to load cached class-name : " + c + " for shortname : " + cname + e); } } } Class<? extends T> clazz = null; try { // first try legacy analysis patterns, now replaced by Lucene's Analysis package: final Matcher m = legacyAnalysisPattern.matcher(cname); if (m.matches()) { final String name = m.group(4); log.trace("Trying to load class from analysis SPI using name='{}'", name); try { if (CharFilterFactory.class.isAssignableFrom(expectedType)) { return clazz = CharFilterFactory.lookupClass(name).asSubclass(expectedType); } else if (TokenizerFactory.class.isAssignableFrom(expectedType)) { return clazz = TokenizerFactory.lookupClass(name).asSubclass(expectedType); } else if (TokenFilterFactory.class.isAssignableFrom(expectedType)) { return clazz = TokenFilterFactory.lookupClass(name).asSubclass(expectedType); } else { log.warn( "'{}' looks like an analysis factory, but caller requested different class type: {}", cname, expectedType.getName()); } } catch (IllegalArgumentException ex) { // ok, we fall back to legacy loading } } // first try cname == full name try { return clazz = Class.forName(cname, true, classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e) { String newName = cname; if (newName.startsWith(project)) { newName = cname.substring(project.length() + 1); } for (String subpackage : subpackages) { try { String name = base + '.' + subpackage + newName; log.trace("Trying class name " + name); return clazz = Class.forName(name, true, classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e1) { // ignore... assume first exception is best. } } throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error loading class '" + cname + "'", e); } } finally { if (clazz != null) { //cache the shortname vs FQN if it is loaded by the webapp classloader and it is loaded // using a shortname if (clazz.getClassLoader() == SolrResourceLoader.class.getClassLoader() && !cname.equals(clazz.getName()) && (subpackages.length == 0 || subpackages == packages)) { //store in the cache classNameCache.put(cname, clazz.getName()); } // print warning if class is deprecated if (clazz.isAnnotationPresent(Deprecated.class)) { log.warn( "Solr loaded a deprecated plugin/analysis class [{}]. Please consult documentation how to replace it accordingly.", cname); } } } }
From source file:org.apache.tika.eval.tokens.AnalyzerDeserializer.java
License:Apache License
private static TokenFilterFactory[] buildTokenFilterFactories(JsonElement el, String analyzerName) throws IOException { if (el == null || el.isJsonNull()) { return null; }/*from ww w.ja v a2 s . c om*/ if (!el.isJsonArray()) { throw new IllegalArgumentException( "Expecting array for tokenfilters, but got:" + el.toString() + " in " + analyzerName); } JsonArray jsonArray = (JsonArray) el; List<TokenFilterFactory> ret = new LinkedList<>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException( "Expecting a map with \"factory\" string and \"params\" map in token filter factory;" + " not: " + filterMap.toString() + " in " + analyzerName); } JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY); if (factoryEl == null || !factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in token filter factory builder in " + analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName; JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); String spiName = ""; for (String s : TokenFilterFactory.availableTokenFilters()) { Class clazz = TokenFilterFactory.lookupClass(s); if (clazz.getName().equals(factoryName)) { spiName = s; break; } } if (spiName.equals("")) { throw new IllegalArgumentException( "A SPI class of type org.apache.lucene.analysis.util.TokenFilterFactory with name" + "'" + factoryName + "' does not exist."); } try { TokenFilterFactory tokenFilterFactory = TokenFilterFactory.forName(spiName, params); if (tokenFilterFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokenFilterFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } ret.add(tokenFilterFactory); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While loading " + analyzerName, e); } } if (ret.size() == 0) { return new TokenFilterFactory[0]; } return ret.toArray(new TokenFilterFactory[ret.size()]); }
From source file:org.elasticsearch.indices.analysis.AnalysisFactoryTestCase.java
License:Apache License
public void testPreBuiltMultiTermAware() { Collection<Object> expected = new HashSet<>(); Collection<Object> actual = new HashSet<>(); Map<String, PreConfiguredTokenFilter> preConfiguredTokenFilters = new HashMap<>( AnalysisModule.setupPreConfiguredTokenFilters(singletonList(plugin))); for (Map.Entry<String, Class<?>> entry : getPreConfiguredTokenFilters().entrySet()) { String name = entry.getKey(); Class<?> luceneFactory = entry.getValue(); PreConfiguredTokenFilter filter = preConfiguredTokenFilters.remove(name); assertNotNull("test claims pre built token filter [" + name + "] should be available but it wasn't", filter);/* ww w. j a va 2 s . co m*/ if (luceneFactory == Void.class) { continue; } if (luceneFactory == null) { luceneFactory = TokenFilterFactory.lookupClass(toCamelCase(name)); } assertThat(luceneFactory, typeCompatibleWith(TokenFilterFactory.class)); if (filter.shouldUseFilterForMultitermQueries()) { actual.add("token filter [" + name + "]"); } if (org.apache.lucene.analysis.util.MultiTermAwareComponent.class.isAssignableFrom(luceneFactory)) { expected.add("token filter [" + name + "]"); } } assertThat("pre configured token filter not registered with test", preConfiguredTokenFilters.keySet(), empty()); Map<String, PreConfiguredTokenizer> preConfiguredTokenizers = new HashMap<>( AnalysisModule.setupPreConfiguredTokenizers(singletonList(plugin))); for (Map.Entry<String, Class<?>> entry : getPreConfiguredTokenizers().entrySet()) { String name = entry.getKey(); Class<?> luceneFactory = entry.getValue(); PreConfiguredTokenizer tokenizer = preConfiguredTokenizers.remove(name); assertNotNull("test claims pre built tokenizer [" + name + "] should be available but it wasn't", tokenizer); if (luceneFactory == Void.class) { continue; } if (luceneFactory == null) { luceneFactory = TokenizerFactory.lookupClass(toCamelCase(name)); } assertThat(luceneFactory, typeCompatibleWith(TokenizerFactory.class)); if (tokenizer.hasMultiTermComponent()) { actual.add(tokenizer); } if (org.apache.lucene.analysis.util.MultiTermAwareComponent.class.isAssignableFrom(luceneFactory)) { expected.add(tokenizer); } } assertThat("pre configured tokenizer not registered with test", preConfiguredTokenizers.keySet(), empty()); Map<String, PreConfiguredCharFilter> preConfiguredCharFilters = new HashMap<>( AnalysisModule.setupPreConfiguredCharFilters(singletonList(plugin))); for (Map.Entry<String, Class<?>> entry : getPreConfiguredCharFilters().entrySet()) { String name = entry.getKey(); Class<?> luceneFactory = entry.getValue(); PreConfiguredCharFilter filter = preConfiguredCharFilters.remove(name); assertNotNull("test claims pre built char filter [" + name + "] should be available but it wasn't", filter); if (luceneFactory == Void.class) { continue; } if (luceneFactory == null) { luceneFactory = TokenFilterFactory.lookupClass(toCamelCase(name)); } assertThat(luceneFactory, typeCompatibleWith(CharFilterFactory.class)); if (filter.shouldUseFilterForMultitermQueries()) { actual.add(filter); } if (org.apache.lucene.analysis.util.MultiTermAwareComponent.class.isAssignableFrom(luceneFactory)) { expected.add("token filter [" + name + "]"); } } assertThat("pre configured char filter not registered with test", preConfiguredCharFilters.keySet(), empty()); Set<Object> classesMissingMultiTermSupport = new HashSet<>(expected); classesMissingMultiTermSupport.removeAll(actual); assertTrue("Pre-built components are missing multi-term support: " + classesMissingMultiTermSupport, classesMissingMultiTermSupport.isEmpty()); Set<Object> classesThatShouldNotHaveMultiTermSupport = new HashSet<>(actual); classesThatShouldNotHaveMultiTermSupport.removeAll(expected); assertTrue( "Pre-built components should not have multi-term support: " + classesThatShouldNotHaveMultiTermSupport, classesThatShouldNotHaveMultiTermSupport.isEmpty()); }
From source file:org.liveSense.service.solr.api.OverridedSolrResourceLoader.java
License:Apache License
/** * This method loads a class either with it's FQN or a short-name (solr.class-simplename or class-simplename). * It tries to load the class with the name that is given first and if it fails, it tries all the known * solr packages. This method caches the FQN of a short-name in a static map in-order to make subsequent lookups * for the same class faster. The caching is done only if the class is loaded by the webapp classloader and it * is loaded using a shortname.//from w ww .jav a 2 s . c o m * * @param cname The name or the short name of the class. * @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 <T> Class<? extends T> findClass(String cname, Class<T> 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).asSubclass(expectedType); } catch (ClassNotFoundException e) { //this is unlikely log.error("Unable to load cached class-name : " + c + " for shortname : " + cname + e); } } } Class<? extends T> clazz = null; // first try legacy analysis patterns, now replaced by Lucene's Analysis package: final Matcher m = legacyAnalysisPattern.matcher(cname); if (m.matches()) { final String name = m.group(4); log.trace("Trying to load class from analysis SPI using name='{}'", name); try { if (CharFilterFactory.class.isAssignableFrom(expectedType)) { return clazz = CharFilterFactory.lookupClass(name).asSubclass(expectedType); } else if (TokenizerFactory.class.isAssignableFrom(expectedType)) { return clazz = TokenizerFactory.lookupClass(name).asSubclass(expectedType); } else if (TokenFilterFactory.class.isAssignableFrom(expectedType)) { return clazz = TokenFilterFactory.lookupClass(name).asSubclass(expectedType); } else { log.warn("'{}' looks like an analysis factory, but caller requested different class type: {}", cname, expectedType.getName()); } } catch (IllegalArgumentException ex) { // ok, we fall back to legacy loading } } // first try cname == full name try { return Class.forName(cname, true, classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e) { String newName = cname; if (newName.startsWith(project)) { newName = cname.substring(project.length() + 1); } for (String subpackage : subpackages) { try { String name = base + '.' + subpackage + newName; log.trace("Trying class name " + name); return clazz = Class.forName(name, true, classLoader).asSubclass(expectedType); } catch (ClassNotFoundException e1) { // ignore... assume first exception is best. } } throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error loading class '" + cname + "'", e); } finally { //cache the shortname vs FQN if it is loaded by the webapp classloader and it is loaded // using a shortname if (clazz != null && clazz.getClassLoader() == OverridedSolrResourceLoader.class.getClassLoader() && !cname.equals(clazz.getName()) && (subpackages.length == 0 || subpackages == packages)) { //store in the cache classNameCache.put(cname, clazz.getName()); } } }
From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java
License:Apache License
private static TokenFilterFactory[] buildTokenFilterFactories(JsonElement el, String analyzerName) throws IOException { if (el == null || el.isJsonNull()) { return null; }/*from w ww . j a v a 2s . c o m*/ if (!el.isJsonArray()) { throw new IllegalArgumentException( "Expecting array for tokenfilters, but got:" + el.toString() + " in " + analyzerName); } JsonArray jsonArray = (JsonArray) el; List<TokenFilterFactory> ret = new LinkedList<>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException( "Expecting a map with \"factory\" string and \"params\" map in token filter factory;" + " not: " + filterMap.toString() + " in " + analyzerName); } JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY); if (factoryEl == null || !factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in token filter factory builder in " + analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName; JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); String spiName = ""; for (String s : TokenFilterFactory.availableTokenFilters()) { Class clazz = TokenFilterFactory.lookupClass(s); if (clazz.getName().equals(factoryName)) { spiName = s; break; } } try { TokenFilterFactory tokenFilterFactory = TokenFilterFactory.forName(spiName, params); if (tokenFilterFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokenFilterFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } ret.add(tokenFilterFactory); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While loading " + analyzerName, e); } } if (ret.size() == 0) { return new TokenFilterFactory[0]; } return ret.toArray(new TokenFilterFactory[ret.size()]); }