author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 438 | 2ae294e4518c |
child 2386 | 7e59f81fd05b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.print; |
|
27 |
||
28 |
import java.net.URI; |
|
29 |
import java.net.URL; |
|
30 |
import java.io.BufferedInputStream; |
|
31 |
import java.io.BufferedOutputStream; |
|
32 |
import java.io.BufferedReader; |
|
33 |
import java.io.BufferedWriter; |
|
34 |
import java.io.File; |
|
35 |
import java.io.FileOutputStream; |
|
36 |
import java.io.InputStream; |
|
37 |
import java.io.InputStreamReader; |
|
38 |
import java.io.OutputStream; |
|
39 |
import java.io.OutputStreamWriter; |
|
40 |
import java.io.IOException; |
|
41 |
import java.io.Reader; |
|
42 |
import java.io.UnsupportedEncodingException; |
|
43 |
import java.util.Vector; |
|
44 |
||
45 |
import javax.print.CancelablePrintJob; |
|
46 |
import javax.print.Doc; |
|
47 |
import javax.print.DocFlavor; |
|
48 |
import javax.print.DocPrintJob; |
|
49 |
import javax.print.PrintService; |
|
50 |
import javax.print.PrintException; |
|
51 |
import javax.print.event.PrintJobEvent; |
|
52 |
import javax.print.event.PrintJobListener; |
|
53 |
import javax.print.event.PrintJobAttributeListener; |
|
54 |
||
55 |
import javax.print.attribute.Attribute; |
|
56 |
import javax.print.attribute.AttributeSet; |
|
57 |
import javax.print.attribute.AttributeSetUtilities; |
|
58 |
import javax.print.attribute.DocAttributeSet; |
|
59 |
import javax.print.attribute.HashPrintJobAttributeSet; |
|
60 |
import javax.print.attribute.HashPrintRequestAttributeSet; |
|
61 |
import javax.print.attribute.PrintJobAttribute; |
|
62 |
import javax.print.attribute.PrintJobAttributeSet; |
|
63 |
import javax.print.attribute.PrintRequestAttribute; |
|
64 |
import javax.print.attribute.PrintRequestAttributeSet; |
|
65 |
import javax.print.attribute.standard.Copies; |
|
66 |
import javax.print.attribute.standard.Destination; |
|
67 |
import javax.print.attribute.standard.DocumentName; |
|
68 |
import javax.print.attribute.standard.Fidelity; |
|
69 |
import javax.print.attribute.standard.JobName; |
|
70 |
import javax.print.attribute.standard.JobOriginatingUserName; |
|
71 |
import javax.print.attribute.standard.JobSheets; |
|
72 |
import javax.print.attribute.standard.Media; |
|
73 |
import javax.print.attribute.standard.MediaSize; |
|
74 |
import javax.print.attribute.standard.MediaSizeName; |
|
75 |
import javax.print.attribute.standard.OrientationRequested; |
|
76 |
import javax.print.attribute.standard.RequestingUserName; |
|
77 |
import javax.print.attribute.standard.NumberUp; |
|
78 |
import javax.print.attribute.standard.Sides; |
|
79 |
import javax.print.attribute.standard.PrinterIsAcceptingJobs; |
|
80 |
||
81 |
import java.awt.print.*; |
|
82 |
||
83 |
||
84 |
||
85 |
public class UnixPrintJob implements CancelablePrintJob { |
|
86 |
private static String debugPrefix = "UnixPrintJob>> "; |
|
87 |
||
88 |
transient private Vector jobListeners; |
|
89 |
transient private Vector attrListeners; |
|
90 |
transient private Vector listenedAttributeSets; |
|
91 |
||
92 |
private PrintService service; |
|
93 |
private boolean fidelity; |
|
94 |
private boolean printing = false; |
|
95 |
private boolean printReturned = false; |
|
96 |
private PrintRequestAttributeSet reqAttrSet = null; |
|
97 |
private PrintJobAttributeSet jobAttrSet = null; |
|
98 |
private PrinterJob job; |
|
99 |
private Doc doc; |
|
100 |
/* these variables used globally to store reference to the print |
|
101 |
* data retrieved as a stream. On completion these are always closed |
|
102 |
* if non-null. |
|
103 |
*/ |
|
104 |
private InputStream instream = null; |
|
105 |
private Reader reader = null; |
|
106 |
||
107 |
/* default values overridden by those extracted from the attributes */ |
|
108 |
private String jobName = "Java Printing"; |
|
109 |
private int copies = 1; |
|
110 |
private MediaSizeName mediaName = MediaSizeName.NA_LETTER; |
|
111 |
private MediaSize mediaSize = MediaSize.NA.LETTER; |
|
112 |
private CustomMediaTray customTray = null; |
|
113 |
private OrientationRequested orient = OrientationRequested.PORTRAIT; |
|
114 |
private NumberUp nUp = null; |
|
115 |
private Sides sides = null; |
|
116 |
||
117 |
UnixPrintJob(PrintService service) { |
|
118 |
this.service = service; |
|
119 |
mDestination = service.getName(); |
|
120 |
mDestType = UnixPrintJob.DESTPRINTER; |
|
121 |
} |
|
122 |
||
123 |
public PrintService getPrintService() { |
|
124 |
return service; |
|
125 |
} |
|
126 |
||
127 |
public PrintJobAttributeSet getAttributes() { |
|
128 |
synchronized (this) { |
|
129 |
if (jobAttrSet == null) { |
|
130 |
/* just return an empty set until the job is submitted */ |
|
131 |
PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet(); |
|
132 |
return AttributeSetUtilities.unmodifiableView(jobSet); |
|
133 |
} else { |
|
134 |
return jobAttrSet; |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
public void addPrintJobListener(PrintJobListener listener) { |
|
140 |
synchronized (this) { |
|
141 |
if (listener == null) { |
|
142 |
return; |
|
143 |
} |
|
144 |
if (jobListeners == null) { |
|
145 |
jobListeners = new Vector(); |
|
146 |
} |
|
147 |
jobListeners.add(listener); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
public void removePrintJobListener(PrintJobListener listener) { |
|
152 |
synchronized (this) { |
|
153 |
if (listener == null || jobListeners == null ) { |
|
154 |
return; |
|
155 |
} |
|
156 |
jobListeners.remove(listener); |
|
157 |
if (jobListeners.isEmpty()) { |
|
158 |
jobListeners = null; |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
||
164 |
/* Closes any stream already retrieved for the data. |
|
165 |
* We want to avoid unnecessarily asking the Doc to create a stream only |
|
166 |
* to get a reference in order to close it because the job failed. |
|
167 |
* If the representation class is itself a "stream", this |
|
168 |
* closes that stream too. |
|
169 |
*/ |
|
170 |
private void closeDataStreams() { |
|
171 |
||
172 |
if (doc == null) { |
|
173 |
return; |
|
174 |
} |
|
175 |
||
176 |
Object data = null; |
|
177 |
||
178 |
try { |
|
179 |
data = doc.getPrintData(); |
|
180 |
} catch (IOException e) { |
|
181 |
return; |
|
182 |
} |
|
183 |
||
184 |
if (instream != null) { |
|
185 |
try { |
|
186 |
instream.close(); |
|
187 |
} catch (IOException e) { |
|
188 |
} finally { |
|
189 |
instream = null; |
|
190 |
} |
|
191 |
} |
|
192 |
else if (reader != null) { |
|
193 |
try { |
|
194 |
reader.close(); |
|
195 |
} catch (IOException e) { |
|
196 |
} finally { |
|
197 |
reader = null; |
|
198 |
} |
|
199 |
} |
|
200 |
else if (data instanceof InputStream) { |
|
201 |
try { |
|
202 |
((InputStream)data).close(); |
|
203 |
} catch (IOException e) { |
|
204 |
} |
|
205 |
} |
|
206 |
else if (data instanceof Reader) { |
|
207 |
try { |
|
208 |
((Reader)data).close(); |
|
209 |
} catch (IOException e) { |
|
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
private void notifyEvent(int reason) { |
|
215 |
||
216 |
/* since this method should always get called, here's where |
|
217 |
* we will perform the clean up of any data stream supplied. |
|
218 |
*/ |
|
219 |
switch (reason) { |
|
220 |
case PrintJobEvent.DATA_TRANSFER_COMPLETE: |
|
221 |
case PrintJobEvent.JOB_CANCELED : |
|
222 |
case PrintJobEvent.JOB_FAILED : |
|
223 |
case PrintJobEvent.NO_MORE_EVENTS : |
|
224 |
case PrintJobEvent.JOB_COMPLETE : |
|
225 |
closeDataStreams(); |
|
226 |
} |
|
227 |
||
228 |
synchronized (this) { |
|
229 |
if (jobListeners != null) { |
|
230 |
PrintJobListener listener; |
|
231 |
PrintJobEvent event = new PrintJobEvent(this, reason); |
|
232 |
for (int i = 0; i < jobListeners.size(); i++) { |
|
233 |
listener = (PrintJobListener)(jobListeners.elementAt(i)); |
|
234 |
switch (reason) { |
|
235 |
||
236 |
case PrintJobEvent.JOB_CANCELED : |
|
237 |
listener.printJobCanceled(event); |
|
238 |
break; |
|
239 |
||
240 |
case PrintJobEvent.JOB_FAILED : |
|
241 |
listener.printJobFailed(event); |
|
242 |
break; |
|
243 |
||
244 |
case PrintJobEvent.DATA_TRANSFER_COMPLETE : |
|
245 |
listener.printDataTransferCompleted(event); |
|
246 |
break; |
|
247 |
||
248 |
case PrintJobEvent.NO_MORE_EVENTS : |
|
249 |
listener.printJobNoMoreEvents(event); |
|
250 |
break; |
|
251 |
||
252 |
default: |
|
253 |
break; |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
public void addPrintJobAttributeListener( |
|
261 |
PrintJobAttributeListener listener, |
|
262 |
PrintJobAttributeSet attributes) { |
|
263 |
synchronized (this) { |
|
264 |
if (listener == null) { |
|
265 |
return; |
|
266 |
} |
|
267 |
if (attrListeners == null) { |
|
268 |
attrListeners = new Vector(); |
|
269 |
listenedAttributeSets = new Vector(); |
|
270 |
} |
|
271 |
attrListeners.add(listener); |
|
272 |
if (attributes == null) { |
|
273 |
attributes = new HashPrintJobAttributeSet(); |
|
274 |
} |
|
275 |
listenedAttributeSets.add(attributes); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
public void removePrintJobAttributeListener( |
|
280 |
PrintJobAttributeListener listener) { |
|
281 |
synchronized (this) { |
|
282 |
if (listener == null || attrListeners == null ) { |
|
283 |
return; |
|
284 |
} |
|
285 |
int index = attrListeners.indexOf(listener); |
|
286 |
if (index == -1) { |
|
287 |
return; |
|
288 |
} else { |
|
289 |
attrListeners.remove(index); |
|
290 |
listenedAttributeSets.remove(index); |
|
291 |
if (attrListeners.isEmpty()) { |
|
292 |
attrListeners = null; |
|
293 |
listenedAttributeSets = null; |
|
294 |
} |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
public void print(Doc doc, PrintRequestAttributeSet attributes) |
|
300 |
throws PrintException { |
|
301 |
||
302 |
synchronized (this) { |
|
303 |
if (printing) { |
|
304 |
throw new PrintException("already printing"); |
|
305 |
} else { |
|
306 |
printing = true; |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
if ((PrinterIsAcceptingJobs)(service.getAttribute( |
|
311 |
PrinterIsAcceptingJobs.class)) == |
|
312 |
PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) { |
|
313 |
throw new PrintException("Printer is not accepting job."); |
|
314 |
} |
|
315 |
||
316 |
this.doc = doc; |
|
317 |
/* check if the parameters are valid before doing much processing */ |
|
318 |
DocFlavor flavor = doc.getDocFlavor(); |
|
319 |
||
320 |
Object data; |
|
321 |
||
322 |
try { |
|
323 |
data = doc.getPrintData(); |
|
324 |
} catch (IOException e) { |
|
325 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
326 |
throw new PrintException("can't get print data: " + e.toString()); |
|
327 |
} |
|
328 |
||
329 |
if (flavor == null || (!service.isDocFlavorSupported(flavor))) { |
|
330 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
331 |
throw new PrintJobFlavorException("invalid flavor", flavor); |
|
332 |
} |
|
333 |
||
334 |
initializeAttributeSets(doc, attributes); |
|
335 |
||
336 |
getAttributeValues(flavor); |
|
337 |
||
338 |
// set up mOptions |
|
339 |
if ((service instanceof IPPPrintService) && |
|
340 |
CUPSPrinter.isCupsRunning()) { |
|
341 |
||
342 |
IPPPrintService.debug_println(debugPrefix+ |
|
343 |
"instanceof IPPPrintService"); |
|
344 |
||
345 |
if (mediaName != null) { |
|
346 |
CustomMediaSizeName customMedia = |
|
347 |
((IPPPrintService)service).findCustomMedia(mediaName); |
|
348 |
if (customMedia != null) { |
|
349 |
mOptions = " media="+ customMedia.getChoiceName(); |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
if (customTray != null && |
|
354 |
customTray instanceof CustomMediaTray) { |
|
355 |
String choice = customTray.getChoiceName(); |
|
356 |
if (choice != null) { |
|
357 |
mOptions += " media="+choice; |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
if (nUp != null) { |
|
362 |
mOptions += " number-up="+nUp.getValue(); |
|
363 |
} |
|
364 |
||
365 |
if (orient == OrientationRequested.LANDSCAPE && |
|
366 |
(flavor != null) && |
|
367 |
!flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) { |
|
368 |
mOptions += " landscape"; |
|
369 |
} |
|
370 |
||
371 |
if (sides != null) { |
|
372 |
mOptions += " sides="+sides; |
|
373 |
} |
|
374 |
||
375 |
} |
|
376 |
||
377 |
IPPPrintService.debug_println(debugPrefix+"mOptions "+mOptions); |
|
378 |
String repClassName = flavor.getRepresentationClassName(); |
|
379 |
String val = flavor.getParameter("charset"); |
|
380 |
String encoding = "us-ascii"; |
|
381 |
if (val != null && !val.equals("")) { |
|
382 |
encoding = val; |
|
383 |
} |
|
384 |
||
385 |
if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || |
|
386 |
flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || |
|
387 |
flavor.equals(DocFlavor.INPUT_STREAM.PNG) || |
|
388 |
flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || |
|
389 |
flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || |
|
390 |
flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) { |
|
391 |
try { |
|
392 |
instream = doc.getStreamForBytes(); |
|
393 |
if (instream == null) { |
|
394 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
395 |
throw new PrintException("No stream for data"); |
|
396 |
} |
|
397 |
if (!(service instanceof IPPPrintService && |
|
398 |
((IPPPrintService)service).isIPPSupportedImages( |
|
399 |
flavor.getMimeType()))) { |
|
400 |
printableJob(new ImagePrinter(instream)); |
|
401 |
((UnixPrintService)service).wakeNotifier(); |
|
402 |
return; |
|
403 |
} |
|
404 |
} catch (ClassCastException cce) { |
|
405 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
406 |
throw new PrintException(cce); |
|
407 |
} catch (IOException ioe) { |
|
408 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
409 |
throw new PrintException(ioe); |
|
410 |
} |
|
411 |
} else if (flavor.equals(DocFlavor.URL.GIF) || |
|
412 |
flavor.equals(DocFlavor.URL.JPEG) || |
|
413 |
flavor.equals(DocFlavor.URL.PNG)) { |
|
414 |
try { |
|
415 |
URL url = (URL)data; |
|
416 |
if ((service instanceof IPPPrintService) && |
|
417 |
((IPPPrintService)service).isIPPSupportedImages( |
|
418 |
flavor.getMimeType())) { |
|
419 |
instream = url.openStream(); |
|
420 |
} else { |
|
421 |
printableJob(new ImagePrinter(url)); |
|
422 |
((UnixPrintService)service).wakeNotifier(); |
|
423 |
return; |
|
424 |
} |
|
425 |
} catch (ClassCastException cce) { |
|
426 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
427 |
throw new PrintException(cce); |
|
428 |
} catch (IOException e) { |
|
429 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
430 |
throw new PrintException(e.toString()); |
|
431 |
} |
|
432 |
} else if (flavor.equals(DocFlavor.CHAR_ARRAY.TEXT_PLAIN) || |
|
433 |
flavor.equals(DocFlavor.READER.TEXT_PLAIN) || |
|
434 |
flavor.equals(DocFlavor.STRING.TEXT_PLAIN)) { |
|
435 |
try { |
|
436 |
reader = doc.getReaderForText(); |
|
437 |
if (reader == null) { |
|
438 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
439 |
throw new PrintException("No reader for data"); |
|
440 |
} |
|
441 |
} catch (IOException ioe) { |
|
442 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
443 |
throw new PrintException(ioe.toString()); |
|
444 |
} |
|
445 |
} else if (repClassName.equals("[B") || |
|
446 |
repClassName.equals("java.io.InputStream")) { |
|
447 |
try { |
|
448 |
instream = doc.getStreamForBytes(); |
|
449 |
if (instream == null) { |
|
450 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
451 |
throw new PrintException("No stream for data"); |
|
452 |
} |
|
453 |
} catch (IOException ioe) { |
|
454 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
455 |
throw new PrintException(ioe.toString()); |
|
456 |
} |
|
457 |
} else if (repClassName.equals("java.net.URL")) { |
|
458 |
/* |
|
459 |
* This extracts the data from the URL and passes it the content |
|
460 |
* directly to the print service as a file. |
|
461 |
* This is appropriate for the current implementation where lp or |
|
462 |
* lpr is always used to spool the data. We expect to revise the |
|
463 |
* implementation to provide more complete IPP support (ie not just |
|
464 |
* CUPS) and at that time the job will be spooled via IPP |
|
465 |
* and the URL |
|
466 |
* itself should be sent to the IPP print service not the content. |
|
467 |
*/ |
|
468 |
URL url = (URL)data; |
|
469 |
try { |
|
470 |
instream = url.openStream(); |
|
471 |
} catch (IOException e) { |
|
472 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
473 |
throw new PrintException(e.toString()); |
|
474 |
} |
|
475 |
} else if (repClassName.equals("java.awt.print.Pageable")) { |
|
476 |
try { |
|
477 |
pageableJob((Pageable)doc.getPrintData()); |
|
478 |
if (service instanceof IPPPrintService) { |
|
479 |
((IPPPrintService)service).wakeNotifier(); |
|
480 |
} else { |
|
481 |
((UnixPrintService)service).wakeNotifier(); |
|
482 |
} |
|
483 |
return; |
|
484 |
} catch (ClassCastException cce) { |
|
485 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
486 |
throw new PrintException(cce); |
|
487 |
} catch (IOException ioe) { |
|
488 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
489 |
throw new PrintException(ioe); |
|
490 |
} |
|
491 |
} else if (repClassName.equals("java.awt.print.Printable")) { |
|
492 |
try { |
|
493 |
printableJob((Printable)doc.getPrintData()); |
|
494 |
if (service instanceof IPPPrintService) { |
|
495 |
((IPPPrintService)service).wakeNotifier(); |
|
496 |
} else { |
|
497 |
((UnixPrintService)service).wakeNotifier(); |
|
498 |
} |
|
499 |
return; |
|
500 |
} catch (ClassCastException cce) { |
|
501 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
502 |
throw new PrintException(cce); |
|
503 |
} catch (IOException ioe) { |
|
504 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
505 |
throw new PrintException(ioe); |
|
506 |
} |
|
507 |
} else { |
|
508 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
509 |
throw new PrintException("unrecognized class: "+repClassName); |
|
510 |
} |
|
511 |
||
512 |
// now spool the print data. |
|
513 |
PrinterOpener po = new PrinterOpener(); |
|
514 |
java.security.AccessController.doPrivileged(po); |
|
515 |
if (po.pex != null) { |
|
516 |
throw po.pex; |
|
517 |
} |
|
518 |
OutputStream output = po.result; |
|
519 |
||
520 |
/* There are three cases: |
|
521 |
* 1) Text data from a Reader, just pass through. |
|
522 |
* 2) Text data from an input stream which we must read using the |
|
523 |
* correct encoding |
|
524 |
* 3) Raw byte data from an InputStream we don't interpret as text, |
|
525 |
* just pass through: eg postscript. |
|
526 |
*/ |
|
527 |
||
528 |
BufferedWriter bw = null; |
|
529 |
if ((instream == null && reader != null)) { |
|
530 |
BufferedReader br = new BufferedReader(reader); |
|
531 |
OutputStreamWriter osw = new OutputStreamWriter(output); |
|
532 |
bw = new BufferedWriter(osw); |
|
533 |
char []buffer = new char[1024]; |
|
534 |
int cread; |
|
535 |
||
536 |
try { |
|
537 |
while ((cread = br.read(buffer, 0, buffer.length)) >=0) { |
|
538 |
bw.write(buffer, 0, cread); |
|
539 |
} |
|
540 |
br.close(); |
|
541 |
bw.flush(); |
|
542 |
bw.close(); |
|
543 |
} catch (IOException e) { |
|
544 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
545 |
throw new PrintException (e); |
|
546 |
} |
|
547 |
} else if (instream != null && |
|
548 |
flavor.getMediaType().equalsIgnoreCase("text")) { |
|
549 |
try { |
|
550 |
||
551 |
InputStreamReader isr = new InputStreamReader(instream, |
|
552 |
encoding); |
|
553 |
BufferedReader br = new BufferedReader(isr); |
|
554 |
OutputStreamWriter osw = new OutputStreamWriter(output); |
|
555 |
bw = new BufferedWriter(osw); |
|
556 |
char []buffer = new char[1024]; |
|
557 |
int cread; |
|
558 |
||
559 |
while ((cread = br.read(buffer, 0, buffer.length)) >=0) { |
|
560 |
bw.write(buffer, 0, cread); |
|
561 |
} |
|
562 |
bw.flush(); |
|
563 |
} catch (IOException e) { |
|
564 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
565 |
throw new PrintException (e); |
|
566 |
} finally { |
|
567 |
try { |
|
568 |
if (bw != null) { |
|
569 |
bw.close(); |
|
570 |
} |
|
571 |
} catch (IOException e) { |
|
572 |
} |
|
573 |
} |
|
574 |
} else if (instream != null) { |
|
575 |
BufferedInputStream bin = new BufferedInputStream(instream); |
|
576 |
BufferedOutputStream bout = new BufferedOutputStream(output); |
|
577 |
byte[] buffer = new byte[1024]; |
|
578 |
int bread = 0; |
|
579 |
||
580 |
try { |
|
581 |
while ((bread = bin.read(buffer)) >= 0) { |
|
582 |
bout.write(buffer, 0, bread); |
|
583 |
} |
|
584 |
bin.close(); |
|
585 |
bout.flush(); |
|
586 |
bout.close(); |
|
587 |
} catch (IOException e) { |
|
588 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
589 |
throw new PrintException (e); |
|
590 |
} |
|
591 |
} |
|
592 |
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); |
|
593 |
||
594 |
if (mDestType == UnixPrintJob.DESTPRINTER) { |
|
595 |
PrinterSpooler spooler = new PrinterSpooler(); |
|
596 |
java.security.AccessController.doPrivileged(spooler); |
|
597 |
if (spooler.pex != null) { |
|
598 |
throw spooler.pex; |
|
599 |
} |
|
600 |
} |
|
601 |
notifyEvent(PrintJobEvent.NO_MORE_EVENTS); |
|
602 |
if (service instanceof IPPPrintService) { |
|
603 |
((IPPPrintService)service).wakeNotifier(); |
|
604 |
} else { |
|
605 |
((UnixPrintService)service).wakeNotifier(); |
|
606 |
} |
|
607 |
} |
|
608 |
||
609 |
public void printableJob(Printable printable) throws PrintException { |
|
610 |
try { |
|
611 |
synchronized(this) { |
|
612 |
if (job != null) { // shouldn't happen |
|
613 |
throw new PrintException("already printing"); |
|
614 |
} else { |
|
615 |
job = new PSPrinterJob(); |
|
616 |
} |
|
617 |
} |
|
618 |
job.setPrintService(getPrintService()); |
|
619 |
job.setCopies(copies); |
|
620 |
job.setJobName(jobName); |
|
621 |
PageFormat pf = new PageFormat(); |
|
622 |
if (mediaSize != null) { |
|
623 |
Paper p = new Paper(); |
|
624 |
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0, |
|
625 |
mediaSize.getY(MediaSize.INCH)*72.0); |
|
626 |
p.setImageableArea(72.0, 72.0, p.getWidth()-144.0, |
|
627 |
p.getHeight()-144.0); |
|
628 |
pf.setPaper(p); |
|
629 |
} |
|
630 |
if (orient == OrientationRequested.REVERSE_LANDSCAPE) { |
|
631 |
pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); |
|
632 |
} else if (orient == OrientationRequested.LANDSCAPE) { |
|
633 |
pf.setOrientation(PageFormat.LANDSCAPE); |
|
634 |
} |
|
635 |
job.setPrintable(printable, pf); |
|
636 |
job.print(reqAttrSet); |
|
637 |
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); |
|
638 |
return; |
|
639 |
} catch (PrinterException pe) { |
|
640 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
641 |
throw new PrintException(pe); |
|
642 |
} finally { |
|
643 |
printReturned = true; |
|
644 |
notifyEvent(PrintJobEvent.NO_MORE_EVENTS); |
|
645 |
} |
|
646 |
} |
|
647 |
||
648 |
public void pageableJob(Pageable pageable) throws PrintException { |
|
649 |
try { |
|
650 |
synchronized(this) { |
|
651 |
if (job != null) { // shouldn't happen |
|
652 |
throw new PrintException("already printing"); |
|
653 |
} else { |
|
654 |
job = new PSPrinterJob(); |
|
655 |
} |
|
656 |
} |
|
657 |
job.setPrintService(getPrintService()); |
|
658 |
job.setCopies(copies); |
|
659 |
job.setJobName(jobName); |
|
660 |
job.setPageable(pageable); |
|
661 |
job.print(reqAttrSet); |
|
662 |
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); |
|
663 |
return; |
|
664 |
} catch (PrinterException pe) { |
|
665 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
666 |
throw new PrintException(pe); |
|
667 |
} finally { |
|
668 |
printReturned = true; |
|
669 |
notifyEvent(PrintJobEvent.NO_MORE_EVENTS); |
|
670 |
} |
|
671 |
} |
|
672 |
/* There's some inefficiency here as the job set is created even though |
|
673 |
* it may never be requested. |
|
674 |
*/ |
|
675 |
private synchronized void |
|
676 |
initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) { |
|
677 |
||
678 |
reqAttrSet = new HashPrintRequestAttributeSet(); |
|
679 |
jobAttrSet = new HashPrintJobAttributeSet(); |
|
680 |
||
681 |
Attribute[] attrs; |
|
682 |
if (reqSet != null) { |
|
683 |
reqAttrSet.addAll(reqSet); |
|
684 |
attrs = reqSet.toArray(); |
|
685 |
for (int i=0; i<attrs.length; i++) { |
|
686 |
if (attrs[i] instanceof PrintJobAttribute) { |
|
687 |
jobAttrSet.add(attrs[i]); |
|
688 |
} |
|
689 |
} |
|
690 |
} |
|
691 |
||
692 |
DocAttributeSet docSet = doc.getAttributes(); |
|
693 |
if (docSet != null) { |
|
694 |
attrs = docSet.toArray(); |
|
695 |
for (int i=0; i<attrs.length; i++) { |
|
696 |
if (attrs[i] instanceof PrintRequestAttribute) { |
|
697 |
reqAttrSet.add(attrs[i]); |
|
698 |
} |
|
699 |
if (attrs[i] instanceof PrintJobAttribute) { |
|
700 |
jobAttrSet.add(attrs[i]); |
|
701 |
} |
|
702 |
} |
|
703 |
} |
|
704 |
||
705 |
/* add the user name to the job */ |
|
706 |
String userName = ""; |
|
707 |
try { |
|
708 |
userName = System.getProperty("user.name"); |
|
709 |
} catch (SecurityException se) { |
|
710 |
} |
|
711 |
||
712 |
if (userName == null || userName.equals("")) { |
|
713 |
RequestingUserName ruName = |
|
714 |
(RequestingUserName)reqSet.get(RequestingUserName.class); |
|
715 |
if (ruName != null) { |
|
716 |
jobAttrSet.add( |
|
717 |
new JobOriginatingUserName(ruName.getValue(), |
|
718 |
ruName.getLocale())); |
|
719 |
} else { |
|
720 |
jobAttrSet.add(new JobOriginatingUserName("", null)); |
|
721 |
} |
|
722 |
} else { |
|
723 |
jobAttrSet.add(new JobOriginatingUserName(userName, null)); |
|
724 |
} |
|
725 |
||
726 |
/* if no job name supplied use doc name (if supplied), if none and |
|
727 |
* its a URL use that, else finally anything .. */ |
|
728 |
if (jobAttrSet.get(JobName.class) == null) { |
|
729 |
JobName jobName; |
|
730 |
if (docSet != null && docSet.get(DocumentName.class) != null) { |
|
731 |
DocumentName docName = |
|
732 |
(DocumentName)docSet.get(DocumentName.class); |
|
733 |
jobName = new JobName(docName.getValue(), docName.getLocale()); |
|
734 |
jobAttrSet.add(jobName); |
|
735 |
} else { |
|
736 |
String str = "JPS Job:" + doc; |
|
737 |
try { |
|
738 |
Object printData = doc.getPrintData(); |
|
739 |
if (printData instanceof URL) { |
|
740 |
str = ((URL)(doc.getPrintData())).toString(); |
|
741 |
} |
|
742 |
} catch (IOException e) { |
|
743 |
} |
|
744 |
jobName = new JobName(str, null); |
|
745 |
jobAttrSet.add(jobName); |
|
746 |
} |
|
747 |
} |
|
748 |
||
749 |
jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet); |
|
750 |
} |
|
751 |
||
752 |
private void getAttributeValues(DocFlavor flavor) throws PrintException { |
|
753 |
Attribute attr; |
|
754 |
Class category; |
|
755 |
||
756 |
if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) { |
|
757 |
fidelity = true; |
|
758 |
} else { |
|
759 |
fidelity = false; |
|
760 |
} |
|
761 |
||
762 |
Attribute []attrs = reqAttrSet.toArray(); |
|
763 |
for (int i=0; i<attrs.length; i++) { |
|
764 |
attr = attrs[i]; |
|
765 |
category = attr.getCategory(); |
|
766 |
if (fidelity == true) { |
|
767 |
if (!service.isAttributeCategorySupported(category)) { |
|
768 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
769 |
throw new PrintJobAttributeException( |
|
770 |
"unsupported category: " + category, category, null); |
|
771 |
} else if |
|
772 |
(!service.isAttributeValueSupported(attr, flavor, null)) { |
|
773 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
774 |
throw new PrintJobAttributeException( |
|
775 |
"unsupported attribute: " + attr, null, attr); |
|
776 |
} |
|
777 |
} |
|
778 |
if (category == Destination.class) { |
|
779 |
URI uri = ((Destination)attr).getURI(); |
|
780 |
if (!"file".equals(uri.getScheme())) { |
|
781 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
782 |
throw new PrintException("Not a file: URI"); |
|
783 |
} else { |
|
784 |
try { |
|
785 |
mDestType = DESTFILE; |
|
786 |
mDestination = (new File(uri)).getPath(); |
|
787 |
} catch (Exception e) { |
|
788 |
throw new PrintException(e); |
|
789 |
} |
|
790 |
// check write access |
|
791 |
SecurityManager security = System.getSecurityManager(); |
|
792 |
if (security != null) { |
|
793 |
try { |
|
794 |
security.checkWrite(mDestination); |
|
795 |
} catch (SecurityException se) { |
|
796 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
797 |
throw new PrintException(se); |
|
798 |
} |
|
799 |
} |
|
800 |
} |
|
801 |
} else if (category == JobSheets.class) { |
|
802 |
if ((JobSheets)attr == JobSheets.NONE) { |
|
803 |
mNoJobSheet = true; |
|
804 |
} |
|
805 |
} else if (category == JobName.class) { |
|
806 |
jobName = ((JobName)attr).getValue(); |
|
807 |
} else if (category == Copies.class) { |
|
808 |
copies = ((Copies)attr).getValue(); |
|
809 |
} else if (category == Media.class) { |
|
810 |
if (attr instanceof MediaSizeName) { |
|
811 |
mediaName = (MediaSizeName)attr; |
|
812 |
IPPPrintService.debug_println(debugPrefix+ |
|
813 |
"mediaName "+mediaName); |
|
814 |
if (!service.isAttributeValueSupported(attr, null, null)) { |
|
815 |
mediaSize = MediaSize.getMediaSizeForName(mediaName); |
|
816 |
} |
|
817 |
} else if (attr instanceof CustomMediaTray) { |
|
818 |
customTray = (CustomMediaTray)attr; |
|
819 |
} |
|
820 |
} else if (category == OrientationRequested.class) { |
|
821 |
orient = (OrientationRequested)attr; |
|
822 |
} else if (category == NumberUp.class) { |
|
823 |
nUp = (NumberUp)attr; |
|
824 |
} else if (category == Sides.class) { |
|
825 |
sides = (Sides)attr; |
|
826 |
} |
|
827 |
} |
|
828 |
} |
|
829 |
||
830 |
private String[] printExecCmd(String printer, String options, |
|
831 |
boolean noJobSheet, |
|
832 |
String banner, int copies, String spoolFile) { |
|
833 |
int PRINTER = 0x1; |
|
834 |
int OPTIONS = 0x2; |
|
835 |
int BANNER = 0x4; |
|
836 |
int COPIES = 0x8; |
|
837 |
int NOSHEET = 0x10; |
|
838 |
int pFlags = 0; |
|
839 |
String execCmd[]; |
|
840 |
int ncomps = 2; // minimum number of print args |
|
841 |
int n = 0; |
|
842 |
||
843 |
// conveniently "lp" is the default destination for both lp and lpr. |
|
844 |
if (printer != null && !printer.equals("") && !printer.equals("lp")) { |
|
845 |
pFlags |= PRINTER; |
|
846 |
ncomps+=1; |
|
847 |
} |
|
848 |
if (options != null && !options.equals("")) { |
|
849 |
pFlags |= OPTIONS; |
|
850 |
ncomps+=1; |
|
851 |
} |
|
852 |
if (banner != null && !banner.equals("")) { |
|
853 |
pFlags |= BANNER; |
|
854 |
ncomps+=1; |
|
855 |
} |
|
856 |
if (copies > 1) { |
|
857 |
pFlags |= COPIES; |
|
858 |
ncomps+=1; |
|
859 |
} |
|
860 |
if (noJobSheet) { |
|
861 |
pFlags |= NOSHEET; |
|
862 |
ncomps+=1; |
|
863 |
} |
|
864 |
if (UnixPrintServiceLookup.osname.equals("SunOS")) { |
|
865 |
ncomps+=1; // lp uses 1 more arg than lpr (make a copy) |
|
866 |
execCmd = new String[ncomps]; |
|
867 |
execCmd[n++] = "/usr/bin/lp"; |
|
868 |
execCmd[n++] = "-c"; // make a copy of the spool file |
|
869 |
if ((pFlags & PRINTER) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
870 |
execCmd[n++] = "-d" + printer; |
2 | 871 |
} |
872 |
if ((pFlags & BANNER) != 0) { |
|
873 |
String quoteChar = "\""; |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
874 |
execCmd[n++] = "-t " + quoteChar+banner+quoteChar; |
2 | 875 |
} |
876 |
if ((pFlags & COPIES) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
877 |
execCmd[n++] = "-n " + copies; |
2 | 878 |
} |
879 |
if ((pFlags & NOSHEET) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
880 |
execCmd[n++] = "-o nobanner"; |
2 | 881 |
} |
882 |
if ((pFlags & OPTIONS) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
883 |
execCmd[n++] = "-o " + options; |
2 | 884 |
} |
885 |
} else { |
|
886 |
execCmd = new String[ncomps]; |
|
887 |
execCmd[n++] = "/usr/bin/lpr"; |
|
888 |
if ((pFlags & PRINTER) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
889 |
execCmd[n++] = "-P" + printer; |
2 | 890 |
} |
891 |
if ((pFlags & BANNER) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
892 |
execCmd[n++] = "-J " + banner; |
2 | 893 |
} |
894 |
if ((pFlags & COPIES) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
895 |
execCmd[n++] = "-#" + copies; |
2 | 896 |
} |
897 |
if ((pFlags & NOSHEET) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
898 |
execCmd[n++] = "-h"; |
2 | 899 |
} |
900 |
if ((pFlags & OPTIONS) != 0) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
901 |
execCmd[n++] = "-o" + options; |
2 | 902 |
} |
903 |
} |
|
904 |
execCmd[n++] = spoolFile; |
|
905 |
if (IPPPrintService.debugPrint) { |
|
906 |
System.out.println("UnixPrintJob>> execCmd"); |
|
907 |
for (int i=0; i<execCmd.length; i++) { |
|
908 |
System.out.print(" "+execCmd[i]); |
|
909 |
} |
|
910 |
System.out.println(); |
|
911 |
} |
|
912 |
return execCmd; |
|
913 |
} |
|
914 |
||
915 |
private static int DESTPRINTER = 1; |
|
916 |
private static int DESTFILE = 2; |
|
917 |
private int mDestType = DESTPRINTER; |
|
918 |
||
919 |
private File spoolFile; |
|
920 |
private String mDestination, mOptions=""; |
|
921 |
private boolean mNoJobSheet = false; |
|
922 |
||
923 |
// Inner class to run "privileged" to open the printer output stream. |
|
924 |
||
925 |
private class PrinterOpener implements java.security.PrivilegedAction { |
|
926 |
PrintException pex; |
|
927 |
OutputStream result; |
|
928 |
||
929 |
public Object run() { |
|
930 |
try { |
|
931 |
if (mDestType == UnixPrintJob.DESTFILE) { |
|
932 |
spoolFile = new File(mDestination); |
|
933 |
} else { |
|
934 |
/* Write to a temporary file which will be spooled to |
|
935 |
* the printer then deleted. In the case that the file |
|
936 |
* is not removed for some reason, request that it is |
|
937 |
* removed when the VM exits. |
|
938 |
*/ |
|
939 |
spoolFile = File.createTempFile("javaprint", ".ps", null); |
|
940 |
spoolFile.deleteOnExit(); |
|
941 |
} |
|
942 |
result = new FileOutputStream(spoolFile); |
|
943 |
return result; |
|
944 |
} catch (IOException ex) { |
|
945 |
// If there is an IOError we subvert it to a PrinterException. |
|
946 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
947 |
pex = new PrintException(ex); |
|
948 |
} |
|
949 |
return null; |
|
950 |
} |
|
951 |
} |
|
952 |
||
953 |
// Inner class to run "privileged" to invoke the system print command |
|
954 |
||
955 |
private class PrinterSpooler implements java.security.PrivilegedAction { |
|
956 |
PrintException pex; |
|
957 |
||
958 |
public Object run() { |
|
959 |
try { |
|
960 |
/** |
|
961 |
* Spool to the printer. |
|
962 |
*/ |
|
963 |
if (spoolFile == null || !spoolFile.exists()) { |
|
964 |
pex = new PrintException("No spool file"); |
|
965 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
966 |
return null; |
|
967 |
} |
|
968 |
String fileName = spoolFile.getAbsolutePath(); |
|
969 |
String execCmd[] = printExecCmd(mDestination, mOptions, |
|
970 |
mNoJobSheet, jobName, copies, fileName); |
|
971 |
||
972 |
Process process = Runtime.getRuntime().exec(execCmd); |
|
973 |
process.waitFor(); |
|
974 |
spoolFile.delete(); |
|
975 |
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); |
|
976 |
} catch (IOException ex) { |
|
977 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
978 |
// REMIND : 2d printing throws PrinterException |
|
979 |
pex = new PrintException(ex); |
|
980 |
} catch (InterruptedException ie) { |
|
981 |
notifyEvent(PrintJobEvent.JOB_FAILED); |
|
982 |
pex = new PrintException(ie); |
|
983 |
} finally { |
|
984 |
notifyEvent(PrintJobEvent.NO_MORE_EVENTS); |
|
985 |
} |
|
986 |
return null; |
|
987 |
} |
|
988 |
} |
|
989 |
||
990 |
public void cancel() throws PrintException { |
|
991 |
synchronized (this) { |
|
992 |
if (!printing) { |
|
993 |
throw new PrintException("Job is not yet submitted."); |
|
994 |
} else if (job != null && !printReturned) { |
|
995 |
job.cancel(); |
|
996 |
notifyEvent(PrintJobEvent.JOB_CANCELED); |
|
997 |
return; |
|
998 |
} else { |
|
999 |
throw new PrintException("Job could not be cancelled."); |
|
1000 |
} |
|
1001 |
} |
|
1002 |
} |
|
1003 |
} |