All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alexnederlof.jasperreport.CompileTask Maven / Gradle / Ivy

There is a newer version: 2.8
Show newest version
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.InputStream;
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 {

    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;
        InputStream in = null;
        try {
            out = new FileOutputStream(destination);
            in = new FileInputStream(source);
            JasperCompileManager.compileReportToStream(in, out);
            if (verbose) {
                log.info("Compiling " + source.getName());
            }
        } catch (Exception e) {
            cleanUpAndThrowError(destination, e);
        } finally {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        }
        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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy