src/solaris/classes/sun/print/UnixPrintServiceLookup.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 183         }
 184     }
 185 
 186     /* Want the PrintService which is default print service to have
 187      * equality of reference with the equivalent in list of print services
 188      * This isn't required by the API and there's a risk doing this will
 189      * lead people to assume its guaranteed.
 190      */
 191     public synchronized PrintService[] getPrintServices() {
 192         SecurityManager security = System.getSecurityManager();
 193         if (security != null) {
 194             security.checkPrintJobAccess();
 195         }
 196 
 197         if (printServices == null || !pollServices) {
 198             refreshServices();
 199         }
 200         if (printServices == null) {
 201             return new PrintService[0];
 202         } else {
 203             return (PrintService[])printServices.clone();
 204         }
 205     }
 206 
 207     private int addPrintServiceToList(ArrayList printerList, PrintService ps) {
 208         int index = printerList.indexOf(ps);
 209         // Check if PrintService with same name is already in the list.
 210         if (CUPSPrinter.isCupsRunning() && index != -1) {
 211             // Bug in Linux: Duplicate entry of a remote printer
 212             // and treats it as local printer but it is returning wrong
 213             // information when queried using IPP. Workaround is to remove it.
 214             // Even CUPS ignores these entries as shown in lpstat or using
 215             // their web configuration.
 216             PrinterURI uri = (PrinterURI)ps.getAttribute(PrinterURI.class);
 217             if (uri.getURI().getHost().equals("localhost")) {
 218                 IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps);
 219                 return index;  // Do not add this.
 220             }
 221             PrintService oldPS = (PrintService)(printerList.get(index));
 222             uri = (PrinterURI)oldPS.getAttribute(PrinterURI.class);
 223             if (uri.getURI().getHost().equals("localhost")) {
 224                 IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS);
 225                 printerList.remove(oldPS);
 226             } else {
 227                 return index;
 228             }
 229         }
 230         printerList.add(ps);
 231         return (printerList.size() - 1);
 232     }
 233 
 234 
 235     // refreshes "printServices"
 236     public synchronized void refreshServices() {
 237         /* excludes the default printer */
 238         String[] printers = null; // array of printer names
 239         String[] printerURIs = null; //array of printer URIs
 240 
 241         getDefaultPrintService();
 242         if (CUPSPrinter.isCupsRunning()) {


 393      */
 394     private String getPrinterDestName(PrintService ps) {
 395         if (isMac()) {
 396             return ((IPPPrintService)ps).getDest();
 397         }
 398         return ps.getName();
 399     }
 400 
 401     /* On a network with many (hundreds) of network printers, it
 402      * can save several seconds if you know all you want is a particular
 403      * printer, to ask for that printer rather than retrieving all printers.
 404      */
 405     private PrintService getServiceByName(PrinterName nameAttr) {
 406         String name = nameAttr.getValue();
 407         if (name == null || name.equals("") || !checkPrinterName(name)) {
 408             return null;
 409         }
 410         /* check if all printers are already available */
 411         if (printServices != null) {
 412             for (PrintService printService : printServices) {
 413                 PrinterName printerName =
 414                     (PrinterName)printService.getAttribute(PrinterName.class);
 415                 if (printerName.getValue().equals(name)) {
 416                     return printService;
 417                 }
 418             }
 419         }
 420         /* take CUPS into account first */
 421         if (CUPSPrinter.isCupsRunning()) {
 422             try {
 423                 return new IPPPrintService(name,
 424                                            new URL("http://"+
 425                                                    CUPSPrinter.getServer()+":"+
 426                                                    CUPSPrinter.getPort()+"/"+
 427                                                    name));
 428             } catch (Exception e) {
 429                 IPPPrintService.debug_println(debugPrefix+
 430                                               " getServiceByName Exception "+
 431                                               e);
 432             }
 433         }
 434         /* fallback if nothing not having a printer at this point */


 447         if (serviceSet == null || serviceSet.isEmpty()) {
 448             return getPrintServices();
 449         }
 450 
 451         /* Typically expect that if a service attribute is specified that
 452          * its a printer name and there ought to be only one match.
 453          * Directly retrieve that service and confirm
 454          * that it meets the other requirements.
 455          * If printer name isn't mentioned then go a slow path checking
 456          * all printers if they meet the reqiremements.
 457          */
 458         PrintService[] services;
 459         PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
 460         PrintService defService;
 461         if (name != null && (defService = getDefaultPrintService()) != null) {
 462             /* To avoid execing a unix command  see if the client is asking
 463              * for the default printer by name, since we already have that
 464              * initialised.
 465              */
 466 
 467             PrinterName defName =
 468                 (PrinterName)defService.getAttribute(PrinterName.class);
 469 
 470             if (defName != null && name.equals(defName)) {
 471                 if (matchesAttributes(defService, serviceSet)) {
 472                     services = new PrintService[1];
 473                     services[0] = defService;
 474                     return services;
 475                 } else {
 476                     return new PrintService[0];
 477                 }
 478             } else {
 479                 /* Its not the default service */
 480                 PrintService service = getServiceByName(name);
 481                 if (service != null &&
 482                     matchesAttributes(service, serviceSet)) {
 483                     services = new PrintService[1];
 484                     services[0] = service;
 485                     return services;
 486                 } else {
 487                     return new PrintService[0];
 488                 }


   1 /*
   2  * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 183         }
 184     }
 185 
 186     /* Want the PrintService which is default print service to have
 187      * equality of reference with the equivalent in list of print services
 188      * This isn't required by the API and there's a risk doing this will
 189      * lead people to assume its guaranteed.
 190      */
 191     public synchronized PrintService[] getPrintServices() {
 192         SecurityManager security = System.getSecurityManager();
 193         if (security != null) {
 194             security.checkPrintJobAccess();
 195         }
 196 
 197         if (printServices == null || !pollServices) {
 198             refreshServices();
 199         }
 200         if (printServices == null) {
 201             return new PrintService[0];
 202         } else {
 203             return printServices.clone();
 204         }
 205     }
 206 
 207     private int addPrintServiceToList(ArrayList printerList, PrintService ps) {
 208         int index = printerList.indexOf(ps);
 209         // Check if PrintService with same name is already in the list.
 210         if (CUPSPrinter.isCupsRunning() && index != -1) {
 211             // Bug in Linux: Duplicate entry of a remote printer
 212             // and treats it as local printer but it is returning wrong
 213             // information when queried using IPP. Workaround is to remove it.
 214             // Even CUPS ignores these entries as shown in lpstat or using
 215             // their web configuration.
 216             PrinterURI uri = ps.getAttribute(PrinterURI.class);
 217             if (uri.getURI().getHost().equals("localhost")) {
 218                 IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps);
 219                 return index;  // Do not add this.
 220             }
 221             PrintService oldPS = (PrintService)(printerList.get(index));
 222             uri = oldPS.getAttribute(PrinterURI.class);
 223             if (uri.getURI().getHost().equals("localhost")) {
 224                 IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS);
 225                 printerList.remove(oldPS);
 226             } else {
 227                 return index;
 228             }
 229         }
 230         printerList.add(ps);
 231         return (printerList.size() - 1);
 232     }
 233 
 234 
 235     // refreshes "printServices"
 236     public synchronized void refreshServices() {
 237         /* excludes the default printer */
 238         String[] printers = null; // array of printer names
 239         String[] printerURIs = null; //array of printer URIs
 240 
 241         getDefaultPrintService();
 242         if (CUPSPrinter.isCupsRunning()) {


 393      */
 394     private String getPrinterDestName(PrintService ps) {
 395         if (isMac()) {
 396             return ((IPPPrintService)ps).getDest();
 397         }
 398         return ps.getName();
 399     }
 400 
 401     /* On a network with many (hundreds) of network printers, it
 402      * can save several seconds if you know all you want is a particular
 403      * printer, to ask for that printer rather than retrieving all printers.
 404      */
 405     private PrintService getServiceByName(PrinterName nameAttr) {
 406         String name = nameAttr.getValue();
 407         if (name == null || name.equals("") || !checkPrinterName(name)) {
 408             return null;
 409         }
 410         /* check if all printers are already available */
 411         if (printServices != null) {
 412             for (PrintService printService : printServices) {
 413                 PrinterName printerName = printService.getAttribute(PrinterName.class);

 414                 if (printerName.getValue().equals(name)) {
 415                     return printService;
 416                 }
 417             }
 418         }
 419         /* take CUPS into account first */
 420         if (CUPSPrinter.isCupsRunning()) {
 421             try {
 422                 return new IPPPrintService(name,
 423                                            new URL("http://"+
 424                                                    CUPSPrinter.getServer()+":"+
 425                                                    CUPSPrinter.getPort()+"/"+
 426                                                    name));
 427             } catch (Exception e) {
 428                 IPPPrintService.debug_println(debugPrefix+
 429                                               " getServiceByName Exception "+
 430                                               e);
 431             }
 432         }
 433         /* fallback if nothing not having a printer at this point */


 446         if (serviceSet == null || serviceSet.isEmpty()) {
 447             return getPrintServices();
 448         }
 449 
 450         /* Typically expect that if a service attribute is specified that
 451          * its a printer name and there ought to be only one match.
 452          * Directly retrieve that service and confirm
 453          * that it meets the other requirements.
 454          * If printer name isn't mentioned then go a slow path checking
 455          * all printers if they meet the reqiremements.
 456          */
 457         PrintService[] services;
 458         PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
 459         PrintService defService;
 460         if (name != null && (defService = getDefaultPrintService()) != null) {
 461             /* To avoid execing a unix command  see if the client is asking
 462              * for the default printer by name, since we already have that
 463              * initialised.
 464              */
 465 
 466             PrinterName defName = defService.getAttribute(PrinterName.class);

 467 
 468             if (defName != null && name.equals(defName)) {
 469                 if (matchesAttributes(defService, serviceSet)) {
 470                     services = new PrintService[1];
 471                     services[0] = defService;
 472                     return services;
 473                 } else {
 474                     return new PrintService[0];
 475                 }
 476             } else {
 477                 /* Its not the default service */
 478                 PrintService service = getServiceByName(name);
 479                 if (service != null &&
 480                     matchesAttributes(service, serviceSet)) {
 481                     services = new PrintService[1];
 482                     services[0] = service;
 483                     return services;
 484                 } else {
 485                     return new PrintService[0];
 486                 }