Search in sources :

Example 41 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class PSPrinterJob method endDoc.

/**
     * Invoked by the RasterPrintJob super class
     * this method is called after that last page
     * has been imaged.
     */
protected void endDoc() throws PrinterException {
    if (mPSStream != null) {
        mPSStream.println(EOF_COMMENT);
        mPSStream.flush();
        if (mDestType != RasterPrinterJob.STREAM) {
            mPSStream.close();
        }
    }
    if (mDestType == RasterPrinterJob.PRINTER) {
        PrintService pServ = getPrintService();
        if (pServ != null) {
            mDestination = pServ.getName();
            if (isMac) {
                PrintServiceAttributeSet psaSet = pServ.getAttributes();
                if (psaSet != null) {
                    mDestination = psaSet.get(PrinterName.class).toString();
                }
            }
        }
        PrinterSpooler spooler = new PrinterSpooler();
        java.security.AccessController.doPrivileged(spooler);
        if (spooler.pex != null) {
            throw spooler.pex;
        }
    }
}
Also used : PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 42 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method getPrintServices.

/*
     * If service attributes are specified then there must be additional
     * filtering.
     */
public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPrintJobAccess();
    }
    PrintRequestAttributeSet requestSet = null;
    PrintServiceAttributeSet serviceSet = null;
    if (attributes != null && !attributes.isEmpty()) {
        requestSet = new HashPrintRequestAttributeSet();
        serviceSet = new HashPrintServiceAttributeSet();
        Attribute[] attrs = attributes.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                requestSet.add(attrs[i]);
            } else if (attrs[i] instanceof PrintServiceAttribute) {
                serviceSet.add(attrs[i]);
            }
        }
    }
    PrintService[] services = getPrintServices(serviceSet);
    if (services.length == 0) {
        return services;
    }
    if (CUPSPrinter.isCupsRunning()) {
        ArrayList matchingServices = new ArrayList();
        for (int i = 0; i < services.length; i++) {
            try {
                if (services[i].getUnsupportedAttributes(flavor, requestSet) == null) {
                    matchingServices.add(services[i]);
                }
            } catch (IllegalArgumentException e) {
            }
        }
        services = new PrintService[matchingServices.size()];
        return (PrintService[]) matchingServices.toArray(services);
    } else {
        // We only need to compare 1 PrintService because all
        // UnixPrintServices are the same anyway.  We will not use
        // default PrintService because it might be null.
        PrintService service = services[0];
        if ((flavor == null || service.isDocFlavorSupported(flavor)) && service.getUnsupportedAttributes(flavor, requestSet) == null) {
            return services;
        } else {
            return new PrintService[0];
        }
    }
}
Also used : PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) ArrayList(java.util.ArrayList) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 43 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method refreshServices.

// refreshes "printServices"
public synchronized void refreshServices() {
    /* excludes the default printer */
    // array of printer names
    String[] printers = null;
    //array of printer URIs
    String[] printerURIs = null;
    try {
        getDefaultPrintService();
    } catch (Throwable t) {
        IPPPrintService.debug_println(debugPrefix + "Exception getting default printer : " + t);
    }
    if (CUPSPrinter.isCupsRunning()) {
        try {
            printerURIs = CUPSPrinter.getAllPrinters();
            IPPPrintService.debug_println("CUPS URIs = " + printerURIs);
            if (printerURIs != null) {
                for (int p = 0; p < printerURIs.length; p++) {
                    IPPPrintService.debug_println("URI=" + printerURIs[p]);
                }
            }
        } catch (Throwable t) {
            IPPPrintService.debug_println(debugPrefix + "Exception getting all CUPS printers : " + t);
        }
        if ((printerURIs != null) && (printerURIs.length > 0)) {
            printers = new String[printerURIs.length];
            for (int i = 0; i < printerURIs.length; i++) {
                int lastIndex = printerURIs[i].lastIndexOf("/");
                printers[i] = printerURIs[i].substring(lastIndex + 1);
            }
        }
    } else {
        if (isMac() || isSysV()) {
            printers = getAllPrinterNamesSysV();
        } else if (isAIX()) {
            printers = getAllPrinterNamesAIX();
        } else {
            //BSD
            printers = getAllPrinterNamesBSD();
        }
    }
    if (printers == null) {
        if (defaultPrintService != null) {
            printServices = new PrintService[1];
            printServices[0] = defaultPrintService;
        } else {
            printServices = null;
        }
        return;
    }
    ArrayList printerList = new ArrayList();
    int defaultIndex = -1;
    for (int p = 0; p < printers.length; p++) {
        if (printers[p] == null) {
            continue;
        }
        if ((defaultPrintService != null) && printers[p].equals(getPrinterDestName(defaultPrintService))) {
            defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
        } else {
            if (printServices == null) {
                IPPPrintService.debug_println(debugPrefix + "total# of printers = " + printers.length);
                if (CUPSPrinter.isCupsRunning()) {
                    try {
                        addPrintServiceToList(printerList, new IPPPrintService(printers[p], printerURIs[p], true));
                    } catch (Exception e) {
                        IPPPrintService.debug_println(debugPrefix + " getAllPrinters Exception " + e);
                    }
                } else {
                    printerList.add(new UnixPrintService(printers[p]));
                }
            } else {
                int j;
                for (j = 0; j < printServices.length; j++) {
                    if (printServices[j] != null) {
                        if (printers[p].equals(getPrinterDestName(printServices[j]))) {
                            printerList.add(printServices[j]);
                            printServices[j] = null;
                            break;
                        }
                    }
                }
                if (j == printServices.length) {
                    // not found?
                    if (CUPSPrinter.isCupsRunning()) {
                        try {
                            addPrintServiceToList(printerList, new IPPPrintService(printers[p], printerURIs[p], true));
                        } catch (Exception e) {
                            IPPPrintService.debug_println(debugPrefix + " getAllPrinters Exception " + e);
                        }
                    } else {
                        printerList.add(new UnixPrintService(printers[p]));
                    }
                }
            }
        }
    }
    // Look for deleted services and invalidate these
    if (printServices != null) {
        for (int j = 0; j < printServices.length; j++) {
            if ((printServices[j] instanceof UnixPrintService) && (!printServices[j].equals(defaultPrintService))) {
                ((UnixPrintService) printServices[j]).invalidateService();
            }
        }
    }
    //if defaultService is not found in printerList
    if (defaultIndex == -1 && defaultPrintService != null) {
        defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
    }
    printServices = (PrintService[]) printerList.toArray(new PrintService[] {});
    // swap default with the first in the list
    if (defaultIndex > 0) {
        PrintService saveService = printServices[0];
        printServices[0] = printServices[defaultIndex];
        printServices[defaultIndex] = saveService;
    }
}
Also used : ArrayList(java.util.ArrayList) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService)

