001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * CompileThread.java
028: *
029: * Created on 19 maggio 2004, 19.52
030: *
031: */
032:
033: package it.businesslogic.ireport.plugin.massivecompiler;
034:
035: import javax.swing.table.*;
036: import java.io.*;
037: import it.businesslogic.ireport.ReportClassLoader;
038: import it.businesslogic.ireport.gui.MainFrame;
039: import it.businesslogic.ireport.util.Misc;
040: import net.sf.jasperreports.engine.*;
041:
042: /**
043: *
044: * @author Administrator
045: */
046: public class CompileThread implements Runnable {
047:
048: private MassiveCompilerFrame massiveCompilerFrame = null;
049: private boolean stop = false;
050: private Thread thread = null;
051: ReportClassLoader reportClassLoader = null;
052:
053: private boolean compileSelectedOnly = false;
054:
055: public CompileThread(MassiveCompilerFrame mcf) {
056: this .massiveCompilerFrame = mcf;
057: thread = new Thread(this );
058: MainFrame.reportClassLoader.rescanLibDirectory();
059: Thread.currentThread().setContextClassLoader(
060: MainFrame.reportClassLoader);
061: }
062:
063: public void stop() {
064: stop = true;
065: }
066:
067: public void start() {
068: thread.start();
069: }
070:
071: public void run() {
072: if (massiveCompilerFrame == null) {
073: return;
074: }
075:
076: DefaultTableModel dtm = (DefaultTableModel) massiveCompilerFrame
077: .getFileTable().getModel();
078:
079: for (int i = 0; i < dtm.getRowCount(); ++i) {
080: FileEntry fe = (FileEntry) dtm.getValueAt(i, 0);
081: if (isCompileSelectedOnly()
082: && !massiveCompilerFrame.getFileTable()
083: .isRowSelected(i)) {
084: continue;
085: }
086:
087: // Start to compile this report...
088: String srcFileName = "";
089: String destFileName = "";
090:
091: try {
092:
093: fe.setStatus(fe.STATUS_COMPILING);
094: dtm.setValueAt(fe, i, 0);
095: try {
096: dtm.setValueAt(fe.getFile().getCanonicalPath(), i,
097: 1);
098: } catch (Exception ex) {
099: ex.printStackTrace();
100: }
101: dtm.setValueAt(fe.decodeStatus(fe.getStatus()), i, 2);
102:
103: srcFileName = fe.getFile().getCanonicalPath();
104:
105: try {
106: reportClassLoader.setRelodablePaths(fe.getFile()
107: .getParent());
108: Thread.currentThread().setContextClassLoader(
109: reportClassLoader);
110: } catch (Exception ex) {
111: }
112:
113: if (massiveCompilerFrame.isSelectedChangeFileExt()) {
114: String srcFileName2 = it.businesslogic.ireport.util.Misc
115: .changeFileExtension(srcFileName, "jrxml");
116:
117: if (!fe.getFile().renameTo(new File(srcFileName2))) {
118: fe.setFile(new File(srcFileName2));
119: fe.setStatus(fe.STATUS_ERROR_COMPILING);
120: fe.setMessage("Unable to rename "
121: + srcFileName
122: + " to "
123: + it.businesslogic.ireport.util.Misc
124: .changeFileExtension(
125: srcFileName, "jrxml")
126: + " **" + fe);
127:
128: dtm.setValueAt(fe, i, 0);
129: dtm.setValueAt(fe.decodeStatus(fe.getStatus()),
130: i, 2);
131: massiveCompilerFrame.getFileTable().updateUI();
132: continue;
133: } else {
134: fe.setFile(new File(srcFileName2));
135: dtm.setValueAt(fe.decodeStatus(fe.getStatus()),
136: i, 2);
137: System.out.println("File renamed as:"
138: + fe.getFile().getCanonicalPath());
139: }
140: srcFileName = srcFileName2;
141: }
142:
143: destFileName = it.businesslogic.ireport.util.Misc
144: .changeFileExtension(srcFileName, "jasper");
145:
146: if (massiveCompilerFrame.isSelectedOptionsCompileDir()) {
147: String destDir = MainFrame.getMainInstance()
148: .getProperties().getProperty(
149: "DefaultCompilationDirectory", ".");
150: if (!destDir.equals("") && !destDir.equals(".")) {
151: File f = new File(destFileName);
152: File f2 = new File(destDir, f.getName());
153: destFileName = f2 + "";
154: }
155: }
156:
157: if (massiveCompilerFrame.isSelectedBackup()) {
158: File old_jasper = new File(destFileName);
159: if (old_jasper.exists()) {
160: old_jasper.renameTo(new File(
161: it.businesslogic.ireport.util.Misc
162: .changeFileExtension(
163: destFileName,
164: "jasper.bak")));
165: }
166:
167: }
168:
169: String compiler_name = "JasperReports default compiler";
170: String compiler_code = it.businesslogic.ireport.gui.MainFrame
171: .getMainInstance().getProperties().getProperty(
172: "DefaultCompiler");
173:
174: // Load the file to look for the compiler to use....
175: boolean useGroovy = false;
176: java.io.FileInputStream fis = null;
177: try {
178: byte[] buffer = new byte[1024 * 5]; // we hope that the compiler is specified in the first 5k...
179: fis = new FileInputStream(srcFileName);
180: int reads = fis.read(buffer);
181: String s = new String(buffer, 0, reads);
182: if (s.indexOf("language=\"groovy\"") >= 0) {
183: useGroovy = true;
184: }
185: } catch (Exception ex) {
186:
187: } finally {
188: if (fis != null) {
189: try {
190: fis.close();
191: } catch (Exception ex2) {
192: }
193: }
194: }
195:
196: net.sf.jasperreports.engine.util.JRProperties
197: .setProperty(
198: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASSPATH,
199: Misc.nvl(new File(srcFileName)
200: .getParent(), ".")
201: + File.pathSeparator
202: + Misc.getClassPath());
203:
204: if (!useGroovy && compiler_code != null
205: && !compiler_code.equals("0")
206: && !compiler_code.equals("")) {
207: if (compiler_code.equals("1")) {
208: System
209: .setProperty(
210: "jasper.reports.compiler.class",
211: "net.sf.jasperreports.engine.design.JRJdk13Compiler");
212: net.sf.jasperreports.engine.util.JRProperties
213: .setProperty(
214: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,
215: "net.sf.jasperreports.engine.design.JRJdk13Compiler");
216: compiler_name = "Java Compiler";
217: } else if (compiler_code.equals("2")) {
218: System
219: .setProperty(
220: "jasper.reports.compiler.class",
221: "net.sf.jasperreports.engine.design.JRJdtCompiler");
222: net.sf.jasperreports.engine.util.JRProperties
223: .setProperty(
224: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,
225: "net.sf.jasperreports.engine.design.JRJdtCompiler");
226: compiler_name = "JDT Compiler";
227: } else if (compiler_code.equals("3")) {
228: System
229: .setProperty(
230: "jasper.reports.compiler.class",
231: "net.sf.jasperreports.engine.design.JRBshCompiler");
232: net.sf.jasperreports.engine.util.JRProperties
233: .setProperty(
234: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,
235: "net.sf.jasperreports.engine.design.JRBshCompiler");
236: compiler_name = "BeanShell Compiler";
237: } else if (compiler_code.equals("4")) {
238: System
239: .setProperty(
240: "jasper.reports.compiler.class",
241: "net.sf.jasperreports.engine.design.JRJikesCompiler");
242: net.sf.jasperreports.engine.util.JRProperties
243: .setProperty(
244: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,
245: "net.sf.jasperreports.engine.design.JRJikesCompiler");
246: compiler_name = "Jikes Compiler";
247: }
248: } else if (useGroovy) {
249: System
250: .setProperty(
251: "jasper.reports.compiler.class",
252: "net.sf.jasperreports.compilers.JRGroovyCompiler");
253: net.sf.jasperreports.engine.util.JRProperties
254: .setProperty(
255: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,
256: "net.sf.jasperreports.compilers.JRGroovyCompiler");
257: } else {
258: System.setProperty("jasper.reports.compiler.class",
259: "");
260: net.sf.jasperreports.engine.util.JRProperties
261: .setProperty(
262: net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,
263: "");
264: }
265:
266: JasperCompileManager.compileReportToFile(srcFileName,
267: destFileName);
268:
269: if (useGroovy) {
270: fe.setStatus(fe.STATUS_COMPILED_GROOVY);
271: } else {
272: fe.setStatus(fe.STATUS_COMPILED);
273: }
274: } catch (Exception ex) {
275: fe.setStatus(fe.STATUS_ERROR_COMPILING);
276:
277: StringWriter sw = new StringWriter();
278: ex.printStackTrace(new PrintWriter(sw));
279: fe.setMessage(sw.getBuffer().toString());
280: }
281:
282: dtm.setValueAt(fe, i, 0);
283: try {
284: dtm.setValueAt(fe.getFile().getCanonicalPath(), i, 1);
285: } catch (Exception ex) {
286: ex.printStackTrace();
287: }
288: dtm.setValueAt(fe.decodeStatus(fe.getStatus()), i, 2);
289: }
290:
291: massiveCompilerFrame.getFileTable().updateUI();
292: massiveCompilerFrame.finishedCompiling();
293: return;
294: }
295:
296: /** Getter for property compileSelectedOnly.
297: * @return Value of property compileSelectedOnly.
298: *
299: */
300: public boolean isCompileSelectedOnly() {
301: return compileSelectedOnly;
302: }
303:
304: /** Setter for property compileSelectedOnly.
305: * @param compileSelectedOnly New value of property compileSelectedOnly.
306: *
307: */
308: public void setCompileSelectedOnly(boolean compileSelectedOnly) {
309: this.compileSelectedOnly = compileSelectedOnly;
310: }
311:
312: }
|