package com.alexnederlof.jasperreport;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to you 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.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.concurrent.Callable;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.maven.plugin.logging.Log;

/**
 * A task that compiles a Jasper sourcefile.
 */
public class CompileTask implements Callable<Void> {

	private final File source;
	private final File destination;
	private final Log log;
	private final boolean verbose;

	/**
	 * @param source
	 *            The source file.
	 * @param destination
	 *            The destination file.
	 * @param log
	 *            The logger.
	 * @param verbose
	 *            If the output should be verbose.
	 */
	public CompileTask(File source, File destination, Log log, boolean verbose) {
		super();
		this.source = source;
		this.destination = destination;
		this.log = log;
		this.verbose = verbose;
	}

	/**
	 * Compile the source file. If the source file doesn't have the right extension, it is skipped.
	 *
	 * @return Debug output of the compile action.
	 * @throws Exception
	 *             when anything goes wrong while compiling.
	 */
	@Override
	public Void call() throws Exception {
		OutputStream out = null;
		try {
	        out =  new FileOutputStream(destination);
			JasperCompileManager.compileReportToStream(new FileInputStream(source), out);
			if (verbose) {
				log.info("Compiling " + source.getName());
			}
		}
		catch (Exception e) {
		    if (out != null) {
		        out.close();
		    }
			cleanUpAndThrowError(destination, e);
		}
		return null;
	}

	private void cleanUpAndThrowError(File out, Exception e) throws JRException {
		log.error("Could not compile " + source.getName() + " because " + e.getMessage(), e);
		if (out != null && out.exists()) {
			out.delete();
		}
		throw new JRException("Could not compile " + source.getName(), e);
	}
}

@@ -890,32 +890,60 @@
 leOutputStream;%0A
+import java.io.InputStream;%0A
 import java.io.O
@@ -2115,64 +2115,168 @@
 ll;%0A
-%09%09try %7B%0A%09        out =  new FileOutputStream(destination
+                InputStream in = null;%0A%09%09try %7B%0A                    out =  new FileOutputStream(destination);%0A                    in = new FileInputStream(source
 );%0A%09
@@ -2324,35 +2324,10 @@
 eam(
+i
 n
-ew FileInputStream(source)
 , ou
@@ -2484,16 +2484,110 @@
 %09%09    %7D%0A
+                    if(in != null)%7B%0A                        in.close();%0A                    %7D%0A
 %09%09%09clean

Fix #38 - closing inputStreams