jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java
changeset 22607 ba232b417248
parent 22584 eed64ee05369
parent 22603 816588059f9d
child 23287 c0f8cdafef56
equal deleted inserted replaced
22585:cb36782f6044 22607:ba232b417248
    76     private static int minRefreshTime = DEFAULT_MINREFRESH;
    76     private static int minRefreshTime = DEFAULT_MINREFRESH;
    77 
    77 
    78 
    78 
    79     static String osname;
    79     static String osname;
    80 
    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 
    81     static {
    94     static {
    82         /* The system property "sun.java2d.print.polling"
    95         /* The system property "sun.java2d.print.polling"
    83          * can be used to force the printing code to poll or not poll
    96          * can be used to force the printing code to poll or not poll
    84          * for PrintServices.
    97          * for PrintServices.
    85          */
    98          */
   112             }
   125             }
   113         }
   126         }
   114 
   127 
   115         osname = java.security.AccessController.doPrivileged(
   128         osname = java.security.AccessController.doPrivileged(
   116             new sun.security.action.GetPropertyAction("os.name"));
   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 (isAIX()) {
       
   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         }
   117     }
   148     }
   118 
   149 
   119     static boolean isMac() {
   150     static boolean isMac() {
   120         return osname.startsWith("Mac");
   151         return osname.startsWith("Mac");
   121     }
   152     }
   129     }
   160     }
   130 
   161 
   131     static boolean isBSD() {
   162     static boolean isBSD() {
   132         return (osname.equals("Linux") ||
   163         return (osname.equals("Linux") ||
   133                 osname.contains("OS X"));
   164                 osname.contains("OS X"));
       
   165     }
       
   166 
       
   167     static boolean isAIX() {
       
   168         return osname.equals("AIX");
   134     }
   169     }
   135 
   170 
   136     static final int UNINITIALIZED = -1;
   171     static final int UNINITIALIZED = -1;
   137     static final int BSD_LPD = 0;
   172     static final int BSD_LPD = 0;
   138     static final int BSD_LPD_NG = 1;
   173     static final int BSD_LPD_NG = 1;
   249                 }
   284                 }
   250             }
   285             }
   251         } else {
   286         } else {
   252             if (isMac() || isSysV()) {
   287             if (isMac() || isSysV()) {
   253                 printers = getAllPrinterNamesSysV();
   288                 printers = getAllPrinterNamesSysV();
       
   289             } else if (isAIX()) {
       
   290                 printers = getAllPrinterNamesAIX();
   254             } else { //BSD
   291             } else { //BSD
   255                 printers = getAllPrinterNamesBSD();
   292                 printers = getAllPrinterNamesBSD();
   256             }
   293             }
   257         }
   294         }
   258 
   295 
   432         }
   469         }
   433         /* fallback if nothing not having a printer at this point */
   470         /* fallback if nothing not having a printer at this point */
   434         PrintService printer = null;
   471         PrintService printer = null;
   435         if (isMac() || isSysV()) {
   472         if (isMac() || isSysV()) {
   436             printer = getNamedPrinterNameSysV(name);
   473             printer = getNamedPrinterNameSysV(name);
       
   474         } else if (isAIX()) {
       
   475             printer = getNamedPrinterNameAIX(name);
   437         } else {
   476         } else {
   438             printer = getNamedPrinterNameBSD(name);
   477             printer = getNamedPrinterNameBSD(name);
   439         }
   478         }
   440         return printer;
   479         return printer;
   441     }
   480     }
   596             defaultPrinter = printerInfo[0];
   635             defaultPrinter = printerInfo[0];
   597             psuri = printerInfo[1];
   636             psuri = printerInfo[1];
   598         } else {
   637         } else {
   599             if (isMac() || isSysV()) {
   638             if (isMac() || isSysV()) {
   600                 defaultPrinter = getDefaultPrinterNameSysV();
   639                 defaultPrinter = getDefaultPrinterNameSysV();
       
   640             } else if (isAIX()) {
       
   641                 defaultPrinter = getDefaultPrinterNameAIX();
   601             } else {
   642             } else {
   602                 defaultPrinter = getDefaultPrinterNameBSD();
   643                 defaultPrinter = getDefaultPrinterNameBSD();
   603             }
   644             }
   604         }
   645         }
   605         if (defaultPrinter == null) {
   646         if (defaultPrinter == null) {
   770             }
   811             }
   771         }
   812         }
   772         return (String[])printerNames.toArray(new String[printerNames.size()]);
   813         return (String[])printerNames.toArray(new String[printerNames.size()]);
   773     }
   814     }
   774 
   815 
       
   816     private String getDefaultPrinterNameAIX() {
       
   817         String[] names = execCmd(lpNameComAix[aix_lpstat_d]);
       
   818         // Remove headers and bogus entries added by remote printers.
       
   819         names = UnixPrintService.filterPrinterNamesAIX(names);
       
   820         if (names == null || names.length != 1) {
       
   821             // No default printer found
       
   822             return null;
       
   823         } else {
       
   824             return names[0];
       
   825         }
       
   826     }
       
   827 
       
   828     private PrintService getNamedPrinterNameAIX(String name) {
       
   829         // On AIX there should be no blank after '-v'.
       
   830         String[] result = execCmd(lpNameComAix[aix_lpstat_v] + name);
       
   831         // Remove headers and bogus entries added by remote printers.
       
   832         result = UnixPrintService.filterPrinterNamesAIX(result);
       
   833         if (result == null || result.length != 1) {
       
   834             return null;
       
   835         } else {
       
   836             return new UnixPrintService(name);
       
   837         }
       
   838     }
       
   839 
       
   840     private String[] getAllPrinterNamesAIX() {
       
   841         // Determine all printers of the system.
       
   842         String [] names = execCmd(lpNameComAix[aix_defaultPrinterEnumeration]);
       
   843 
       
   844         // Remove headers and bogus entries added by remote printers.
       
   845         names = UnixPrintService.filterPrinterNamesAIX(names);
       
   846 
       
   847         ArrayList<String> printerNames = new ArrayList<String>();
       
   848         for ( int i=0; i < names.length; i++) {
       
   849             printerNames.add(names[i]);
       
   850         }
       
   851         return printerNames.toArray(new String[printerNames.size()]);
       
   852     }
       
   853 
   775     static String[] execCmd(final String command) {
   854     static String[] execCmd(final String command) {
   776         ArrayList results = null;
   855         ArrayList results = null;
   777         try {
   856         try {
   778             final String[] cmd = new String[3];
   857             final String[] cmd = new String[3];
   779             if (isSysV()) {
   858             if (isSysV() || isAIX()) {
   780                 cmd[0] = "/usr/bin/sh";
   859                 cmd[0] = "/usr/bin/sh";
   781                 cmd[1] = "-c";
   860                 cmd[1] = "-c";
   782                 cmd[2] = "env LC_ALL=C " + command;
   861                 cmd[2] = "env LC_ALL=C " + command;
   783             } else {
   862             } else {
   784                 cmd[0] = "/bin/sh";
   863                 cmd[0] = "/bin/sh";