<commit_before>/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed 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.jmeter.reporters;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.engine.util.NoThreadClone;
import org.apache.jmeter.samplers.Clearable;
import org.apache.jmeter.samplers.Remoteable;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.OldSaveService;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.save.TestResultWrapper;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.ObjectProperty;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.xml.sax.SAXException;

/**
 */
public class ResultCollector extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
		TestListener, Remoteable, NoThreadClone {
	private static final Logger log = LoggingManager.getLoggerForClass();

	private static final long serialVersionUID = 2;

	private static final String TESTRESULTS_START = "<testResults>"; // $NON-NLS-1$

	private static final String TESTRESULTS_START_V1_1_PREVER = "<testResults version=\"";  // $NON-NLS-1$
        private static final String TESTRESULTS_START_V1_1_POSTVER="\">"; // $NON-NLS-1$

	private static final String TESTRESULTS_END = "</testResults>"; // $NON-NLS-1$

	private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; // $NON-NLS-1$

	private static final int MIN_XML_FILE_LEN = XML_HEADER.length() + TESTRESULTS_START.length()
			+ TESTRESULTS_END.length();

	public final static String FILENAME = "filename"; // $NON-NLS-1$

	private final static String SAVE_CONFIG = "saveConfig"; // $NON-NLS-1$

	private static final String ERROR_LOGGING = "ResultCollector.error_logging"; // $NON-NLS-1$

	// protected List results = Collections.synchronizedList(new ArrayList());
	// private int current;
	transient private DefaultConfigurationSerializer serializer;

	// private boolean inLoading = false;
	transient private volatile PrintWriter out;

	private boolean inTest = false;

	private static Map files = new HashMap();

	private Set hosts = new HashSet();

	protected boolean isStats = false;

	/**
	 * No-arg constructor.
	 */
	public ResultCollector() {
		// current = -1;
		// serializer = new DefaultConfigurationSerializer();
		setErrorLogging(false);
		setProperty(new ObjectProperty(SAVE_CONFIG, new SampleSaveConfiguration()));
	}

	private void setFilenameProperty(String f) {
		setProperty(FILENAME, f);
	}

	public String getFilename() {
		return getPropertyAsString(FILENAME);
	}

	public boolean isErrorLogging() {
		return getPropertyAsBoolean(ERROR_LOGGING);
	}

	public void setErrorLogging(boolean errorLogging) {
		setProperty(new BooleanProperty(ERROR_LOGGING, errorLogging));
	}

	/**
	 * Sets the filename attribute of the ResultCollector object.
	 * 
	 * @param f
	 *            the new filename value
	 */
	public void setFilename(String f) {
		if (inTest) {
			return;
		}
		setFilenameProperty(f);
	}

	public void testEnded(String host) {
		hosts.remove(host);
		if (hosts.size() == 0) {
			finalizeFileOutput();
			inTest = false;
		}
	}

	public void testStarted(String host) {
		hosts.add(host);
		try {
			initializeFileOutput();
			if (getVisualizer() != null) {
				this.isStats = getVisualizer().isStats();
			}
		} catch (Exception e) {
			log.error("", e);
		}
		inTest = true;
	}

	public void testEnded() {
		testEnded("local"); // $NON-NLS-1$
	}

	public void testStarted() {
		testStarted("local"); // $NON-NLS-1$
	}

	public void loadExistingFile() throws IOException {
		// inLoading = true;
		boolean parsedOK = false;
		if (new File(getFilename()).exists()) {
			clearVisualizer();
			BufferedReader dataReader = null;
            BufferedInputStream bufferedInputStream = null;
			try {
                bufferedInputStream = new BufferedInputStream(new FileInputStream(getFilename()));
                readSamples(SaveService.loadTestResults(bufferedInputStream));
				parsedOK = true;
			} catch (Exception e) {
				log.warn("File load failure, trying old data format.");
				try {
					Configuration savedSamples = getConfiguration(getFilename());
					Configuration[] samples = savedSamples.getChildren();
					for (int i = 0; i < samples.length; i++) {
						SampleResult result = OldSaveService.getSampleResult(samples[i]);
						sendToVisualizer(result);
					}
					parsedOK = true;
				} catch (Exception e1) {
					log.warn("Error parsing XML results " + e);
					log.info("Assuming CSV format instead");
					dataReader = new BufferedReader(new FileReader(getFilename()));
					String line;
					while ((line = dataReader.readLine()) != null) {<commit_msg>Log exception message when file loading fails<commit_after>/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed 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.jmeter.reporters;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.engine.util.NoThreadClone;
import org.apache.jmeter.samplers.Clearable;
import org.apache.jmeter.samplers.Remoteable;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.OldSaveService;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.save.TestResultWrapper;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.ObjectProperty;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.xml.sax.SAXException;

/**
 */
public class ResultCollector extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
		TestListener, Remoteable, NoThreadClone {
	private static final Logger log = LoggingManager.getLoggerForClass();

	private static final long serialVersionUID = 2;

	private static final String TESTRESULTS_START = "<testResults>"; // $NON-NLS-1$

	private static final String TESTRESULTS_START_V1_1_PREVER = "<testResults version=\"";  // $NON-NLS-1$
        private static final String TESTRESULTS_START_V1_1_POSTVER="\">"; // $NON-NLS-1$

	private static final String TESTRESULTS_END = "</testResults>"; // $NON-NLS-1$

	private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; // $NON-NLS-1$

	private static final int MIN_XML_FILE_LEN = XML_HEADER.length() + TESTRESULTS_START.length()
			+ TESTRESULTS_END.length();

	public final static String FILENAME = "filename"; // $NON-NLS-1$

	private final static String SAVE_CONFIG = "saveConfig"; // $NON-NLS-1$

	private static final String ERROR_LOGGING = "ResultCollector.error_logging"; // $NON-NLS-1$

	// protected List results = Collections.synchronizedList(new ArrayList());
	// private int current;
	transient private DefaultConfigurationSerializer serializer;

	// private boolean inLoading = false;
	transient private volatile PrintWriter out;

	private boolean inTest = false;

	private static Map files = new HashMap();

	private Set hosts = new HashSet();

	protected boolean isStats = false;

	/**
	 * No-arg constructor.
	 */
	public ResultCollector() {
		// current = -1;
		// serializer = new DefaultConfigurationSerializer();
		setErrorLogging(false);
		setProperty(new ObjectProperty(SAVE_CONFIG, new SampleSaveConfiguration()));
	}

	private void setFilenameProperty(String f) {
		setProperty(FILENAME, f);
	}

	public String getFilename() {
		return getPropertyAsString(FILENAME);
	}

	public boolean isErrorLogging() {
		return getPropertyAsBoolean(ERROR_LOGGING);
	}

	public void setErrorLogging(boolean errorLogging) {
		setProperty(new BooleanProperty(ERROR_LOGGING, errorLogging));
	}

	/**
	 * Sets the filename attribute of the ResultCollector object.
	 * 
	 * @param f
	 *            the new filename value
	 */
	public void setFilename(String f) {
		if (inTest) {
			return;
		}
		setFilenameProperty(f);
	}

	public void testEnded(String host) {
		hosts.remove(host);
		if (hosts.size() == 0) {
			finalizeFileOutput();
			inTest = false;
		}
	}

	public void testStarted(String host) {
		hosts.add(host);
		try {
			initializeFileOutput();
			if (getVisualizer() != null) {
				this.isStats = getVisualizer().isStats();
			}
		} catch (Exception e) {
			log.error("", e);
		}
		inTest = true;
	}

	public void testEnded() {
		testEnded("local"); // $NON-NLS-1$
	}

	public void testStarted() {
		testStarted("local"); // $NON-NLS-1$
	}

	public void loadExistingFile() throws IOException {
		// inLoading = true;
		boolean parsedOK = false;
		if (new File(getFilename()).exists()) {
			clearVisualizer();
			BufferedReader dataReader = null;
            BufferedInputStream bufferedInputStream = null;
			try {
                bufferedInputStream = new BufferedInputStream(new FileInputStream(getFilename()));
                readSamples(SaveService.loadTestResults(bufferedInputStream));
				parsedOK = true;
			} catch (Exception e) {
				log.warn("File load failure, trying old data format: "+e.getLocalizedMessage());
				try {
					Configuration savedSamples = getConfiguration(getFilename());
					Configuration[] samples = savedSamples.getChildren();
					for (int i = 0; i < samples.length; i++) {
						SampleResult result = OldSaveService.getSampleResult(samples[i]);
						sendToVisualizer(result);
					}
					parsedOK = true;
				} catch (Exception e1) {
					log.warn("Error parsing XML results " + e);
					log.info("Assuming CSV format instead");
					dataReader = new BufferedReader(new FileReader(getFilename()));
					String line;
					while ((line = dataReader.readLine()) != null) {
{'fraction_non_alphanumeric': 0.10925854782942758, 'fraction_numerical': 0.005224740683826354, 'mean_word_length': 4.455155071248952, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 2, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2366455', 'n_tokens_mistral': 4125, 'n_tokens_neox': 3756, 'n_words': 967}