1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.sql.Connection ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 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 50 public class AsynchronousFillHandle 51 { 52 protected final JasperReport jasperReport; 53 protected final Map parameters; 54 protected final JRDataSource dataSource; 55 protected final Connection conn; 56 protected final JRBaseFiller filler; 57 protected final List listeners; 58 protected Thread fillThread; 59 protected boolean started = false; 60 protected boolean running = false; 61 protected boolean cancelled = false; 62 protected final Object lock; 63 64 protected Integer priority = null; 65 66 protected String threadName; 67 68 protected AsynchronousFillHandle ( 69 JasperReport jasperReport, 70 Map parameters, 71 JRDataSource dataSource 72 ) throws JRException 73 { 74 this(jasperReport, parameters, dataSource, null); 75 } 76 77 protected AsynchronousFillHandle ( 78 JasperReport jasperReport, 79 Map parameters, 80 Connection conn 81 ) throws JRException 82 { 83 this(jasperReport, parameters, null, conn); 84 } 85 86 protected AsynchronousFillHandle ( 87 JasperReport jasperReport, 88 Map parameters 89 ) throws JRException 90 { 91 this(jasperReport, parameters, null, null); 92 } 93 94 protected AsynchronousFillHandle ( 95 JasperReport jasperReport, 96 Map parameters, 97 JRDataSource dataSource, 98 Connection 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 (); 107 lock = this; 108 } 109 110 111 116 public void addListener(AsynchronousFilllListener listener) 117 { 118 listeners.add(listener); 119 } 120 121 122 128 public boolean removeListener(AsynchronousFilllListener listener) 129 { 130 return listeners.remove(listener); 131 } 132 133 134 protected class ReportFiller implements Runnable 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 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 195 public void startFill() 196 { 197 synchronized (lock) 198 { 199 if (started) 200 { 201 throw new IllegalStateException ("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 (reportFiller); 212 } 213 else 214 { 215 fillThread = new Thread (reportFiller, threadName); 216 } 217 218 if (priority != null) 219 { 220 fillThread.setPriority(priority.intValue()); 221 } 222 223 fillThread.start(); 224 } 225 226 227 236 public void cancellFill() throws JRException 237 { 238 synchronized (lock) 239 { 240 if (!running) 241 { 242 throw new IllegalStateException ("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 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 i = listeners.iterator(); i.hasNext();) 264 { 265 AsynchronousFilllListener listener = (AsynchronousFilllListener) i.next(); 266 listener.reportCancelled(); 267 } 268 } 269 270 271 protected void notifyError(Throwable e) 272 { 273 for (Iterator i = listeners.iterator(); i.hasNext();) 274 { 275 AsynchronousFilllListener listener = (AsynchronousFilllListener) i.next(); 276 listener.reportFillError(e); 277 } 278 } 279 280 281 290 public static AsynchronousFillHandle createHandle( 291 JasperReport jasperReport, 292 Map parameters, 293 JRDataSource dataSource 294 ) throws JRException 295 { 296 AsynchronousFillHandle filler = new AsynchronousFillHandle(jasperReport, parameters, dataSource); 297 298 return filler; 299 } 300 301 302 311 public static AsynchronousFillHandle createHandle( 312 JasperReport jasperReport, 313 Map parameters, 314 Connection conn 315 ) throws JRException 316 { 317 AsynchronousFillHandle filler = new AsynchronousFillHandle(jasperReport, parameters, conn); 318 319 return filler; 320 } 321 322 323 331 public static AsynchronousFillHandle createHandle( 332 JasperReport jasperReport, 333 Map parameters 334 ) throws JRException 335 { 336 AsynchronousFillHandle filler = new AsynchronousFillHandle(jasperReport, parameters); 337 338 return filler; 339 } 340 341 342 348 public void setPriority (int priority) 349 { 350 synchronized (lock) 351 { 352 this.priority = new Integer (priority); 353 if (fillThread != null) 354 { 355 fillThread.setPriority(priority); 356 } 357 } 358 } 359 360 361 367 public void setThreadName (String 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 |