This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
CompileTask.java
87 lines (79 loc) · 3 KB
/
CompileTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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<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;
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);
}
}