SOLR-1677: Code cleanup (heavy duplication removal) and more checks.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/branches/solr@923109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2010-03-15 08:09:34 +00:00
parent ae8d108b7d
commit 9ad4aae24d
5 changed files with 105 additions and 83 deletions

View File

@ -17,71 +17,14 @@
package org.apache.solr.analysis; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.apache.lucene.util.Version;
/** /**
* Simple abstract implementation that handles init arg processing. * Simple abstract implementation that handles init arg processing.
* *
* @version $Id$ * @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); public static final Logger log = LoggerFactory.getLogger(BaseTokenFilterFactory.class);
/** The init args */
protected Map<String,String> args;
/** the luceneVersion arg */
protected Version luceneMatchVersion = null;
public void init(Map<String,String> args) {
this.args=args;
String matchVersion = args.get(IndexSchema.LUCENE_MATCH_VERSION_PARAM);
if (matchVersion != null) {
luceneMatchVersion = Config.parseLuceneVersionString(matchVersion);
}
}
public Map<String,String> 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);
}
} }

View File

@ -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<String,String> args;
/** the luceneVersion arg */
protected Version luceneMatchVersion = null;
public void init(Map<String,String> args) {
this.args=args;
String matchVersion = args.get(IndexSchema.LUCENE_MATCH_VERSION_PARAM);
if (matchVersion != null) {
luceneMatchVersion = Config.parseLuceneVersionString(matchVersion);
}
}
public Map<String,String> 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);
}
}

View File

@ -17,38 +17,14 @@
package org.apache.solr.analysis; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.apache.lucene.util.Version;
/** /**
* Simple abstract implementation that handles init arg processing. * Simple abstract implementation that handles init arg processing.
* *
* @version $Id$ * @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); public static final Logger log = LoggerFactory.getLogger(BaseTokenizerFactory.class);
/** The init args */
protected Map<String,String> args;
/** the luceneVersion arg */
protected Version luceneMatchVersion = null;
public void init(Map<String,String> args) {
this.args=args;
String matchVersion = args.get(IndexSchema.LUCENE_MATCH_VERSION_PARAM);
if (matchVersion != null) {
luceneMatchVersion = Config.parseLuceneVersionString(matchVersion);
}
}
public Map<String,String> getArgs() {
return args;
}
} }

View File

@ -28,6 +28,7 @@ import java.io.Reader;
public class StandardTokenizerFactory extends BaseTokenizerFactory { public class StandardTokenizerFactory extends BaseTokenizerFactory {
public StandardTokenizer create(Reader input) { public StandardTokenizer create(Reader input) {
assureMatchVersion();
return new StandardTokenizer(luceneMatchVersion, input); return new StandardTokenizer(luceneMatchVersion, input);
} }
} }

View File

@ -830,6 +830,11 @@ public final class IndexSchema {
final String matchVersionStr = DOMUtil.getAttr(attrs, LUCENE_MATCH_VERSION_PARAM); final String matchVersionStr = DOMUtil.getAttr(attrs, LUCENE_MATCH_VERSION_PARAM);
final Version luceneMatchVersion = (matchVersionStr == null) ? final Version luceneMatchVersion = (matchVersionStr == null) ?
solrConfig.luceneMatchVersion : Config.parseLuceneVersionString(matchVersionStr); 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); return cnstr.newInstance(luceneMatchVersion);
} catch (NoSuchMethodException nsme) { } catch (NoSuchMethodException nsme) {
// otherwise use default ctor // otherwise use default ctor