KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > plugin > massivecompiler > CompileThread


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * CompileThread.java
28  *
29  * Created on 19 maggio 2004, 19.52
30  *
31  */

32
33 package it.businesslogic.ireport.plugin.massivecompiler;
34
35 import javax.swing.table.*;
36 import java.io.*;
37 import it.businesslogic.ireport.ReportClassLoader;
38 import it.businesslogic.ireport.gui.MainFrame;
39 import it.businesslogic.ireport.util.Misc;
40 import net.sf.jasperreports.engine.*;
41 /**
42  *
43  * @author Administrator
44  */

45 public class CompileThread implements Runnable JavaDoc {
46     
47     private MassiveCompilerFrame massiveCompilerFrame = null;
48     private boolean stop = false;
49     private Thread JavaDoc thread = null;
50     ReportClassLoader reportClassLoader = null;
51     
52     
53     private boolean compileSelectedOnly = false;
54     
55     public CompileThread(MassiveCompilerFrame mcf)
56     {
57         this.massiveCompilerFrame = mcf;
58         thread = new Thread JavaDoc(this);
59         MainFrame.reportClassLoader.rescanLibDirectory();
60         Thread.currentThread().setContextClassLoader( MainFrame.reportClassLoader);
61     }
62     
63     public void stop()
64     {
65         stop = true;
66     }
67     
68     public void start()
69     {
70         thread.start();
71     }
72     
73     public void run() {
74         if (massiveCompilerFrame == null)
75         {
76             return;
77         }
78         
79         DefaultTableModel dtm = (DefaultTableModel)massiveCompilerFrame.getFileTable().getModel();
80         
81         for (int i=0; i<dtm.getRowCount(); ++i)
82         {
83             FileEntry fe = (FileEntry)dtm.getValueAt(i, 0);
84             if ( isCompileSelectedOnly() && !massiveCompilerFrame.getFileTable().isRowSelected(i))
85             {
86                 continue;
87             }
88             
89             // Start to compile this report...
90
String JavaDoc srcFileName = "";
91             String JavaDoc destFileName = "";
92             
93             
94             
95             
96             try
97             {
98                 
99                 fe.setStatus( fe.STATUS_COMPILING );
100                 dtm.setValueAt( fe, i, 0);
101                 try {
102                 dtm.setValueAt( fe.getFile().getCanonicalPath(), i, 1);
103                 } catch (Exception JavaDoc ex) {
104                     ex.printStackTrace();
105                 }
106                 dtm.setValueAt( fe.decodeStatus( fe.getStatus()), i, 2);
107             
108                 srcFileName = fe.getFile().getCanonicalPath();
109                 
110                 try {
111                         reportClassLoader.setRelodablePaths( fe.getFile().getParent() );
112                         Thread.currentThread().setContextClassLoader( reportClassLoader );
113                     } catch (Exception JavaDoc ex) {}
114                 
115                 if (massiveCompilerFrame.isSelectedChangeFileExt())
116                 {
117                     String JavaDoc srcFileName2 = it.businesslogic.ireport.util.Misc.changeFileExtension(srcFileName,"jrxml");
118                     
119                     if (!fe.getFile().renameTo( new File(srcFileName2 )))
120                     {
121                         fe.setFile( new File(srcFileName2 ) );
122                         fe.setStatus( fe.STATUS_ERROR_COMPILING);
123                         fe.setMessage("Unable to rename " + srcFileName + " to " + it.businesslogic.ireport.util.Misc.changeFileExtension(srcFileName,"jrxml")+" **" + fe);
124                         
125                         dtm.setValueAt( fe, i, 0);
126                         dtm.setValueAt( fe.decodeStatus( fe.getStatus()), i, 2);
127                         massiveCompilerFrame.getFileTable().updateUI();
128                         continue;
129                     }
130                     else
131                     {
132                         fe.setFile( new File(srcFileName2 ) );
133                         dtm.setValueAt( fe.decodeStatus( fe.getStatus()), i, 2);
134                         System.out.println("File renamed as:" + fe.getFile().getCanonicalPath() );
135                     }
136                     srcFileName = srcFileName2;
137                 }
138                 
139                 destFileName = it.businesslogic.ireport.util.Misc.changeFileExtension(srcFileName,"jasper");
140                 
141                 if (massiveCompilerFrame.isSelectedOptionsCompileDir())
142                 {
143                     String JavaDoc destDir = MainFrame.getMainInstance().getProperties().getProperty("DefaultCompilationDirectory",".");
144                     if (!destDir.equals("") && !destDir.equals("."))
145                     {
146                         File f = new File(destFileName);
147                         File f2 = new File(destDir, f.getName());
148                         destFileName = f2+"";
149                     }
150                 }
151                 
152                 if (massiveCompilerFrame.isSelectedBackup())
153                 {
154                     File old_jasper = new File(destFileName);
155                     if (old_jasper.exists())
156                     {
157                         old_jasper.renameTo(new File( it.businesslogic.ireport.util.Misc.changeFileExtension(destFileName,"jasper.bak")) );
158                     }
159                         
160                 }
161                 
162                 String JavaDoc compiler_name = "JasperReports default compiler";
163                 String JavaDoc compiler_code = it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("DefaultCompiler");
164         
165                 // Load the file to look for the compiler to use....
166
boolean useGroovy = false;
167                 java.io.FileInputStream JavaDoc fis = null;
168                 try {
169                     byte[] buffer = new byte[1024*5]; // we hope that the compiler is specified in the first 5k...
170
fis = new FileInputStream( srcFileName );
171                     int reads = fis.read(buffer);
172                     String JavaDoc s = new String JavaDoc(buffer,0,reads);
173                     if (s.indexOf("language=\"groovy\"") >= 0)
174                     {
175                         useGroovy = true;
176                     }
177                 } catch (Exception JavaDoc ex)
178                 {
179                     
180                 } finally
181                 {
182                     if (fis != null)
183                     {
184                         try { fis.close(); } catch (Exception JavaDoc ex2) {}
185                     }
186                 }
187                 
188                  net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASSPATH, Misc.nvl( new File(srcFileName).getParent(), ".") + File.pathSeparator + Misc.getClassPath());
189                 
190                 if (!useGroovy && compiler_code != null && !compiler_code.equals("0") && !compiler_code.equals(""))
191         {
192             if (compiler_code.equals("1"))
193             {
194                 System.setProperty("jasper.reports.compiler.class","net.sf.jasperreports.engine.design.JRJdk13Compiler" );
195                             net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,"net.sf.jasperreports.engine.design.JRJdk13Compiler" );
196                 compiler_name = "Java Compiler";
197             }
198             else if (compiler_code.equals("2"))
199             {
200                 System.setProperty("jasper.reports.compiler.class","net.sf.jasperreports.engine.design.JRJdtCompiler" );
201                             net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,"net.sf.jasperreports.engine.design.JRJdtCompiler" );
202                 compiler_name = "JDT Compiler";
203             }
204             else if (compiler_code.equals("3"))
205             {
206                 System.setProperty("jasper.reports.compiler.class","net.sf.jasperreports.engine.design.JRBshCompiler" );
207                             net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,"net.sf.jasperreports.engine.design.JRBshCompiler" );
208                 compiler_name = "BeanShell Compiler";
209             }
210             else if (compiler_code.equals("4"))
211             {
212                 System.setProperty("jasper.reports.compiler.class","net.sf.jasperreports.engine.design.JRJikesCompiler" );
213                             net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,"net.sf.jasperreports.engine.design.JRJikesCompiler" );
214                 compiler_name = "Jikes Compiler";
215             }
216         }
217                 else if (useGroovy)
218                 {
219                     System.setProperty("jasper.reports.compiler.class","net.sf.jasperreports.compilers.JRGroovyCompiler" );
220                     net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,"net.sf.jasperreports.compilers.JRGroovyCompiler");
221                 }
222         else
223         {
224             System.setProperty("jasper.reports.compiler.class","" );
225                         net.sf.jasperreports.engine.util.JRProperties.setProperty(net.sf.jasperreports.engine.util.JRProperties.COMPILER_CLASS,"");
226         }
227                 
228                 
229                 
230                 JasperCompileManager.compileReportToFile(srcFileName, destFileName);
231                 
232                 if (useGroovy)
233                 {
234                     fe.setStatus(fe.STATUS_COMPILED_GROOVY);
235                 }
236                 else
237                 {
238                     fe.setStatus( fe.STATUS_COMPILED );
239                 }
240             } catch (Exception JavaDoc ex)
241             {
242                 fe.setStatus( fe.STATUS_ERROR_COMPILING );
243                 
244                 StringWriter sw = new StringWriter();
245                 ex.printStackTrace( new PrintWriter( sw ));
246                 fe.setMessage( sw.getBuffer().toString() );
247             }
248             
249             dtm.setValueAt( fe, i, 0);
250             try {
251             dtm.setValueAt( fe.getFile().getCanonicalPath(), i, 1);
252             } catch (Exception JavaDoc ex) {
253                 ex.printStackTrace();
254             }
255             dtm.setValueAt( fe.decodeStatus( fe.getStatus()), i, 2);
256         }
257         
258         massiveCompilerFrame.getFileTable().updateUI();
259         massiveCompilerFrame.finishedCompiling();
260         return;
261     }
262     
263     /** Getter for property compileSelectedOnly.
264      * @return Value of property compileSelectedOnly.
265      *
266      */

267     public boolean isCompileSelectedOnly() {
268         return compileSelectedOnly;
269     }
270     
271     /** Setter for property compileSelectedOnly.
272      * @param compileSelectedOnly New value of property compileSelectedOnly.
273      *
274      */

275     public void setCompileSelectedOnly(boolean compileSelectedOnly) {
276         this.compileSelectedOnly = compileSelectedOnly;
277     }
278     
279 }
280
Popular Tags