diff --git a/src/java/org/apache/solr/analysis/BaseTokenFilterFactory.java b/src/java/org/apache/solr/analysis/BaseTokenFilterFactory.java index 6d7c918c161..90ad366c33b 100644 --- a/src/java/org/apache/solr/analysis/BaseTokenFilterFactory.java +++ b/src/java/org/apache/solr/analysis/BaseTokenFilterFactory.java @@ -17,71 +17,14 @@ package org.apache.solr.analysis; -import org.apache.solr.core.Config; -import org.apache.solr.schema.IndexSchema; - -import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.lucene.util.Version; - /** * Simple abstract implementation that handles init arg processing. * * @version $Id$ */ -public abstract class BaseTokenFilterFactory implements TokenFilterFactory { +public abstract class BaseTokenFilterFactory extends BaseTokenStreamFactory implements TokenFilterFactory { public static final Logger log = LoggerFactory.getLogger(BaseTokenFilterFactory.class); - - /** The init args */ - protected Map args; - - /** the luceneVersion arg */ - protected Version luceneMatchVersion = null; - - public void init(Map args) { - this.args=args; - String matchVersion = args.get(IndexSchema.LUCENE_MATCH_VERSION_PARAM); - if (matchVersion != null) { - luceneMatchVersion = Config.parseLuceneVersionString(matchVersion); - } - } - - public Map getArgs() { - return args; - } - - // TODO: move these somewhere that tokenizers and others - // can also use them... - protected int getInt(String name) { - return getInt(name,-1,false); - } - - protected int getInt(String name, int defaultVal) { - return getInt(name,defaultVal,true); - } - - protected int getInt(String name, int defaultVal, boolean useDefault) { - String s = args.get(name); - if (s==null) { - if (useDefault) return defaultVal; - throw new RuntimeException("Configuration Error: missing parameter '" + name + "'"); - } - return Integer.parseInt(s); - } - - protected boolean getBoolean(String name, boolean defaultVal) { - return getBoolean(name,defaultVal,true); - } - - protected boolean getBoolean(String name, boolean defaultVal, boolean useDefault) { - String s = args.get(name); - if (s==null) { - if (useDefault) return defaultVal; - throw new RuntimeException("Configuration Error: missing parameter '" + name + "'"); - } - return Boolean.parseBoolean(s); - } - } diff --git a/src/java/org/apache/solr/analysis/BaseTokenStreamFactory.java b/src/java/org/apache/solr/analysis/BaseTokenStreamFactory.java new file mode 100644 index 00000000000..21e9e4f5f9d --- /dev/null +++ b/src/java/org/apache/solr/analysis/BaseTokenStreamFactory.java @@ -0,0 +1,97 @@ +/** + * 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. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.analysis; + +import org.apache.solr.core.Config; +import org.apache.solr.common.SolrException; +import org.apache.solr.schema.IndexSchema; + +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.lucene.util.Version; + + +/** + * Simple abstract implementation that handles init arg processing, is not really + * a factory as it implements no interface, but removes code duplication + * in its subclasses. + * + * @version $Id$ + */ +abstract class BaseTokenStreamFactory { + /** The init args */ + protected Map args; + + /** the luceneVersion arg */ + protected Version luceneMatchVersion = null; + + public void init(Map args) { + this.args=args; + String matchVersion = args.get(IndexSchema.LUCENE_MATCH_VERSION_PARAM); + if (matchVersion != null) { + luceneMatchVersion = Config.parseLuceneVersionString(matchVersion); + } + } + + public Map getArgs() { + return args; + } + + /** this method can be called in the {@link #create} method, + * to inform user, that for this factory a {@link #luceneMatchVersion} is required */ + protected final void assureMatchVersion() { + if (luceneMatchVersion == null) { + throw new RuntimeException("Configuration Error: Factory '" + this.getClass().getName() + + "' needs a 'luceneMatchVersion' parameter"); + } + } + + // TODO: move these somewhere that tokenizers and others + // can also use them... + protected int getInt(String name) { + return getInt(name,-1,false); + } + + protected int getInt(String name, int defaultVal) { + return getInt(name,defaultVal,true); + } + + protected int getInt(String name, int defaultVal, boolean useDefault) { + String s = args.get(name); + if (s==null) { + if (useDefault) return defaultVal; + throw new RuntimeException("Configuration Error: missing parameter '" + name + "'"); + } + return Integer.parseInt(s); + } + + protected boolean getBoolean(String name, boolean defaultVal) { + return getBoolean(name,defaultVal,true); + } + + protected boolean getBoolean(String name, boolean defaultVal, boolean useDefault) { + String s = args.get(name); + if (s==null) { + if (useDefault) return defaultVal; + throw new RuntimeException("Configuration Error: missing parameter '" + name + "'"); + } + return Boolean.parseBoolean(s); + } + +} diff --git a/src/java/org/apache/solr/analysis/BaseTokenizerFactory.java b/src/java/org/apache/solr/analysis/BaseTokenizerFactory.java index 6fbcf78c704..5e4666377a2 100644 --- a/src/java/org/apache/solr/analysis/BaseTokenizerFactory.java +++ b/src/java/org/apache/solr/analysis/BaseTokenizerFactory.java @@ -17,38 +17,14 @@ package org.apache.solr.analysis; -import org.apache.solr.core.Config; -import org.apache.solr.schema.IndexSchema; - -import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.lucene.util.Version; - /** * Simple abstract implementation that handles init arg processing. * * @version $Id$ */ -public abstract class BaseTokenizerFactory implements TokenizerFactory { +public abstract class BaseTokenizerFactory extends BaseTokenStreamFactory implements TokenizerFactory { public static final Logger log = LoggerFactory.getLogger(BaseTokenizerFactory.class); - - /** The init args */ - protected Map args; - - /** the luceneVersion arg */ - protected Version luceneMatchVersion = null; - - public void init(Map args) { - this.args=args; - String matchVersion = args.get(IndexSchema.LUCENE_MATCH_VERSION_PARAM); - if (matchVersion != null) { - luceneMatchVersion = Config.parseLuceneVersionString(matchVersion); - } - } - - public Map getArgs() { - return args; - } } diff --git a/src/java/org/apache/solr/analysis/StandardTokenizerFactory.java b/src/java/org/apache/solr/analysis/StandardTokenizerFactory.java index cd90d90da0e..f59924d6704 100644 --- a/src/java/org/apache/solr/analysis/StandardTokenizerFactory.java +++ b/src/java/org/apache/solr/analysis/StandardTokenizerFactory.java @@ -28,6 +28,7 @@ import java.io.Reader; public class StandardTokenizerFactory extends BaseTokenizerFactory { public StandardTokenizer create(Reader input) { + assureMatchVersion(); return new StandardTokenizer(luceneMatchVersion, input); } } diff --git a/src/java/org/apache/solr/schema/IndexSchema.java b/src/java/org/apache/solr/schema/IndexSchema.java index e43f92b92b0..4661538b242 100644 --- a/src/java/org/apache/solr/schema/IndexSchema.java +++ b/src/java/org/apache/solr/schema/IndexSchema.java @@ -830,6 +830,11 @@ public final class IndexSchema { final String matchVersionStr = DOMUtil.getAttr(attrs, LUCENE_MATCH_VERSION_PARAM); final Version luceneMatchVersion = (matchVersionStr == null) ? solrConfig.luceneMatchVersion : Config.parseLuceneVersionString(matchVersionStr); + if (luceneMatchVersion == null) { + throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, + "Configuration Error: Analyzer '" + clazz.getName() + + "' needs a 'luceneMatchVersion' parameter"); + } return cnstr.newInstance(luceneMatchVersion); } catch (NoSuchMethodException nsme) { // otherwise use default ctor