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.protocol.http.util.accesslog;
020:
021: import java.io.IOException;
022:
023: import org.apache.jmeter.services.FileServer;
024: import org.apache.jmeter.testelement.TestCloneable;
025: import org.apache.jmeter.testelement.TestElement;
026:
027: public class SharedTCLogParser extends TCLogParser implements
028: TestCloneable {
029:
030: public SharedTCLogParser() {
031: super ();
032: }
033:
034: public SharedTCLogParser(String source) {
035: super (source);
036: }
037:
038: /* (non-Javadoc)
039: * @see java.lang.Object#clone()
040: */
041: public Object clone() {
042: SharedTCLogParser parser = new SharedTCLogParser();
043: parser.FILENAME = FILENAME;
044: parser.FILTER = FILTER;
045: return parser;
046: }
047:
048: /* (non-Javadoc)
049: * @see org.apache.jmeter.protocol.http.util.accesslog.TCLogParser#parse(org.apache.jmeter.testelement.TestElement, int)
050: */
051: public int parse(TestElement el, int parseCount) {
052: FileServer fileServer = FileServer.getFileServer();
053: fileServer.reserveFile(FILENAME);
054: try {
055: return parse(fileServer, el, parseCount);
056: } catch (Exception exception) {
057: log.error("Problem creating samples", exception);
058: }
059: return -1;// indicate that an error occured
060: }
061:
062: /**
063: * The method is responsible for reading each line, and breaking out of the
064: * while loop if a set number of lines is given.
065: *
066: * @param breader
067: */
068: protected int parse(FileServer breader, TestElement el,
069: int parseCount) {
070: int actualCount = 0;
071: String line = null;
072: try {
073: // read one line at a time using
074: // BufferedReader
075: line = breader.readLine(FILENAME);
076: while (line != null) {
077: if (line.length() > 0) {
078: actualCount += this .parseLine(line, el);
079: }
080: // we check the count to see if we have exceeded
081: // the number of lines to parse. There's no way
082: // to know where to stop in the file. Therefore
083: // we use break to escape the while loop when
084: // we've reached the count.
085: if (parseCount != -1 && actualCount >= parseCount) {
086: break;
087: }
088: line = breader.readLine(FILENAME);
089: }
090: if (line == null) {
091: breader.closeFile(FILENAME);
092: // this.READER = new BufferedReader(new
093: // FileReader(this.SOURCE));
094: // parse(this.READER,el);
095: }
096: } catch (IOException ioe) {
097: log.error("Error reading log file", ioe);
098: }
099: return actualCount;
100: }
101:
102: public void close() {
103: try {
104: FileServer.getFileServer().closeFile(FILENAME);
105: } catch (IOException e) {
106: // do nothing
107: }
108: }
109:
110: }
|