1 package org.apache.lucene.benchmark.byTask.tasks;
3 * Copyright 2005 The Apache Software Foundation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import org.apache.lucene.analysis.Analyzer;
19 import org.apache.lucene.benchmark.byTask.PerfRunData;
20 import org.apache.lucene.util.Version;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.StringTokenizer;
26 import java.lang.reflect.Constructor;
29 * Create a new {@link org.apache.lucene.analysis.Analyzer} and set it it in the getRunData() for use by all future tasks.
32 public class NewAnalyzerTask extends PerfTask {
33 private List<String> analyzerClassNames;
36 public NewAnalyzerTask(PerfRunData runData) {
38 analyzerClassNames = new ArrayList<String>();
41 public static final Analyzer createAnalyzer(String className) throws Exception{
42 final Class<? extends Analyzer> clazz = Class.forName(className).asSubclass(Analyzer.class);
44 // first try to use a ctor with version parameter (needed for many new Analyzers that have no default one anymore
45 Constructor<? extends Analyzer> cnstr = clazz.getConstructor(Version.class);
46 return cnstr.newInstance(Version.LUCENE_CURRENT);
47 } catch (NoSuchMethodException nsme) {
48 // otherwise use default ctor
49 return clazz.newInstance();
54 public int doLogic() throws IOException {
55 String className = null;
57 if (current >= analyzerClassNames.size())
61 className = analyzerClassNames.get(current++);
62 if (className == null || className.equals(""))
64 className = "org.apache.lucene.analysis.standard.StandardAnalyzer";
66 if (className.indexOf(".") == -1 || className.startsWith("standard."))//there is no package name, assume o.a.l.analysis
68 className = "org.apache.lucene.analysis." + className;
70 getRunData().setAnalyzer(createAnalyzer(className));
71 System.out.println("Changed Analyzer to: " + className);
72 } catch (Exception e) {
73 throw new RuntimeException("Error creating Analyzer: " + className, e);
79 * Set the params (analyzerClassName only), Comma-separate list of Analyzer class names. If the Analyzer lives in
80 * org.apache.lucene.analysis, the name can be shortened by dropping the o.a.l.a part of the Fully Qualified Class Name.
82 * Example Declaration: {"NewAnalyzer" NewAnalyzer(WhitespaceAnalyzer, SimpleAnalyzer, StopAnalyzer, standard.StandardAnalyzer) >
83 * @param params analyzerClassName, or empty for the StandardAnalyzer
86 public void setParams(String params) {
87 super.setParams(params);
88 for (StringTokenizer tokenizer = new StringTokenizer(params, ","); tokenizer.hasMoreTokens();) {
89 String s = tokenizer.nextToken();
90 analyzerClassNames.add(s.trim());
95 * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
98 public boolean supportsParams() {