KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > AsynchronousFillHandle


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.sql.Connection JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import net.sf.jasperreports.engine.JRDataSource;
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JasperPrint;
39 import net.sf.jasperreports.engine.JasperReport;
40
41 /**
42  * Class used to perform report filling asychronously.
43  * <p>
44  * An instance of this type can be used as a handle to an asychronous fill process.
45  * The main benefit of this method is that the filling process can be cancelled.
46  *
47  * @author Lucian Chirita (lucianc@users.sourceforge.net)
48  * @version $Id: AsynchronousFillHandle.java 1440 2006-10-19 12:01:06 +0300 (Thu, 19 Oct 2006) lucianc $
49  */

50 public class AsynchronousFillHandle
51 {
52     protected final JasperReport jasperReport;
53     protected final Map JavaDoc parameters;
54     protected final JRDataSource dataSource;
55     protected final Connection JavaDoc conn;
56     protected final JRBaseFiller filler;
57     protected final List JavaDoc listeners;
58     protected Thread JavaDoc fillThread;
59     protected boolean started = false;
60     protected boolean running = false;
61     protected boolean cancelled = false;
62     protected final Object JavaDoc lock;
63     
64     protected Integer JavaDoc priority = null;
65     
66     protected String JavaDoc threadName;
67     
68     protected AsynchronousFillHandle (
69             JasperReport jasperReport,
70             Map JavaDoc parameters,
71             JRDataSource dataSource
72             ) throws JRException
73     {
74         this(jasperReport, parameters, dataSource, null);
75     }
76     
77     protected AsynchronousFillHandle (
78             JasperReport jasperReport,
79             Map JavaDoc parameters,
80             Connection JavaDoc conn
81             ) throws JRException
82     {
83         this(jasperReport, parameters, null, conn);
84     }
85     
86     protected AsynchronousFillHandle (
87             JasperReport jasperReport,
88             Map JavaDoc parameters
89             ) throws JRException
90     {
91         this(jasperReport, parameters, null, null);
92     }
93     
94     protected AsynchronousFillHandle (
95             JasperReport jasperReport,
96             Map JavaDoc parameters,
97             JRDataSource dataSource,
98             Connection JavaDoc conn
99             ) throws JRException
100     {
101         this.jasperReport = jasperReport;
102         this.parameters = parameters;
103         this.dataSource = dataSource;
104         this.conn = conn;
105         this.filler = JRFiller.createFiller(jasperReport);
106         this.listeners = new ArrayList JavaDoc();
107         lock = this;
108     }
109
110     
111     /**
112      * Adds a listener to the filling process.
113      *
114      * @param listener the listener to be added
115      */

116     public void addListener(AsynchronousFilllListener listener)
117     {
118         listeners.add(listener);
119     }
120
121
122     /**
123      * Removes a listener from the filling process.
124      *
125      * @param listener the listener to be removed
126      * @return <tt>true</tt> if the listener was found and removed
127      */

128     public boolean removeListener(AsynchronousFilllListener listener)
129     {
130         return listeners.remove(listener);
131     }
132
133     
134     protected class ReportFiller implements Runnable JavaDoc
135     {
136         public void run()
137         {
138             synchronized (lock)
139             {
140                 running = true;
141             }
142             
143             try
144             {
145                 JasperPrint print;
146                 if (conn != null)
147                 {
148                     print = filler.fill(parameters, conn);
149                 }
150                 else if (dataSource != null)
151                 {
152                     print = filler.fill(parameters, dataSource);
153                 }
154                 else
155                 {
156                     print = filler.fill(parameters);
157                 }
158                 
159                 notifyFinish(print);
160             }
161             catch (Exception JavaDoc e)
162             {
163                 synchronized (lock)
164                 {
165                     if (cancelled)
166                     {
167                         notifyCancel();
168                     }
169                     else
170                     {
171                         notifyError(e);
172                     }
173                 }
174             }
175             finally
176             {
177                 synchronized (lock)
178                 {
179                     running = false;
180                 }
181             }
182         }
183     }
184     
185     
186     /**
187      * Starts the filling process asychronously.
188      * <p>
189      * The filling is launched on a new thread and the method exits
190      * after the thread is started.
191      * <p>
192      * When the filling finishes either in success or failure, the listeners
193      * are notified.
194      */

195     public void startFill()
196     {
197         synchronized (lock)
198         {
199             if (started)
200             {
201                 throw new IllegalStateException JavaDoc("Fill already started.");
202             }
203
204             started = true;
205         }
206         
207         ReportFiller reportFiller = new ReportFiller();
208         
209         if (threadName == null)
210         {
211             fillThread = new Thread JavaDoc(reportFiller);
212         }
213         else
214         {
215             fillThread = new Thread JavaDoc(reportFiller, threadName);
216         }
217         
218         if (priority != null)
219         {
220             fillThread.setPriority(priority.intValue());
221         }
222         
223         fillThread.start();
224     }
225
226     
227     /**
228      * Cancels the fill started by the handle.
229      * <p>
230      * The method sends a cancel signal to the filling process.
231      * When the filling process will end, the listeners will be notified
232      * that the filling has been cancelled.
233      *
234      * @throws JRException
235      */

236     public void cancellFill() throws JRException
237     {
238         synchronized (lock)
239         {
240             if (!running)
241             {
242                 throw new IllegalStateException JavaDoc("Fill not running.");
243             }
244             
245             filler.cancelFill();
246             cancelled = true;
247         }
248     }
249     
250     
251     protected void notifyFinish(JasperPrint print)
252     {
253         for (Iterator JavaDoc i = listeners.iterator(); i.hasNext();)
254         {
255             AsynchronousFilllListener listener = (AsynchronousFilllListener) i.next();
256             listener.reportFinished(print);
257         }
258     }
259     
260     
261     protected void notifyCancel()
262     {
263         for (Iterator JavaDoc i = listeners.iterator(); i.hasNext();)
264         {
265             AsynchronousFilllListener listener = (AsynchronousFilllListener) i.next();
266             listener.reportCancelled();
267         }
268     }
269     
270     
271     protected void notifyError(Throwable JavaDoc e)
272     {
273         for (Iterator JavaDoc i = listeners.iterator(); i.hasNext();)
274         {
275             AsynchronousFilllListener listener = (AsynchronousFilllListener) i.next();
276             listener.reportFillError(e);
277         }
278     }
279
280
281     /**
282      * Creates an asychronous filling handle.
283      *
284      * @param jasperReport the report
285      * @param parameters the parameter map
286      * @param dataSource the data source
287      * @return the handle
288      * @throws JRException
289      */

290     public static AsynchronousFillHandle createHandle(
291         JasperReport jasperReport,
292         Map JavaDoc parameters,
293         JRDataSource dataSource
294         ) throws JRException
295     {
296         AsynchronousFillHandle filler = new AsynchronousFillHandle(jasperReport, parameters, dataSource);
297         
298         return filler;
299     }
300
301
302     /**
303      * Creates an asychronous filling handle.
304      *
305      * @param jasperReport the report
306      * @param parameters the parameter map
307      * @param conn the connection
308      * @return the handle
309      * @throws JRException
310      */

311     public static AsynchronousFillHandle createHandle(
312         JasperReport jasperReport,
313         Map JavaDoc parameters,
314         Connection JavaDoc conn
315         ) throws JRException
316     {
317         AsynchronousFillHandle filler = new AsynchronousFillHandle(jasperReport, parameters, conn);
318         
319         return filler;
320     }
321
322
323     /**
324      * Creates an asychronous filling handle.
325      *
326      * @param jasperReport the report
327      * @param parameters the parameter map
328      * @return the handle
329      * @throws JRException
330      */

331     public static AsynchronousFillHandle createHandle(
332         JasperReport jasperReport,
333         Map JavaDoc parameters
334         ) throws JRException
335     {
336         AsynchronousFillHandle filler = new AsynchronousFillHandle(jasperReport, parameters);
337         
338         return filler;
339     }
340     
341     
342     /**
343      * Sets the priority of the filler thread.
344      *
345      * @param priority the filler thread priority.
346      * @see Thread#setPriority(int)
347      */

348     public void setPriority (int priority)
349     {
350         synchronized (lock)
351         {
352             this.priority = new Integer JavaDoc(priority);
353             if (fillThread != null)
354             {
355                 fillThread.setPriority(priority);
356             }
357         }
358     }
359     
360     
361     /**
362      * Sets the name of the filler thread.
363      *
364      * @param name the filler thread name.
365      * @see Thread#setName(java.lang.String)
366      */

367     public void setThreadName (String JavaDoc name)
368     {
369         synchronized (lock)
370         {
371             this.threadName = name;
372             if (fillThread != null)
373             {
374                 fillThread.setName(name);
375             }
376         }
377     }
378 }
379
Popular Tags