001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.config;
020:
021: import java.io.IOException;
022:
023: import org.apache.jmeter.config.ConfigTestElement;
024: import org.apache.jmeter.engine.event.LoopIterationEvent;
025: import org.apache.jmeter.engine.event.LoopIterationListener;
026: import org.apache.jmeter.services.FileServer;
027: import org.apache.jmeter.testbeans.TestBean;
028: import org.apache.jmeter.threads.JMeterVariables;
029: import org.apache.jmeter.util.JMeterUtils;
030: import org.apache.jorphan.logging.LoggingManager;
031: import org.apache.jorphan.util.JMeterStopThreadException;
032: import org.apache.jorphan.util.JOrphanUtils;
033: import org.apache.log.Logger;
034:
035: /**
036: * @author mstover
037: *
038: */
039: public class CSVDataSet extends ConfigTestElement implements TestBean,
040: LoopIterationListener {
041: private static final Logger log = LoggingManager
042: .getLoggerForClass();
043:
044: private static final long serialVersionUID = 2;
045:
046: private static final String EOFVALUE = // value to return at EOF
047: JMeterUtils.getPropDefault("csvdataset.eofstring", "<EOF>"); //$NON-NLS-1$ //$NON-NLS-2$
048:
049: private transient String filename;
050:
051: private transient String fileEncoding;
052:
053: private transient String variableNames;
054:
055: private transient String delimiter;
056:
057: private transient boolean recycle = true;
058:
059: private transient boolean stopThread = false;
060:
061: transient private String[] vars;
062:
063: private Object readResolve() {
064: recycle = true;
065: return this ;
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.apache.jmeter.engine.event.LoopIterationListener#iterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
072: */
073: public void iterationStart(LoopIterationEvent iterEvent) {
074: FileServer server = FileServer.getFileServer();
075: String _fileName = getFilename();
076: if (vars == null) {
077: server.reserveFile(_fileName, getFileEncoding());
078: vars = JOrphanUtils.split(getVariableNames(), ","); // $NON-NLS-1$
079: }
080: try {
081: String delim = getDelimiter();
082: if (delim.equals("\\t")) // $NON-NLS-1$
083: delim = "\t";// Make it easier to enter a Tab // $NON-NLS-1$
084: JMeterVariables threadVars = this .getThreadContext()
085: .getVariables();
086: String line = server.readLine(_fileName, getRecycle());
087: if (line != null) {// i.e. not EOF
088: String[] lineValues = JOrphanUtils.split(line, delim,
089: false);
090: for (int a = 0; a < vars.length
091: && a < lineValues.length; a++) {
092: threadVars.put(vars[a], lineValues[a]);
093: }
094: // TODO - provide option to set unused variables ?
095: } else {
096: if (getStopThread()) {
097: throw new JMeterStopThreadException(
098: "End of file detected");
099: }
100: for (int a = 0; a < vars.length; a++) {
101: threadVars.put(vars[a], EOFVALUE);
102: }
103: }
104: } catch (IOException e) {
105: log.error(e.toString());
106: }
107: }
108:
109: /**
110: * @return Returns the filename.
111: */
112: public String getFilename() {
113: return filename;
114: }
115:
116: /**
117: * @param filename
118: * The filename to set.
119: */
120: public void setFilename(String filename) {
121: this .filename = filename;
122: }
123:
124: /**
125: * @return Returns the file encoding.
126: */
127: public String getFileEncoding() {
128: return fileEncoding;
129: }
130:
131: /**
132: * @param fileEncoding
133: * The fileEncoding to set.
134: */
135: public void setFileEncoding(String fileEncoding) {
136: this .fileEncoding = fileEncoding;
137: }
138:
139: /**
140: * @return Returns the variableNames.
141: */
142: public String getVariableNames() {
143: return variableNames;
144: }
145:
146: /**
147: * @param variableNames
148: * The variableNames to set.
149: */
150: public void setVariableNames(String variableNames) {
151: this .variableNames = variableNames;
152: }
153:
154: public String getDelimiter() {
155: return delimiter;
156: }
157:
158: public void setDelimiter(String delimiter) {
159: this .delimiter = delimiter;
160: }
161:
162: public boolean getRecycle() {
163: return recycle;
164: }
165:
166: public void setRecycle(boolean recycle) {
167: this .recycle = recycle;
168: }
169:
170: public boolean getStopThread() {
171: return stopThread;
172: }
173:
174: public void setStopThread(boolean value) {
175: this.stopThread = value;
176: }
177:
178: }
|