Java Code Examples for javax.print.PrintService#getAttribute()

The following examples show how to use javax.print.PrintService#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrintServiceLookupProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private int addPrintServiceToList(ArrayList<PrintService> printerList, PrintService ps) {
    int index = printerList.indexOf(ps);
    // Check if PrintService with same name is already in the list.
    if (CUPSPrinter.isCupsRunning() && index != -1) {
        // Bug in Linux: Duplicate entry of a remote printer
        // and treats it as local printer but it is returning wrong
        // information when queried using IPP. Workaround is to remove it.
        // Even CUPS ignores these entries as shown in lpstat or using
        // their web configuration.
        PrinterURI uri = ps.getAttribute(PrinterURI.class);
        if (uri.getURI().getHost().equals("localhost")) {
            IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps);
            return index;  // Do not add this.
        }
        PrintService oldPS = printerList.get(index);
        uri = oldPS.getAttribute(PrinterURI.class);
        if (uri.getURI().getHost().equals("localhost")) {
            IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS);
            printerList.remove(oldPS);
        } else {
            return index;
        }
    }
    printerList.add(ps);
    return (printerList.size() - 1);
}
 
Example 2
Source File: PrintServiceLookupProvider.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean matchingService(PrintService service,
                        PrintServiceAttributeSet serviceSet) {
    if (serviceSet != null) {
        Attribute [] attrs =  serviceSet.toArray();
        Attribute serviceAttr;
        for (int i=0; i<attrs.length; i++) {
            serviceAttr
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                return false;
            }
        }
    }
    return true;
}
 
Example 3
Source File: RasterPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Associate this PrinterJob with a new PrintService.
 *
 * Throws <code>PrinterException</code> if the specified service
 * cannot support the <code>Pageable</code> and
 * <code>Printable</code> interfaces necessary to support 2D printing.
 * @param a print service which supports 2D printing.
 *
 * @throws PrinterException if the specified service does not support
 * 2D printing or no longer available.
 */
