/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2011 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2011 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.jasper.rrdtool;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.exolab.castor.xml.Unmarshaller;
import org.opennms.core.utils.StringUtils;
import org.opennms.core.utils.ThreadCategory;
import org.springframework.util.FileCopyUtils;

import net.sf.jasperreports.engine.JRDataSource;

public class RrdtoolXportCmd {

	public JRDataSource executeCommand(String queryString) {
		Xport data = getXportData(queryString);
		return new RrdtoolDataSource(data);
	}

	private Xport getXportData(String queryString) {
		String command = System.getProperty("rrd.binary") + " xport " + queryString.replaceAll("[\r\n]+", " ").replaceAll("\\s+", " ");
		log().debug("getXportData: executing command: " + command);
		String[] commandArray = StringUtils.createCommandArray(command, '@');
		Xport data = null;
		try {
			Process process = Runtime.getRuntime().exec(commandArray);
			// this closes the stream when its finished
			byte[] byteArray = FileCopyUtils.copyToByteArray(process.getInputStream());             
			// this close the stream when its finished
			String errors = FileCopyUtils.copyToString(new InputStreamReader(process.getErrorStream()));
			if (errors.length() > 0) {
				log().error("getXportData: RRDtool command fail: " + errors);
				return null;
			}
			BufferedReader reader = null;
			try {
				InputStream is = new ByteArrayInputStream(byteArray);
				reader = new BufferedReader(new InputStreamReader(is));
				data = (Xport) Unmarshaller.unmarshal(Xport.class, reader);
			} finally {
				reader.close();
			}
		} catch (Exception e) {
			log().error("getXportData: can't execute command '" + command + ": ", e);
			return null;
		}
		return data;
	}

	private ThreadCategory log() {
		return ThreadCategory.getInstance(getClass());
	}

}

@@ -1349,16 +1349,168 @@
 eader;%0A%0A
+import net.sf.jasperreports.engine.JRDataSource;%0Aimport net.sf.jasperreports.engine.JRException;%0Aimport net.sf.jasperreports.engine.util.JRProperties;%0A%0A
 import o
@@ -1685,58 +1685,8 @@
 s;%0A%0A
-import net.sf.jasperreports.engine.JRDataSource;%0A%0A
 publ
@@ -1764,24 +1764,43 @@
 queryString)
+ throws JRException
  %7B%0A%09%09Xport d
@@ -1926,61 +1926,253 @@
 ng) 
-%7B%0A%09%09String command = System.getProperty(%22rrd.b
+throws JRException %7B%0A%09    String rrdBinary = getRrdBinary();%0A        if(rrdBinary == null) %7B%0A            throw new JRException(%22rrd.binary property must be set either in opennms.properties or in iReport%22);%0A        %7D%0A%0A%09%09String command = rrdB
 inary
-%22)
  + %22
@@ -3244,45 +3244,444 @@
 %0A%09%09%09
-return null;%0A%09%09%7D%0A%09%09return data;%0A%09%7D%0A%0A%09
+throw new JRException(%22getXportData: can't execute command '%22 + command + %22: %22, e);%0A%09%09%7D%0A%09%09return data;%0A%09%7D%0A%0A%09private String getRrdBinary() %7B%0A%09    if(System.getProperty(%22rrd.binary%22) != null) %7B%0A            return System.getProperty(%22rrd.binary%22);%0A        %7D else if(JRProperties.getProperty(%22rrd.binary%22) != null) %7B%0A            return JRProperties.getProperty(%22rrd.binary%22);%0A        %7D else %7B%0A            return null;%0A        %7D%0A%0A    %7D%0A%0A    
 priv

update RrdtoolXportCmd to work with iReport