1 13 package org.ejbca.util; 14 15 import java.awt.print.PageFormat ; 16 import java.awt.print.Paper ; 17 import java.awt.print.PrinterException ; 18 import java.awt.print.PrinterJob ; 19 import java.io.IOException ; 20 import java.io.StringReader ; 21 22 import javax.ejb.EJBException ; 23 import javax.print.DocFlavor ; 24 import javax.print.PrintService ; 25 import javax.print.PrintServiceLookup ; 26 import javax.print.attribute.HashPrintRequestAttributeSet ; 27 import javax.print.attribute.PrintRequestAttributeSet ; 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 41 public class PrinterManager { 42 43 private static Logger log = Logger.getLogger(PrinterManager.class); 44 45 private static transient PrintService currentService = null; 46 private static transient String currentPrinterName = null; 47 private static transient String currentSVGTemplateName = null; 48 private static transient SVGImageManipulator sVGImagemanipulator = null; 49 50 54 55 public static String [] listPrinters(){ 56 String [] printerNames = new String [0]; 57 try { 58 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet (); 59 DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; 60 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); 61 printerNames = new String [printService.length]; 62 for(int i=0;i<printService.length;i++){ 63 printerNames[i] = printService[i].getName(); 64 } 65 } catch (Throwable e) { 66 log.error("Error looking up printers: ", e); 67 } 68 return printerNames; 69 } 70 71 72 73 96 public static void print(String printerName, String svtTemplateName, 97 String sVGData, int copies, 98 int visualVaildity, UserDataVO userDataVO, 99 String [] pINs, String [] pUKs, String hardTokenSerialPrefix, 100 String hardTokenSN, String copyOfHardTokenSN) throws PrinterException { 101 if(currentService == null 102 || currentPrinterName == null 103 || !printerName.equals(currentPrinterName)){ 104 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet (); 105 DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; 106 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); 107 int i = 0; 108 String 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 (sVGData),visualVaildity,hardTokenSerialPrefix); 118 } 119 120 if(currentService != null){ 121 PrinterJob job = PrinterJob.getPrinterJob(); 122 123 job.setPrintService(currentService); 124 PageFormat pf = job.defaultPage(); 125 Paper paper = new Paper (); 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 ("Error, couldn't find the right printer"); 135 } 136 }catch(IOException e){ 137 log.debug(e); 138 throw new PrinterException ("Error occured when processing the SVG data :" + e.getMessage()); 139 } 140 } 141 } 142 | Popular Tags |