01: package org.apache.lucene.benchmark.byTask.tasks;
02:
03: /**
04: * Copyright 2005 The Apache Software Foundation
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import org.apache.lucene.analysis.Analyzer;
20: import org.apache.lucene.benchmark.byTask.PerfRunData;
21:
22: import java.io.IOException;
23: import java.util.ArrayList;
24: import java.util.List;
25: import java.util.StringTokenizer;
26:
27: /**
28: * Create a new {@link org.apache.lucene.analysis.Analyzer} and set it it in the getRunData() for use by all future tasks.
29: *
30: */
31: public class NewAnalyzerTask extends PerfTask {
32: private List/*<String>*/analyzerClassNames;
33: private int current;
34:
35: public NewAnalyzerTask(PerfRunData runData) {
36: super (runData);
37: analyzerClassNames = new ArrayList();
38: }
39:
40: public int doLogic() throws IOException {
41: String className = null;
42: try {
43: if (current >= analyzerClassNames.size()) {
44: current = 0;
45: }
46: className = (String) analyzerClassNames.get(current++);
47: if (className == null || className.equals("")) {
48: className = "org.apache.lucene.analysis.standard.StandardAnalyzer";
49: }
50: if (className.indexOf(".") == -1
51: || className.startsWith("standard."))//there is no package name, assume o.a.l.analysis
52: {
53: className = "org.apache.lucene.analysis." + className;
54: }
55: getRunData().setAnalyzer(
56: (Analyzer) Class.forName(className).newInstance());
57: System.out.println("Changed Analyzer to: " + className);
58: } catch (Exception e) {
59: throw new RuntimeException("Error creating Analyzer: "
60: + className, e);
61: }
62: return 1;
63: }
64:
65: /**
66: * Set the params (analyzerClassName only), Comma-separate list of Analyzer class names. If the Analyzer lives in
67: * org.apache.lucene.analysis, the name can be shortened by dropping the o.a.l.a part of the Fully Qualified Class Name.
68: * <p/>
69: * Example Declaration: {"NewAnalyzer" NewAnalyzer(WhitespaceAnalyzer, SimpleAnalyzer, StopAnalyzer, standard.StandardAnalyzer) >
70: * @param params analyzerClassName, or empty for the StandardAnalyzer
71: */
72: public void setParams(String params) {
73: super .setParams(params);
74: for (StringTokenizer tokenizer = new StringTokenizer(params,
75: ","); tokenizer.hasMoreTokens();) {
76: String s = tokenizer.nextToken();
77: analyzerClassNames.add(s.trim());
78: }
79: }
80:
81: /* (non-Javadoc)
82: * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
83: */
84: public boolean supportsParams() {
85: return true;
86: }
87: }
|