KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > PrinterManager


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13 package org.ejbca.util;
14
15 import java.awt.print.PageFormat JavaDoc;
16 import java.awt.print.Paper JavaDoc;
17 import java.awt.print.PrinterException JavaDoc;
18 import java.awt.print.PrinterJob JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.StringReader JavaDoc;
21
22 import javax.ejb.EJBException JavaDoc;
23 import javax.print.DocFlavor JavaDoc;
24 import javax.print.PrintService JavaDoc;
25 import javax.print.PrintServiceLookup JavaDoc;
26 import javax.print.attribute.HashPrintRequestAttributeSet JavaDoc;
27 import javax.print.attribute.PrintRequestAttributeSet JavaDoc;
28
29 import org.apache.log4j.Logger;
30 import org.ejbca.core.model.hardtoken.profiles.SVGImageManipulator;
31 import org.ejbca.core.model.ra.UserDataVO;
32
33 /**
34  * Class managing EJBCA print functionality, such as listing printers
35  * and managing printjobs
36  *
37  * @author Philip Vendil 2006 sep 20
38  *
39  * @version $Id: PrinterManager.java,v 1.2.4.2 2007/02/26 12:06:34 anatom Exp $
40  */

41 public class PrinterManager {
42     
43     private static Logger log = Logger.getLogger(PrinterManager.class);
44
45     private static transient PrintService JavaDoc currentService = null;
46     private static transient String JavaDoc currentPrinterName = null;
47     private static transient String JavaDoc currentSVGTemplateName = null;
48     private static transient SVGImageManipulator sVGImagemanipulator = null;
49     
50     /**
51      * Method that returns the names of all the available printers.
52      * @return
53      */

54     
55     public static String JavaDoc[] listPrinters(){
56         String JavaDoc[] printerNames = new String JavaDoc[0];
57         try {
58             PrintRequestAttributeSet JavaDoc pras = new HashPrintRequestAttributeSet JavaDoc();
59             DocFlavor JavaDoc flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
60             PrintService JavaDoc printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
61             printerNames = new String JavaDoc[printService.length];
62             for(int i=0;i<printService.length;i++){
63                 printerNames[i] = printService[i].getName();
64             }
65         } catch (Throwable JavaDoc e) {
66             log.error("Error looking up printers: ", e);
67         }
68         return printerNames;
69     }
70
71
72     
73     /**
74      * Method performin the actual processing and
75      * printing of an SVG Template
76      *
77      * Imporant the method is caching template data
78      * so to subsekvent calls with the samt svtTemplate name
79      * will result in the same SVG data being processed.
80      * Avoid the same svgTemplate name for two different
81      * SVG files
82      *
83      * @param printerName the name of the printer to use
84      * @param svtTemplateName the filname of the SVG file.
85      * @param sVGData the actual SVG data.
86      * @param copies number of copies to print
87      * @param visualVaildity the visual validity use 0 if not used
88      * @param userDataVO the userdata
89      * @param pINs the PINS (or password)
90      * @param pUKs the PUKS, optional
91      * @param hardTokenSerialPrefix the prefix of the hardtoken, optional
92      * @param hardTokenSN, optional
93      * @param copyOfHardTokenSN, optional
94      * @throws PrinterException throws if any printer problems occur.
95      */

96     public static void print(String JavaDoc printerName, String JavaDoc svtTemplateName,
97             String JavaDoc sVGData, int copies,
98             int visualVaildity, UserDataVO userDataVO,
99             String JavaDoc[] pINs, String JavaDoc[] pUKs, String JavaDoc hardTokenSerialPrefix,
100             String JavaDoc hardTokenSN, String JavaDoc copyOfHardTokenSN) throws PrinterException JavaDoc{
101         if(currentService == null
102            || currentPrinterName == null
103            || !printerName.equals(currentPrinterName)){
104             PrintRequestAttributeSet JavaDoc pras = new HashPrintRequestAttributeSet JavaDoc();
105             DocFlavor JavaDoc flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
106             PrintService JavaDoc printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
107             int i = 0;
108             String JavaDoc trimemdPrinterName = printerName.trim();
109             while ( i<printService.length && !trimemdPrinterName.equalsIgnoreCase(printService[i].getName()) ){
110                 i++;
111             }
112             currentService = i<printService.length ? printService[i] : null;
113             currentPrinterName = printerName;
114         }
115         try{
116             if(currentSVGTemplateName == null || !currentSVGTemplateName.equals(svtTemplateName)){
117                 sVGImagemanipulator = new SVGImageManipulator(new StringReader JavaDoc(sVGData),visualVaildity,hardTokenSerialPrefix);
118             }
119
120             if(currentService != null){
121                 PrinterJob JavaDoc job = PrinterJob.getPrinterJob();
122
123                 job.setPrintService(currentService);
124                 PageFormat JavaDoc pf = job.defaultPage();
125                 Paper JavaDoc paper = new Paper JavaDoc();
126                 paper.setSize(pf.getWidth(), pf.getHeight());
127                 paper.setImageableArea(0.0,0.0,pf.getWidth(), pf.getHeight());
128
129                 pf.setPaper(paper);
130                 job.setPrintable(sVGImagemanipulator.print(userDataVO,pINs,pUKs,hardTokenSN, copyOfHardTokenSN),pf);
131                 job.setCopies(copies);
132                 job.print();
133             }else{
134                 throw new EJBException JavaDoc("Error, couldn't find the right printer");
135             }
136         }catch(IOException JavaDoc e){
137             log.debug(e);
138             throw new PrinterException JavaDoc("Error occured when processing the SVG data :" + e.getMessage());
139         }
140     }
141 }
142
Popular Tags