public void setPrintService(PrintService service)
    throws PrinterException {
    if (service == null) {
        throw new PrinterException("Service cannot be null");
    } else if (!(service instanceof StreamPrintService) &&
               service.getName() == null) {
        throw new PrinterException("Null PrintService name.");
    } else {
        // Check the list of services.  This service may have been
        // deleted already
        PrinterState prnState = (PrinterState)service.getAttribute(
                                              PrinterState.class);
        if (prnState == PrinterState.STOPPED) {
            PrinterStateReasons prnStateReasons =
                (PrinterStateReasons)service.getAttribute(
                                             PrinterStateReasons.class);
            if ((prnStateReasons != null) &&
                (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
            {
                throw new PrinterException("PrintService is no longer available.");
            }
        }


        if (service.isDocFlavorSupported(
                                         DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
            service.isDocFlavorSupported(
                                         DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            myService = service;
        } else {
            throw new PrinterException("Not a 2D print service: " + service);
        }
    }
}
 
Example 4
Source File: UnixPrintServiceLookup.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: PrintServiceLookupProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
boolean matchingService(PrintService service,
                        PrintServiceAttributeSet serviceSet) {
    if (serviceSet != null) {
        Attribute [] attrs =  serviceSet.toArray();
        Attribute serviceAttr;
        for (int i=0; i<attrs.length; i++) {
            serviceAttr
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                return false;
            }
        }
    }
    return true;
}
 
Example 6
Source File: UnixPrintServiceLookup.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private boolean matchesAttributes(PrintService service,
                                  PrintServiceAttributeSet attributes) {

    Attribute [] attrs =  attributes.toArray();
    Attribute serviceAttr;
    for (int i=0; i<attrs.length; i++) {
        serviceAttr
            = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
        if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: RasterPrinterJob.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Associate this PrinterJob with a new PrintService.
 *
 * Throws <code>PrinterException</code> if the specified service
 * cannot support the <code>Pageable</code> and
 * <code>Printable</code> interfaces necessary to support 2D printing.
 * @param a print service which supports 2D printing.
 *
 * @throws PrinterException if the specified service does not support
 * 2D printing or no longer available.
 */
public void setPrintService(PrintService service)
    throws PrinterException {
    if (service == null) {
        throw new PrinterException("Service cannot be null");
    } else if (!(service instanceof StreamPrintService) &&
               service.getName() == null) {
        throw new PrinterException("Null PrintService name.");
    } else {
        // Check the list of services.  This service may have been
        // deleted already
        PrinterState prnState = (PrinterState)service.getAttribute(
                                              PrinterState.class);
        if (prnState == PrinterState.STOPPED) {
            PrinterStateReasons prnStateReasons =
                (PrinterStateReasons)service.getAttribute(
                                             PrinterStateReasons.class);
            if ((prnStateReasons != null) &&
                (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
            {
                throw new PrinterException("PrintService is no longer available.");
            }
        }


        if (service.isDocFlavorSupported(
                                         DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
            service.isDocFlavorSupported(
                                         DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            myService = service;
        } else {
            throw new PrinterException("Not a 2D print service: " + service);
        }
    }
}
 
Example 8
Source File: UnixPrintServiceLookup.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: Win32PrintServiceLookup.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean matchingService(PrintService service,
                        PrintServiceAttributeSet serviceSet) {
    if (serviceSet != null) {
        Attribute [] attrs =  serviceSet.toArray();
        Attribute serviceAttr;
        for (int i=0; i<attrs.length; i++) {
            serviceAttr
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                return false;
            }
        }
    }
    return true;
}
 
Example 10
Source File: Win32PrintServiceLookup.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
boolean matchingService(PrintService service,
                        PrintServiceAttributeSet serviceSet) {
    if (serviceSet != null) {
        Attribute [] attrs =  serviceSet.toArray();
        Attribute serviceAttr;
        for (int i=0; i<attrs.length; i++) {
            serviceAttr
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                return false;
            }
        }
    }
    return true;
}
 
Example 11
Source File: UnixPrintServiceLookup.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean matchesAttributes(PrintService service,
                                  PrintServiceAttributeSet attributes) {

    Attribute [] attrs =  attributes.toArray();
    Attribute serviceAttr;
    for (int i=0; i<attrs.length; i++) {
        serviceAttr
            = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
        if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
            return false;
        }
    }
    return true;
}
 
Example 12
Source File: UnixPrintServiceLookup.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: Win32PrintServiceLookup.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
boolean matchingService(PrintService service,
                        PrintServiceAttributeSet serviceSet) {
    if (serviceSet != null) {
        Attribute [] attrs =  serviceSet.toArray();
        Attribute serviceAttr;
        for (int i=0; i<attrs.length; i++) {
            serviceAttr
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                return false;
            }
        }
    }
    return true;
}
 
Example 14
Source File: PrintServiceLookupProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean matchesAttributes(PrintService service,
                                  PrintServiceAttributeSet attributes) {

    Attribute [] attrs =  attributes.toArray();
    for (int i=0; i<attrs.length; i++) {
        @SuppressWarnings("unchecked")
        Attribute serviceAttr
            = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
        if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
            return false;
        }
    }
    return true;
}
 
Example 15
Source File: UnixPrintServiceLookup.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private boolean matchesAttributes(PrintService service,
                                  PrintServiceAttributeSet attributes) {

    Attribute [] attrs =  attributes.toArray();
    Attribute serviceAttr;
    for (int i=0; i<attrs.length; i++) {
        serviceAttr
            = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
        if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
            return false;
        }
    }
    return true;
}
 
Example 16
Source File: PrintServiceLookupProvider.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: RasterPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Associate this PrinterJob with a new PrintService.
 *
 * Throws <code>PrinterException</code> if the specified service
 * cannot support the <code>Pageable</code> and
 * <code>Printable</code> interfaces necessary to support 2D printing.
 * @param a print service which supports 2D printing.
 *
 * @throws PrinterException if the specified service does not support
 * 2D printing or no longer available.
 */
public void setPrintService(PrintService service)
    throws PrinterException {
    if (service == null) {
        throw new PrinterException("Service cannot be null");
    } else if (!(service instanceof StreamPrintService) &&
               service.getName() == null) {
        throw new PrinterException("Null PrintService name.");
    } else {
        // Check the list of services.  This service may have been
        // deleted already
        PrinterState prnState = (PrinterState)service.getAttribute(
                                              PrinterState.class);
        if (prnState == PrinterState.STOPPED) {
            PrinterStateReasons prnStateReasons =
                (PrinterStateReasons)service.getAttribute(
                                             PrinterStateReasons.class);
            if ((prnStateReasons != null) &&
                (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
            {
                throw new PrinterException("PrintService is no longer available.");
            }
        }


        if (service.isDocFlavorSupported(
                                         DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
            service.isDocFlavorSupported(
                                         DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            myService = service;
        } else {
            throw new PrinterException("Not a 2D print service: " + service);
        }
    }
}
 
Example 18
Source File: PrintServiceLookupProvider.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private PrintService[]
    getPrintServices(PrintServiceAttributeSet serviceSet) {

    if (serviceSet == null || serviceSet.isEmpty()) {
        return getPrintServices();
    }

    /* Typically expect that if a service attribute is specified that
     * its a printer name and there ought to be only one match.
     * Directly retrieve that service and confirm
     * that it meets the other requirements.
     * If printer name isn't mentioned then go a slow path checking
     * all printers if they meet the reqiremements.
     */
    PrintService[] services;
    PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
    PrintService defService;
    if (name != null && (defService = getDefaultPrintService()) != null) {
        /* To avoid execing a unix command  see if the client is asking
         * for the default printer by name, since we already have that
         * initialised.
         */

        PrinterName defName =
            (PrinterName)defService.getAttribute(PrinterName.class);

        if (defName != null && name.equals(defName)) {
            if (matchesAttributes(defService, serviceSet)) {
                services = new PrintService[1];
                services[0] = defService;
                return services;
            } else {
                return new PrintService[0];
            }
        } else {
            /* Its not the default service */
            PrintService service = getServiceByName(name);
            if (service != null &&
                matchesAttributes(service, serviceSet)) {
                services = new PrintService[1];
                services[0] = service;
                return services;
            } else {
                return new PrintService[0];
            }
        }
    } else {
        /* specified service attributes don't include a name.*/
        Vector matchedServices = new Vector();
        services = getPrintServices();
        for (int i = 0; i< services.length; i++) {
            if (matchesAttributes(services[i], serviceSet)) {
                matchedServices.add(services[i]);
            }
        }
        services = new PrintService[matchedServices.size()];
        for (int i = 0; i< services.length; i++) {
            services[i] = (PrintService)matchedServices.elementAt(i);
        }
        return services;
    }
}
 
Example 19
Source File: UnixPrintServiceLookup.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private PrintService[]
    getPrintServices(PrintServiceAttributeSet serviceSet) {

    if (serviceSet == null || serviceSet.isEmpty()) {
        return getPrintServices();
    }

    /* Typically expect that if a service attribute is specified that
     * its a printer name and there ought to be only one match.
     * Directly retrieve that service and confirm
     * that it meets the other requirements.
     * If printer name isn't mentioned then go a slow path checking
     * all printers if they meet the reqiremements.
     */
    PrintService[] services;
    PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
    PrintService defService;
    if (name != null && (defService = getDefaultPrintService()) != null) {
        /* To avoid execing a unix command  see if the client is asking
         * for the default printer by name, since we already have that
         * initialised.
         */

        PrinterName defName =
            (PrinterName)defService.getAttribute(PrinterName.class);

        if (defName != null && name.equals(defName)) {
            if (matchesAttributes(defService, serviceSet)) {
                services = new PrintService[1];
                services[0] = defService;
                return services;
            } else {
                return new PrintService[0];
            }
        } else {
            /* Its not the default service */
            PrintService service = getServiceByName(name);
            if (service != null &&
                matchesAttributes(service, serviceSet)) {
                services = new PrintService[1];
                services[0] = service;
                return services;
            } else {
                return new PrintService[0];
            }
        }
    } else {
        /* specified service attributes don't include a name.*/
        Vector matchedServices = new Vector();
        services = getPrintServices();
        for (int i = 0; i< services.length; i++) {
            if (matchesAttributes(services[i], serviceSet)) {
                matchedServices.add(services[i]);
            }
        }
        services = new PrintService[matchedServices.size()];
        for (int i = 0; i< services.length; i++) {
            services[i] = (PrintService)matchedServices.elementAt(i);
        }
        return services;
    }
}
 
Example 20
Source File: PrintServiceLookupProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private PrintService[]
    getPrintServices(PrintServiceAttributeSet serviceSet) {

    if (serviceSet == null || serviceSet.isEmpty()) {
        return getPrintServices();
    }

    /* Typically expect that if a service attribute is specified that
     * its a printer name and there ought to be only one match.
     * Directly retrieve that service and confirm
     * that it meets the other requirements.
     * If printer name isn't mentioned then go a slow path checking
     * all printers if they meet the reqiremements.
     */
    PrintService[] services;
    PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
    PrintService defService;
    if (name != null && (defService = getDefaultPrintService()) != null) {
        /* To avoid execing a unix command  see if the client is asking
         * for the default printer by name, since we already have that
         * initialised.
         */

        PrinterName defName =
            (PrinterName)defService.getAttribute(PrinterName.class);

        if (defName != null && name.equals(defName)) {
            if (matchesAttributes(defService, serviceSet)) {
                services = new PrintService[1];
                services[0] = defService;
                return services;
            } else {
                return new PrintService[0];
            }
        } else {
            /* Its not the default service */
            PrintService service = getServiceByName(name);
            if (service != null &&
                matchesAttributes(service, serviceSet)) {
                services = new PrintService[1];
                services[0] = service;
                return services;
            } else {
                return new PrintService[0];
            }
        }
    } else {
        /* specified service attributes don't include a name.*/
        Vector matchedServices = new Vector();
        services = getPrintServices();
        for (int i = 0; i< services.length; i++) {
            if (matchesAttributes(services[i], serviceSet)) {
                matchedServices.add(services[i]);
            }
        }
        services = new PrintService[matchedServices.size()];
        for (int i = 0; i< services.length; i++) {
            services[i] = (PrintService)matchedServices.elementAt(i);
        }
        return services;
    }
}