From ca69c478663010ace9dacefa3c4179de3227b0aa Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Tue, 24 Jul 2012 19:24:46 +0000 Subject: [PATCH] LUCENE-2510: Improve the Maven-like pattern detection for the named loader. TODO: We should create an ant task that creates the META-INF/services list from filesets git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene2510@1365239 13f79535-47bb-0310-9956-ffa450edef68 --- .../analysis/util/AnalysisSPILoader.java | 47 ++++--------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java index 5b2e8f50d07..95fcf5b4002 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java @@ -17,12 +17,14 @@ package org.apache.lucene.analysis.util; * limitations under the License. */ +import java.util.Arrays; import java.util.Collections; import java.util.Locale; import java.util.Map; import java.util.LinkedHashMap; import java.util.Set; -import org.apache.lucene.util.NamedSPILoader.NamedSPI; // javadocs +import java.util.ServiceConfigurationError; + import org.apache.lucene.util.SPIClassIterator; /** @@ -53,56 +55,27 @@ public final class AnalysisSPILoader { while (loader.hasNext()) { final Class service = loader.next(); final String clazzName = service.getSimpleName(); - int suffixIndex = -1; + String name = null; for (String suffix : suffixes) { - suffixIndex = clazzName.lastIndexOf(suffix); - if (suffixIndex != -1) { + if (clazzName.endsWith(suffix)) { + name = clazzName.substring(0, clazzName.length() - suffix.length()).toLowerCase(Locale.ROOT); break; } } - final String name = clazzName.substring(0, suffixIndex).toLowerCase(Locale.ROOT); + if (name == null) { + throw new ServiceConfigurationError("The class name " + service.getName() + + " has wrong suffix, allowed are: " + Arrays.toString(suffixes)); + } // only add the first one for each name, later services will be ignored // this allows to place services before others in classpath to make // them used instead of others if (!services.containsKey(name)) { - assert checkServiceName(name); services.put(name, service); } } this.services = Collections.unmodifiableMap(services); } - /** - * Validates that a service name meets the requirements of {@link NamedSPI} - */ - public static boolean checkServiceName(String name) { - // based on harmony charset.java - if (name.length() >= 128) { - throw new IllegalArgumentException("Illegal service name: '" + name + "' is too long (must be < 128 chars)."); - } - for (int i = 0; i < name.length(); i++) { - char c = name.charAt(i); - if (!isLetter(c) && !isDigit(c)) { - throw new IllegalArgumentException("Illegal service name: '" + name + "' must be simple ascii alphanumeric."); - } - } - return true; - } - - /* - * Checks whether a character is a letter (ascii) which are defined in the spec. - */ - private static boolean isLetter(char c) { - return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); - } - - /* - * Checks whether a character is a digit (ascii) which are defined in the spec. - */ - private static boolean isDigit(char c) { - return ('0' <= c && c <= '9'); - } - // TODO: do we even need this method? public S newInstance(String name) { final Class service = lookupClass(name);