Example 44 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method getServiceByName.

/* On a network with many (hundreds) of network printers, it
     * can save several seconds if you know all you want is a particular
     * printer, to ask for that printer rather than retrieving all printers.
     */
private PrintService getServiceByName(PrinterName nameAttr) {
    String name = nameAttr.getValue();
    if (name == null || name.equals("") || !checkPrinterName(name)) {
        return null;
    }
    /* check if all printers are already available */
    if (printServices != null) {
        for (PrintService printService : printServices) {
            PrinterName printerName = (PrinterName) printService.getAttribute(PrinterName.class);
            if (printerName.getValue().equals(name)) {
                return printService;
            }
        }
    }
    /* take CUPS into account first */
    if (CUPSPrinter.isCupsRunning()) {
        try {
            return new IPPPrintService(name, new URL("http://" + CUPSPrinter.getServer() + ":" + CUPSPrinter.getPort() + "/" + name));
        } catch (Exception e) {
            IPPPrintService.debug_println(debugPrefix + " getServiceByName Exception " + e);
        }
    }
    /* fallback if nothing not having a printer at this point */
    PrintService printer = null;
    if (isMac() || isSysV()) {
        printer = getNamedPrinterNameSysV(name);
    } else if (isAIX()) {
        printer = getNamedPrinterNameAIX(name);
    } else {
        printer = getNamedPrinterNameBSD(name);
    }
    return printer;
}
Also used : PrinterName(javax.print.attribute.standard.PrinterName) URL(java.net.URL) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService)

Example 45 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class CountPrintServices method main.

public static void main(String[] args) throws Exception {
    String os = System.getProperty("os.name").toLowerCase();
    System.out.println("OS is " + os);
    if (!os.equals("linux")) {
        System.out.println("Linux specific test. No need to continue");
        return;
    }
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    if (services.length > 0) {
        System.out.println("Services found. No need to test further.");
        return;
    }
    String[] lpcmd = { "lpstat", "-a" };
    Process proc = Runtime.getRuntime().exec(lpcmd);
    proc.waitFor();
    InputStreamReader ir = new InputStreamReader(proc.getInputStream());
    BufferedReader br = new BufferedReader(ir);
    int count = 0;
    String printer;
    while ((printer = br.readLine()) != null) {
        System.out.println("lpstat:: " + printer);
        count++;
    }
    if (count > 0) {
        throw new RuntimeException("Services exist, but not found by JDK.");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PrintService(javax.print.PrintService)

Aggregations

PrintService (javax.print.PrintService)48 StreamPrintService (javax.print.StreamPrintService)13 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)13 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)13 PrinterException (java.awt.print.PrinterException)10 PageFormat (java.awt.print.PageFormat)9 IOException (java.io.IOException)8 MultiDocPrintService (javax.print.MultiDocPrintService)8 DocFlavor (javax.print.DocFlavor)7 Copies (javax.print.attribute.standard.Copies)7 MediaSizeName (javax.print.attribute.standard.MediaSizeName)7 HeadlessException (java.awt.HeadlessException)6 File (java.io.File)6 DocPrintJob (javax.print.DocPrintJob)5 PrintException (javax.print.PrintException)5 Destination (javax.print.attribute.standard.Destination)5 Media (javax.print.attribute.standard.Media)5 MediaSize (javax.print.attribute.standard.MediaSize)5 Paper (java.awt.print.Paper)4 ArrayList (java.util.ArrayList)4