package kg.apc.jmeter.perfmon;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import kg.apc.perfmon.PerfMonMetricGetter;
import kg.apc.perfmon.client.Transport;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class NewAgentConnector implements PerfMonAgentConnector {

    private static final Logger log = LoggingManager.getLoggerForClass();
    protected Transport transport;
    private Map<String, String> metrics = new HashMap<String, String>();
    private String[] metricLabels;

    public void setTransport(Transport atransport) {
        transport = atransport;
    }

    public void connect() throws IOException {
        log.debug(metrics.toString());

        ArrayList<String> labels = new ArrayList<String>(metrics.keySet());
        metricLabels = labels.toArray(new String[0]);

        ArrayList<String> arr = new ArrayList<String>(metrics.values());
        String[] m = arr.toArray(new String[0]);
        transport.startWithMetrics(m);
    }

    public void disconnect() {
        transport.disconnect();
    }

    public void generateSamples(PerfMonSampleGenerator collector) throws IOException {
        String[] data = transport.readMetrics();
        for (int n = 0; n < data.length; n++) {
            if (!data[n].isEmpty()) {
                try {
                    collector.generateSample(Double.parseDouble(data[n]), metricLabels[n]);
                } catch (NumberFormatException e) {
                    collector.generateErrorSample(metricLabels[n], e.toString());
                } catch (ArrayIndexOutOfBoundsException e) {
                    collector.generateErrorSample(metricLabels[n], e.toString());
                }
            }
        }
    }

    public void addMetric(String metric, String params, String label) {
        metrics.put(label, metric.toLowerCase() + PerfMonMetricGetter.DVOETOCHIE + params);
    }
}

@@ -29,112 +29,8 @@
 n;%0A%0A
-import java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A
 impo
@@ -189,16 +189,121 @@
 ogger;%0A%0A
+import java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 public c
@@ -524,30 +524,16 @@
 HashMap%3C
-String, String
 %3E();%0A   
@@ -785,38 +785,32 @@
 = new ArrayList%3C
-String
 %3E(metrics.keySet
@@ -859,25 +859,37 @@
 (new String%5B
-0
+labels.size()
 %5D);%0A%0A       
@@ -927,22 +927,16 @@
 rayList%3C
-String
 %3E(metric
@@ -992,17 +992,26 @@
  String%5B
-0
+arr.size()
 %5D);%0A    
@@ -1509,120 +1509,10 @@
 ion 
-e) %7B%0A                    collector.generateErrorSample(metricLabels%5Bn%5D, e.toString());%0A                %7D catch (
+%7C 
 Arra

reformat code