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

Print this page
rev 8972 : 8031134: PPC64: implement printing on AIX


  61 public class UnixPrintServiceLookup extends PrintServiceLookup
  62     implements BackgroundServiceLookup, Runnable {
  63 
  64     /* Remind: the current implementation is static, as its assumed
  65      * its preferable to minimize creation of PrintService instances.
  66      * Later we should add logic to add/remove services on the fly which
  67      * will take a hit of needing to regather the list of services.
  68      */
  69     private String defaultPrinter;
  70     private PrintService defaultPrintService;
  71     private PrintService[] printServices; /* includes the default printer */
  72     private Vector lookupListeners = null;
  73     private static String debugPrefix = "UnixPrintServiceLookup>> ";
  74     private static boolean pollServices = true;
  75     private static final int DEFAULT_MINREFRESH = 120;  // 2 minutes
  76     private static int minRefreshTime = DEFAULT_MINREFRESH;
  77 
  78 
  79     static String osname;
  80 













  81     static {
  82         /* The system property "sun.java2d.print.polling"
  83          * can be used to force the printing code to poll or not poll
  84          * for PrintServices.
  85          */
  86         String pollStr = java.security.AccessController.doPrivileged(
  87             new sun.security.action.GetPropertyAction("sun.java2d.print.polling"));
  88 
  89         if (pollStr != null) {
  90             if (pollStr.equalsIgnoreCase("true")) {
  91                 pollServices = true;
  92             } else if (pollStr.equalsIgnoreCase("false")) {
  93                 pollServices = false;
  94             }
  95         }
  96 
  97         /* The system property "sun.java2d.print.minRefreshTime"
  98          * can be used to specify minimum refresh time (in seconds)
  99          * for polling PrintServices.  The default is 120.
 100          */
 101         String refreshTimeStr = java.security.AccessController.doPrivileged(
 102             new sun.security.action.GetPropertyAction(
 103                 "sun.java2d.print.minRefreshTime"));
 104 
 105         if (refreshTimeStr != null) {
 106             try {
 107                 minRefreshTime = (new Integer(refreshTimeStr)).intValue();
 108             } catch (NumberFormatException e) {
 109             }
 110             if (minRefreshTime < DEFAULT_MINREFRESH) {
 111                 minRefreshTime = DEFAULT_MINREFRESH;
 112             }
 113         }
 114 
 115         osname = java.security.AccessController.doPrivileged(
 116             new sun.security.action.GetPropertyAction("os.name"));


















 117     }
 118 
 119     static boolean isMac() {
 120         return osname.startsWith("Mac");
 121     }
 122 
 123     static boolean isSysV() {
 124         return osname.equals("SunOS");
 125     }
 126 
 127     static boolean isLinux() {
 128         return (osname.equals("Linux"));
 129     }
 130 
 131     static boolean isBSD() {
 132         return (osname.equals("Linux") ||
 133                 osname.contains("OS X"));
 134     }
 135 




 136     static final int UNINITIALIZED = -1;
 137     static final int BSD_LPD = 0;
 138     static final int BSD_LPD_NG = 1;
 139 
 140     static int cmdIndex = UNINITIALIZED;
 141 
 142     String[] lpcFirstCom = {
 143         "/usr/sbin/lpc status | grep : | sed -ne '1,1 s/://p'",
 144         "/usr/sbin/lpc status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
 145     };
 146 
 147     String[] lpcAllCom = {
 148         "/usr/sbin/lpc status all | grep : | sed -e 's/://'",
 149         "/usr/sbin/lpc status all | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}' | sort"
 150     };
 151 
 152     String[] lpcNameCom = {
 153         "| grep : | sed -ne 's/://p'",
 154         "| grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
 155     };


 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()) {
 243             printerURIs = CUPSPrinter.getAllPrinters();
 244             if ((printerURIs != null) && (printerURIs.length > 0)) {
 245                 printers = new String[printerURIs.length];
 246                 for (int i=0; i<printerURIs.length; i++) {
 247                     int lastIndex = printerURIs[i].lastIndexOf("/");
 248                     printers[i] = printerURIs[i].substring(lastIndex+1);
 249                 }
 250             }
 251         } else {
 252             if (isMac() || isSysV()) {
 253                 printers = getAllPrinterNamesSysV();


 254             } else { //BSD
 255                 printers = getAllPrinterNamesBSD();
 256             }
 257         }
 258 
 259         if (printers == null) {
 260             if (defaultPrintService != null) {
 261                 printServices = new PrintService[1];
 262                 printServices[0] = defaultPrintService;
 263             } else {
 264                 printServices = null;
 265             }
 266             return;
 267         }
 268 
 269         ArrayList printerList = new ArrayList();
 270         int defaultIndex = -1;
 271         for (int p=0; p<printers.length; p++) {
 272             if (printers[p] == null) {
 273                 continue;


 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 */
 435         PrintService printer = null;
 436         if (isMac() || isSysV()) {
 437             printer = getNamedPrinterNameSysV(name);


 438         } else {
 439             printer = getNamedPrinterNameBSD(name);
 440         }
 441         return printer;
 442     }
 443 
 444     private PrintService[]
 445         getPrintServices(PrintServiceAttributeSet serviceSet) {
 446 
 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          */


 583 
 584     public synchronized PrintService getDefaultPrintService() {
 585         SecurityManager security = System.getSecurityManager();
 586         if (security != null) {
 587           security.checkPrintJobAccess();
 588         }
 589 
 590         // clear defaultPrintService
 591         defaultPrintService = null;
 592         String psuri = null;
 593 
 594         IPPPrintService.debug_println("isRunning ? "+
 595                                       (CUPSPrinter.isCupsRunning()));
 596         if (CUPSPrinter.isCupsRunning()) {
 597             String[] printerInfo = CUPSPrinter.getDefaultPrinter();
 598             defaultPrinter = printerInfo[0];
 599             psuri = printerInfo[1];
 600         } else {
 601             if (isMac() || isSysV()) {
 602                 defaultPrinter = getDefaultPrinterNameSysV();


 603             } else {
 604                 defaultPrinter = getDefaultPrinterNameBSD();
 605             }
 606         }
 607         if (defaultPrinter == null) {
 608             return null;
 609         }
 610         defaultPrintService = null;
 611         if (printServices != null) {
 612             for (int j=0; j<printServices.length; j++) {
 613                 if (defaultPrinter.equals(getPrinterDestName(printServices[j]))) {
 614                     defaultPrintService = printServices[j];
 615                     break;
 616                 }
 617             }
 618         }
 619         if (defaultPrintService == null) {
 620             if (CUPSPrinter.isCupsRunning()) {
 621                 try {
 622                     PrintService defaultPS;


 757             return new UnixPrintService(name);
 758         }
 759     }
 760 
 761     private String[] getAllPrinterNamesSysV() {
 762         String defaultPrinter = "lp";
 763         String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort";
 764 
 765         String [] names = execCmd(command);
 766         ArrayList printerNames = new ArrayList();
 767         for (int i=0; i < names.length; i++) {
 768             if (!names[i].equals("_default") &&
 769                 !names[i].equals(defaultPrinter) &&
 770                 !names[i].equals("")) {
 771                 printerNames.add(names[i]);
 772             }
 773         }
 774         return (String[])printerNames.toArray(new String[printerNames.size()]);
 775     }
 776 









































 777     static String[] execCmd(final String command) {
 778         ArrayList results = null;
 779         try {
 780             final String[] cmd = new String[3];
 781             if (isSysV()) {
 782                 cmd[0] = "/usr/bin/sh";
 783                 cmd[1] = "-c";
 784                 cmd[2] = "env LC_ALL=C " + command;
 785             } else {
 786                 cmd[0] = "/bin/sh";
 787                 cmd[1] = "-c";
 788                 cmd[2] = "LC_ALL=C " + command;
 789             }
 790 
 791             results = (ArrayList)AccessController.doPrivileged(
 792                 new PrivilegedExceptionAction() {
 793                     public Object run() throws IOException {
 794 
 795                         Process proc;
 796                         BufferedReader bufferedReader = null;
 797                         File f = Files.createTempFile("prn","xc").toFile();
 798                         cmd[2] = cmd[2]+">"+f.getAbsolutePath();
 799 
 800                         proc = Runtime.getRuntime().exec(cmd);
 801                         try {




  61 public class UnixPrintServiceLookup extends PrintServiceLookup
  62     implements BackgroundServiceLookup, Runnable {
  63 
  64     /* Remind: the current implementation is static, as its assumed
  65      * its preferable to minimize creation of PrintService instances.
  66      * Later we should add logic to add/remove services on the fly which
  67      * will take a hit of needing to regather the list of services.
  68      */
  69     private String defaultPrinter;
  70     private PrintService defaultPrintService;
  71     private PrintService[] printServices; /* includes the default printer */
  72     private Vector lookupListeners = null;
  73     private static String debugPrefix = "UnixPrintServiceLookup>> ";
  74     private static boolean pollServices = true;
  75     private static final int DEFAULT_MINREFRESH = 120;  // 2 minutes
  76     private static int minRefreshTime = DEFAULT_MINREFRESH;
  77 
  78 
  79     static String osname;
  80 
  81     // List of commands used to deal with the printer queues on AIX
  82     String[] lpNameComAix = {
  83       "/usr/bin/lsallq",
  84       "/usr/bin/lpstat -W -p|/usr/bin/expand|/usr/bin/cut -f1 -d' '",
  85       "/usr/bin/lpstat -W -d|/usr/bin/expand|/usr/bin/cut -f1 -d' '",
  86       "/usr/bin/lpstat -W -v"
  87     };
  88     private static final int aix_lsallq = 0;
  89     private static final int aix_lpstat_p = 1;
  90     private static final int aix_lpstat_d = 2;
  91     private static final int aix_lpstat_v = 3;
  92     private static int aix_defaultPrinterEnumeration = aix_lsallq;
  93 
  94     static {
  95         /* The system property "sun.java2d.print.polling"
  96          * can be used to force the printing code to poll or not poll
  97          * for PrintServices.
  98          */
  99         String pollStr = java.security.AccessController.doPrivileged(
 100             new sun.security.action.GetPropertyAction("sun.java2d.print.polling"));
 101 
 102         if (pollStr != null) {
 103             if (pollStr.equalsIgnoreCase("true")) {
 104                 pollServices = true;
 105             } else if (pollStr.equalsIgnoreCase("false")) {
 106                 pollServices = false;
 107             }
 108         }
 109 
 110         /* The system property "sun.java2d.print.minRefreshTime"
 111          * can be used to specify minimum refresh time (in seconds)
 112          * for polling PrintServices.  The default is 120.
 113          */
 114         String refreshTimeStr = java.security.AccessController.doPrivileged(
 115             new sun.security.action.GetPropertyAction(
 116                 "sun.java2d.print.minRefreshTime"));
 117 
 118         if (refreshTimeStr != null) {
 119             try {
 120                 minRefreshTime = (new Integer(refreshTimeStr)).intValue();
 121             } catch (NumberFormatException e) {
 122             }
 123             if (minRefreshTime < DEFAULT_MINREFRESH) {
 124                 minRefreshTime = DEFAULT_MINREFRESH;
 125             }
 126         }
 127 
 128         osname = java.security.AccessController.doPrivileged(
 129             new sun.security.action.GetPropertyAction("os.name"));
 130 
 131         /* The system property "sun.java2d.print.aix.lpstat"
 132          * can be used to force the usage of 'lpstat -p' to enumerate all
 133          * printer queues. By default we use 'lsallq', because 'lpstat -p' can
 134          * take lots of time if thousands of printers are attached to a server.
 135          */
 136         if (osname.equals("AIX")) {
 137             String aixPrinterEnumerator = java.security.AccessController.doPrivileged(
 138                 new sun.security.action.GetPropertyAction("sun.java2d.print.aix.lpstat"));
 139 
 140             if (aixPrinterEnumerator != null) {
 141                 if (aixPrinterEnumerator.equalsIgnoreCase("lpstat")) {
 142                     aix_defaultPrinterEnumeration = aix_lpstat_p;
 143                 } else if (aixPrinterEnumerator.equalsIgnoreCase("lsallq")) {
 144                     aix_defaultPrinterEnumeration = aix_lsallq;
 145                 }
 146             }
 147         }
 148     }
 149 
 150     static boolean isMac() {
 151         return osname.startsWith("Mac");
 152     }
 153 
 154     static boolean isSysV() {
 155         return osname.equals("SunOS");
 156     }
 157 
 158     static boolean isLinux() {
 159         return (osname.equals("Linux"));
 160     }
 161 
 162     static boolean isBSD() {
 163         return (osname.equals("Linux") ||
 164                 osname.contains("OS X"));
 165     }
 166 
 167     static boolean isAIX( ) {
 168         return osname.equals("AIX");
 169     }
 170 
 171     static final int UNINITIALIZED = -1;
 172     static final int BSD_LPD = 0;
 173     static final int BSD_LPD_NG = 1;
 174 
 175     static int cmdIndex = UNINITIALIZED;
 176 
 177     String[] lpcFirstCom = {
 178         "/usr/sbin/lpc status | grep : | sed -ne '1,1 s/://p'",
 179         "/usr/sbin/lpc status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
 180     };
 181 
 182     String[] lpcAllCom = {
 183         "/usr/sbin/lpc status all | grep : | sed -e 's/://'",
 184         "/usr/sbin/lpc status all | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}' | sort"
 185     };
 186 
 187     String[] lpcNameCom = {
 188         "| grep : | sed -ne 's/://p'",
 189         "| grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
 190     };


 269 
 270     // refreshes "printServices"
 271     public synchronized void refreshServices() {
 272         /* excludes the default printer */
 273         String[] printers = null; // array of printer names
 274         String[] printerURIs = null; //array of printer URIs
 275 
 276         getDefaultPrintService();
 277         if (CUPSPrinter.isCupsRunning()) {
 278             printerURIs = CUPSPrinter.getAllPrinters();
 279             if ((printerURIs != null) && (printerURIs.length > 0)) {
 280                 printers = new String[printerURIs.length];
 281                 for (int i=0; i<printerURIs.length; i++) {
 282                     int lastIndex = printerURIs[i].lastIndexOf("/");
 283                     printers[i] = printerURIs[i].substring(lastIndex+1);
 284                 }
 285             }
 286         } else {
 287             if (isMac() || isSysV()) {
 288                 printers = getAllPrinterNamesSysV();
 289             } else if (isAIX()) {
 290                 printers = getAllPrinterNamesAIX();
 291             } else { //BSD
 292                 printers = getAllPrinterNamesBSD();
 293             }
 294         }
 295 
 296         if (printers == null) {
 297             if (defaultPrintService != null) {
 298                 printServices = new PrintService[1];
 299                 printServices[0] = defaultPrintService;
 300             } else {
 301                 printServices = null;
 302             }
 303             return;
 304         }
 305 
 306         ArrayList printerList = new ArrayList();
 307         int defaultIndex = -1;
 308         for (int p=0; p<printers.length; p++) {
 309             if (printers[p] == null) {
 310                 continue;


 455             }
 456         }
 457         /* take CUPS into account first */
 458         if (CUPSPrinter.isCupsRunning()) {
 459             try {
 460                 return new IPPPrintService(name,
 461                                            new URL("http://"+
 462                                                    CUPSPrinter.getServer()+":"+
 463                                                    CUPSPrinter.getPort()+"/"+
 464                                                    name));
 465             } catch (Exception e) {
 466                 IPPPrintService.debug_println(debugPrefix+
 467                                               " getServiceByName Exception "+
 468                                               e);
 469             }
 470         }
 471         /* fallback if nothing not having a printer at this point */
 472         PrintService printer = null;
 473         if (isMac() || isSysV()) {
 474             printer = getNamedPrinterNameSysV(name);
 475         } else if (isAIX()) {
 476             printer = getNamedPrinterNameAIX(name);
 477         } else {
 478             printer = getNamedPrinterNameBSD(name);
 479         }
 480         return printer;
 481     }
 482 
 483     private PrintService[]
 484         getPrintServices(PrintServiceAttributeSet serviceSet) {
 485 
 486         if (serviceSet == null || serviceSet.isEmpty()) {
 487             return getPrintServices();
 488         }
 489 
 490         /* Typically expect that if a service attribute is specified that
 491          * its a printer name and there ought to be only one match.
 492          * Directly retrieve that service and confirm
 493          * that it meets the other requirements.
 494          * If printer name isn't mentioned then go a slow path checking
 495          * all printers if they meet the reqiremements.
 496          */


 622 
 623     public synchronized PrintService getDefaultPrintService() {
 624         SecurityManager security = System.getSecurityManager();
 625         if (security != null) {
 626           security.checkPrintJobAccess();
 627         }
 628 
 629         // clear defaultPrintService
 630         defaultPrintService = null;
 631         String psuri = null;
 632 
 633         IPPPrintService.debug_println("isRunning ? "+
 634                                       (CUPSPrinter.isCupsRunning()));
 635         if (CUPSPrinter.isCupsRunning()) {
 636             String[] printerInfo = CUPSPrinter.getDefaultPrinter();
 637             defaultPrinter = printerInfo[0];
 638             psuri = printerInfo[1];
 639         } else {
 640             if (isMac() || isSysV()) {
 641                 defaultPrinter = getDefaultPrinterNameSysV();
 642             } else if (isAIX()) {
 643                 defaultPrinter = getDefaultPrinterNameAIX();
 644             } else {
 645                 defaultPrinter = getDefaultPrinterNameBSD();
 646             }
 647         }
 648         if (defaultPrinter == null) {
 649             return null;
 650         }
 651         defaultPrintService = null;
 652         if (printServices != null) {
 653             for (int j=0; j<printServices.length; j++) {
 654                 if (defaultPrinter.equals(getPrinterDestName(printServices[j]))) {
 655                     defaultPrintService = printServices[j];
 656                     break;
 657                 }
 658             }
 659         }
 660         if (defaultPrintService == null) {
 661             if (CUPSPrinter.isCupsRunning()) {
 662                 try {
 663                     PrintService defaultPS;


 798             return new UnixPrintService(name);
 799         }
 800     }
 801 
 802     private String[] getAllPrinterNamesSysV() {
 803         String defaultPrinter = "lp";
 804         String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort";
 805 
 806         String [] names = execCmd(command);
 807         ArrayList printerNames = new ArrayList();
 808         for (int i=0; i < names.length; i++) {
 809             if (!names[i].equals("_default") &&
 810                 !names[i].equals(defaultPrinter) &&
 811                 !names[i].equals("")) {
 812                 printerNames.add(names[i]);
 813             }
 814         }
 815         return (String[])printerNames.toArray(new String[printerNames.size()]);
 816     }
 817 
 818     private String getDefaultPrinterNameAIX() {
 819         String defaultPrinter = "lp";
 820         String[] names = execCmd(lpNameComAix[aix_lpstat_d]);
 821         // Remove headers and bogus entries added by remote printers.
 822         names = UnixPrintService.filterPrinterNamesAIX(names);
 823         if (names == null || names.length == 0) {
 824             return defaultPrinter;
 825         } else if (names.length != 1) {
 826             // No default printer found
 827             return null;
 828         } else {
 829             return names[0];
 830         }
 831     }
 832 
 833     private PrintService getNamedPrinterNameAIX(String name) {
 834         // On AIX there should be no blank after '-v'.
 835         String[] result = execCmd(lpNameComAix[aix_lpstat_v] + name);
 836         // Remove headers and bogus entries added by remote printers.
 837         result = UnixPrintService.filterPrinterNamesAIX(result);
 838         if (result == null || result.length == 0 || result.length != 1) {
 839             return null;
 840         } else {
 841             return new UnixPrintService(name);
 842         }
 843     }
 844 
 845     private String[] getAllPrinterNamesAIX() {
 846         // Determine all printers of the system.
 847         String [] names = execCmd(lpNameComAix[aix_defaultPrinterEnumeration]);
 848 
 849         // Remove headers and bogus entries added by remote printers.
 850         names = UnixPrintService.filterPrinterNamesAIX(names);
 851 
 852         ArrayList<String> printerNames = new ArrayList<String>();
 853         for ( int i=0; i < names.length; i++) {
 854             printerNames.add(names[i]);
 855         }
 856         return (String[])printerNames.toArray(new String[printerNames.size()]);
 857     }
 858 
 859     static String[] execCmd(final String command) {
 860         ArrayList results = null;
 861         try {
 862             final String[] cmd = new String[3];
 863             if (isSysV() || isAIX()) {
 864                 cmd[0] = "/usr/bin/sh";
 865                 cmd[1] = "-c";
 866                 cmd[2] = "env LC_ALL=C " + command;
 867             } else {
 868                 cmd[0] = "/bin/sh";
 869                 cmd[1] = "-c";
 870                 cmd[2] = "LC_ALL=C " + command;
 871             }
 872 
 873             results = (ArrayList)AccessController.doPrivileged(
 874                 new PrivilegedExceptionAction() {
 875                     public Object run() throws IOException {
 876 
 877                         Process proc;
 878                         BufferedReader bufferedReader = null;
 879                         File f = Files.createTempFile("prn","xc").toFile();
 880                         cmd[2] = cmd[2]+">"+f.getAbsolutePath();
 881 
 882                         proc = Runtime.getRuntime().exec(cmd);
 883                         try {