< prev index next >

src/java.desktop/unix/classes/sun/print/UnixPrintService.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2007, 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


 162      *  separately because many attributes are in multiple categories.
 163      */
 164     private static final Class<?>[] otherAttrCats = {
 165         Chromaticity.class,
 166         Copies.class,
 167         Destination.class,
 168         Fidelity.class,
 169         JobName.class,
 170         JobSheets.class,
 171         Media.class, /* have to support this somehow ... */
 172         MediaPrintableArea.class,
 173         OrientationRequested.class,
 174         PageRanges.class,
 175         RequestingUserName.class,
 176         SheetCollate.class,
 177         Sides.class,
 178     };
 179 
 180     private static int MAXCOPIES = 1000;
 181 
 182     private static final MediaSizeName mediaSizes[] = {
 183         MediaSizeName.NA_LETTER,
 184         MediaSizeName.TABLOID,
 185         MediaSizeName.LEDGER,
 186         MediaSizeName.NA_LEGAL,
 187         MediaSizeName.EXECUTIVE,
 188         MediaSizeName.ISO_A3,
 189         MediaSizeName.ISO_A4,
 190         MediaSizeName.ISO_A5,
 191         MediaSizeName.ISO_B4,
 192         MediaSizeName.ISO_B5,
 193     };
 194 
 195     private String printer;
 196     private PrinterName name;
 197     private boolean isInvalid;
 198 
 199     private transient PrintServiceAttributeSet lastSet;
 200     private transient ServiceNotifier notifier = null;
 201 
 202     UnixPrintService(String name) {


 207         isInvalid = false;
 208     }
 209 
 210     public void invalidateService() {
 211         isInvalid = true;
 212     }
 213 
 214     public String getName() {
 215         return printer;
 216     }
 217 
 218     private PrinterName getPrinterName() {
 219         if (name == null) {
 220             name = new PrinterName(printer, null);
 221         }
 222         return name;
 223     }
 224 
 225     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobsSysV() {
 226         String command = "/usr/bin/lpstat -a " + printer;
 227         String results[]= PrintServiceLookupProvider.execCmd(command);
 228 
 229         if (results != null && results.length > 0) {
 230             if (results[0].startsWith(printer + " accepting requests")) {
 231                 return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 232             }
 233             else if (results[0].startsWith(printer)) {
 234                 /* As well as "myprinter accepting requests", look for
 235                  * "myprinter@somehost accepting requests".
 236                  */
 237                 int index = printer.length();
 238                 String str = results[0];
 239                 if (str.length() > index &&
 240                     str.charAt(index) == '@' &&
 241                     str.indexOf(" accepting requests", index) > 0 &&
 242                     str.indexOf(" not accepting requests", index) == -1) {
 243                    return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 244                 }
 245             }
 246         }
 247         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS ;
 248     }
 249 
 250     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobsBSD() {
 251         if (PrintServiceLookupProvider.cmdIndex ==
 252             PrintServiceLookupProvider.UNINITIALIZED) {
 253 
 254             PrintServiceLookupProvider.cmdIndex =
 255                 PrintServiceLookupProvider.getBSDCommandIndex();
 256         }
 257 
 258         String command = "/usr/sbin/lpc status " + printer
 259             + lpcStatusCom[PrintServiceLookupProvider.cmdIndex];
 260         String results[]= PrintServiceLookupProvider.execCmd(command);
 261 
 262         if (results != null && results.length > 0) {
 263             if (PrintServiceLookupProvider.cmdIndex ==
 264                 PrintServiceLookupProvider.BSD_LPD_NG) {
 265                 if (results[0].startsWith("enabled enabled")) {
 266                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS ;
 267                 }
 268             } else {
 269                 if ((results[1].trim().startsWith("queuing is enabled") &&
 270                     results[2].trim().startsWith("printing is enabled")) ||
 271                     (results.length >= 4 &&
 272                      results[2].trim().startsWith("queuing is enabled") &&
 273                      results[3].trim().startsWith("printing is enabled"))) {
 274                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS ;
 275                 }
 276             }
 277         }
 278         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS ;
 279     }
 280 


 288         for(int i = 0; i < posPrinters.length; i++) {
 289             // Remove the header lines
 290             if (posPrinters[i].startsWith("---") ||
 291                 posPrinters[i].startsWith("Queue") ||
 292                 posPrinters[i].equals("")) continue;
 293 
 294             // Check if there is a ":" in the end of the first colomn.
 295             // This means that it is not a valid printer definition.
 296             splitPart = posPrinters[i].split(" ");
 297             if(splitPart.length >= 1 && !splitPart[0].trim().endsWith(":")) {
 298                 printers.add(posPrinters[i]);
 299             }
 300         }
 301 
 302         return printers.toArray(new String[printers.size()]);
 303     }
 304 
 305     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobsAIX() {
 306         // On AIX there should not be a blank after '-a'.
 307         String command = "/usr/bin/lpstat -a" + printer;
 308         String results[]= PrintServiceLookupProvider.execCmd(command);
 309 
 310         // Remove headers and bogus entries added by remote printers.
 311         results = filterPrinterNamesAIX(results);
 312 
 313         if (results != null && results.length > 0) {
 314             for (int i = 0; i < results.length; i++) {
 315                 if (results[i].contains("READY") ||
 316                     results[i].contains("RUNNING")) {
 317                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 318                 }
 319             }
 320         }
 321 
 322         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS;
 323 
 324     }
 325 
 326     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobs() {
 327         if (PrintServiceLookupProvider.isSysV()) {
 328             return getPrinterIsAcceptingJobsSysV();


 338     private PrinterState getPrinterState() {
 339         if (isInvalid) {
 340             return PrinterState.STOPPED;
 341         } else {
 342             return null;
 343         }
 344     }
 345 
 346     private PrinterStateReasons getPrinterStateReasons() {
 347         if (isInvalid) {
 348             PrinterStateReasons psr = new PrinterStateReasons();
 349             psr.put(PrinterStateReason.SHUTDOWN, Severity.ERROR);
 350             return psr;
 351         } else {
 352             return null;
 353         }
 354     }
 355 
 356     private QueuedJobCount getQueuedJobCountSysV() {
 357         String command = "/usr/bin/lpstat -R " + printer;
 358         String results[]= PrintServiceLookupProvider.execCmd(command);
 359         int qlen = (results == null) ? 0 : results.length;
 360 
 361         return new QueuedJobCount(qlen);
 362     }
 363 
 364     private QueuedJobCount getQueuedJobCountBSD() {
 365         if (PrintServiceLookupProvider.cmdIndex ==
 366             PrintServiceLookupProvider.UNINITIALIZED) {
 367 
 368             PrintServiceLookupProvider.cmdIndex =
 369                 PrintServiceLookupProvider.getBSDCommandIndex();
 370         }
 371 
 372         int qlen = 0;
 373         String command = "/usr/sbin/lpc status " + printer
 374             + lpcQueueCom[PrintServiceLookupProvider.cmdIndex];
 375         String results[] = PrintServiceLookupProvider.execCmd(command);
 376 
 377         if (results != null && results.length > 0) {
 378             String queued;
 379             if (PrintServiceLookupProvider.cmdIndex ==
 380                 PrintServiceLookupProvider.BSD_LPD_NG) {
 381                 queued = results[0];
 382             } else {
 383                 queued = results[3].trim();
 384                 if (queued.startsWith("no")) {
 385                     return new QueuedJobCount(0);
 386                 } else {
 387                     queued = queued.substring(0, queued.indexOf(' '));
 388                 }
 389             }
 390 
 391             try {
 392                 qlen = Integer.parseInt(queued);
 393             } catch (NumberFormatException e) {
 394             }
 395         }
 396 
 397         return new QueuedJobCount(qlen);
 398     }
 399 
 400     private QueuedJobCount getQueuedJobCountAIX() {
 401         // On AIX there should not be a blank after '-a'.
 402         String command = "/usr/bin/lpstat -a" + printer;
 403         String results[]=  PrintServiceLookupProvider.execCmd(command);
 404 
 405         // Remove headers and bogus entries added by remote printers.
 406         results = filterPrinterNamesAIX(results);
 407 
 408         int qlen = 0;
 409         if (results != null && results.length > 0){
 410             for (int i = 0; i < results.length; i++) {
 411                 if (results[i].contains("QUEUED")){
 412                     qlen ++;
 413                 }
 414             }
 415         }
 416         return new QueuedJobCount(qlen);
 417     }
 418 
 419     private QueuedJobCount getQueuedJobCount() {
 420         if (PrintServiceLookupProvider.isSysV()) {
 421             return getQueuedJobCountSysV();
 422         } else if (PrintServiceLookupProvider.isBSD()) {
 423             return getQueuedJobCountBSD();


 777             if (flavor == null || isServiceFormattedFlavor(flavor)) {
 778                 Chromaticity[]arr = new Chromaticity[1];
 779                 arr[0] = Chromaticity.COLOR;
 780                 return (arr);
 781             } else {
 782                 return null;
 783             }
 784         } else if (category == Destination.class) {
 785             try {
 786                 return new Destination((new File("out.ps")).toURI());
 787             } catch (SecurityException se) {
 788                 try {
 789                     return new Destination(new URI("file:out.ps"));
 790                 } catch (URISyntaxException e) {
 791                     return null;
 792                 }
 793             }
 794         } else if (category == JobName.class) {
 795             return new JobName("Java Printing", null);
 796         } else if (category == JobSheets.class) {
 797             JobSheets arr[] = new JobSheets[2];
 798             arr[0] = JobSheets.NONE;
 799             arr[1] = JobSheets.STANDARD;
 800             return arr;
 801         } else if (category == RequestingUserName.class) {
 802             String userName = "";
 803             try {
 804               userName = System.getProperty("user.name", "");
 805             } catch (SecurityException se) {
 806             }
 807             return new RequestingUserName(userName, null);
 808         } else if (category == OrientationRequested.class) {
 809             if (flavor == null || isServiceFormattedFlavor(flavor)) {
 810                 OrientationRequested []arr = new OrientationRequested[3];
 811                 arr[0] = OrientationRequested.PORTRAIT;
 812                 arr[1] = OrientationRequested.LANDSCAPE;
 813                 arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
 814                 return arr;
 815             } else {
 816                 return null;
 817             }


   1 /*
   2  * Copyright (c) 2000, 2018, 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


 162      *  separately because many attributes are in multiple categories.
 163      */
 164     private static final Class<?>[] otherAttrCats = {
 165         Chromaticity.class,
 166         Copies.class,
 167         Destination.class,
 168         Fidelity.class,
 169         JobName.class,
 170         JobSheets.class,
 171         Media.class, /* have to support this somehow ... */
 172         MediaPrintableArea.class,
 173         OrientationRequested.class,
 174         PageRanges.class,
 175         RequestingUserName.class,
 176         SheetCollate.class,
 177         Sides.class,
 178     };
 179 
 180     private static int MAXCOPIES = 1000;
 181 
 182     private static final MediaSizeName[] mediaSizes = {
 183         MediaSizeName.NA_LETTER,
 184         MediaSizeName.TABLOID,
 185         MediaSizeName.LEDGER,
 186         MediaSizeName.NA_LEGAL,
 187         MediaSizeName.EXECUTIVE,
 188         MediaSizeName.ISO_A3,
 189         MediaSizeName.ISO_A4,
 190         MediaSizeName.ISO_A5,
 191         MediaSizeName.ISO_B4,
 192         MediaSizeName.ISO_B5,
 193     };
 194 
 195     private String printer;
 196     private PrinterName name;
 197     private boolean isInvalid;
 198 
 199     private transient PrintServiceAttributeSet lastSet;
 200     private transient ServiceNotifier notifier = null;
 201 
 202     UnixPrintService(String name) {


 207         isInvalid = false;
 208     }
 209 
 210     public void invalidateService() {
 211         isInvalid = true;
 212     }
 213 
 214     public String getName() {
 215         return printer;
 216     }
 217 
 218     private PrinterName getPrinterName() {
 219         if (name == null) {
 220             name = new PrinterName(printer, null);
 221         }
 222         return name;
 223     }
 224 
 225     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobsSysV() {
 226         String command = "/usr/bin/lpstat -a " + printer;
 227         String[] results= PrintServiceLookupProvider.execCmd(command);
 228 
 229         if (results != null && results.length > 0) {
 230             if (results[0].startsWith(printer + " accepting requests")) {
 231                 return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 232             }
 233             else if (results[0].startsWith(printer)) {
 234                 /* As well as "myprinter accepting requests", look for
 235                  * "myprinter@somehost accepting requests".
 236                  */
 237                 int index = printer.length();
 238                 String str = results[0];
 239                 if (str.length() > index &&
 240                     str.charAt(index) == '@' &&
 241                     str.indexOf(" accepting requests", index) > 0 &&
 242                     str.indexOf(" not accepting requests", index) == -1) {
 243                    return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 244                 }
 245             }
 246         }
 247         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS ;
 248     }
 249 
 250     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobsBSD() {
 251         if (PrintServiceLookupProvider.cmdIndex ==
 252             PrintServiceLookupProvider.UNINITIALIZED) {
 253 
 254             PrintServiceLookupProvider.cmdIndex =
 255                 PrintServiceLookupProvider.getBSDCommandIndex();
 256         }
 257 
 258         String command = "/usr/sbin/lpc status " + printer
 259             + lpcStatusCom[PrintServiceLookupProvider.cmdIndex];
 260         String[] results= PrintServiceLookupProvider.execCmd(command);
 261 
 262         if (results != null && results.length > 0) {
 263             if (PrintServiceLookupProvider.cmdIndex ==
 264                 PrintServiceLookupProvider.BSD_LPD_NG) {
 265                 if (results[0].startsWith("enabled enabled")) {
 266                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS ;
 267                 }
 268             } else {
 269                 if ((results[1].trim().startsWith("queuing is enabled") &&
 270                     results[2].trim().startsWith("printing is enabled")) ||
 271                     (results.length >= 4 &&
 272                      results[2].trim().startsWith("queuing is enabled") &&
 273                      results[3].trim().startsWith("printing is enabled"))) {
 274                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS ;
 275                 }
 276             }
 277         }
 278         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS ;
 279     }
 280 


 288         for(int i = 0; i < posPrinters.length; i++) {
 289             // Remove the header lines
 290             if (posPrinters[i].startsWith("---") ||
 291                 posPrinters[i].startsWith("Queue") ||
 292                 posPrinters[i].equals("")) continue;
 293 
 294             // Check if there is a ":" in the end of the first colomn.
 295             // This means that it is not a valid printer definition.
 296             splitPart = posPrinters[i].split(" ");
 297             if(splitPart.length >= 1 && !splitPart[0].trim().endsWith(":")) {
 298                 printers.add(posPrinters[i]);
 299             }
 300         }
 301 
 302         return printers.toArray(new String[printers.size()]);
 303     }
 304 
 305     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobsAIX() {
 306         // On AIX there should not be a blank after '-a'.
 307         String command = "/usr/bin/lpstat -a" + printer;
 308         String[] results= PrintServiceLookupProvider.execCmd(command);
 309 
 310         // Remove headers and bogus entries added by remote printers.
 311         results = filterPrinterNamesAIX(results);
 312 
 313         if (results != null && results.length > 0) {
 314             for (int i = 0; i < results.length; i++) {
 315                 if (results[i].contains("READY") ||
 316                     results[i].contains("RUNNING")) {
 317                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 318                 }
 319             }
 320         }
 321 
 322         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS;
 323 
 324     }
 325 
 326     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobs() {
 327         if (PrintServiceLookupProvider.isSysV()) {
 328             return getPrinterIsAcceptingJobsSysV();


 338     private PrinterState getPrinterState() {
 339         if (isInvalid) {
 340             return PrinterState.STOPPED;
 341         } else {
 342             return null;
 343         }
 344     }
 345 
 346     private PrinterStateReasons getPrinterStateReasons() {
 347         if (isInvalid) {
 348             PrinterStateReasons psr = new PrinterStateReasons();
 349             psr.put(PrinterStateReason.SHUTDOWN, Severity.ERROR);
 350             return psr;
 351         } else {
 352             return null;
 353         }
 354     }
 355 
 356     private QueuedJobCount getQueuedJobCountSysV() {
 357         String command = "/usr/bin/lpstat -R " + printer;
 358         String[] results= PrintServiceLookupProvider.execCmd(command);
 359         int qlen = (results == null) ? 0 : results.length;
 360 
 361         return new QueuedJobCount(qlen);
 362     }
 363 
 364     private QueuedJobCount getQueuedJobCountBSD() {
 365         if (PrintServiceLookupProvider.cmdIndex ==
 366             PrintServiceLookupProvider.UNINITIALIZED) {
 367 
 368             PrintServiceLookupProvider.cmdIndex =
 369                 PrintServiceLookupProvider.getBSDCommandIndex();
 370         }
 371 
 372         int qlen = 0;
 373         String command = "/usr/sbin/lpc status " + printer
 374             + lpcQueueCom[PrintServiceLookupProvider.cmdIndex];
 375         String[] results = PrintServiceLookupProvider.execCmd(command);
 376 
 377         if (results != null && results.length > 0) {
 378             String queued;
 379             if (PrintServiceLookupProvider.cmdIndex ==
 380                 PrintServiceLookupProvider.BSD_LPD_NG) {
 381                 queued = results[0];
 382             } else {
 383                 queued = results[3].trim();
 384                 if (queued.startsWith("no")) {
 385                     return new QueuedJobCount(0);
 386                 } else {
 387                     queued = queued.substring(0, queued.indexOf(' '));
 388                 }
 389             }
 390 
 391             try {
 392                 qlen = Integer.parseInt(queued);
 393             } catch (NumberFormatException e) {
 394             }
 395         }
 396 
 397         return new QueuedJobCount(qlen);
 398     }
 399 
 400     private QueuedJobCount getQueuedJobCountAIX() {
 401         // On AIX there should not be a blank after '-a'.
 402         String command = "/usr/bin/lpstat -a" + printer;
 403         String[] results=  PrintServiceLookupProvider.execCmd(command);
 404 
 405         // Remove headers and bogus entries added by remote printers.
 406         results = filterPrinterNamesAIX(results);
 407 
 408         int qlen = 0;
 409         if (results != null && results.length > 0){
 410             for (int i = 0; i < results.length; i++) {
 411                 if (results[i].contains("QUEUED")){
 412                     qlen ++;
 413                 }
 414             }
 415         }
 416         return new QueuedJobCount(qlen);
 417     }
 418 
 419     private QueuedJobCount getQueuedJobCount() {
 420         if (PrintServiceLookupProvider.isSysV()) {
 421             return getQueuedJobCountSysV();
 422         } else if (PrintServiceLookupProvider.isBSD()) {
 423             return getQueuedJobCountBSD();


 777             if (flavor == null || isServiceFormattedFlavor(flavor)) {
 778                 Chromaticity[]arr = new Chromaticity[1];
 779                 arr[0] = Chromaticity.COLOR;
 780                 return (arr);
 781             } else {
 782                 return null;
 783             }
 784         } else if (category == Destination.class) {
 785             try {
 786                 return new Destination((new File("out.ps")).toURI());
 787             } catch (SecurityException se) {
 788                 try {
 789                     return new Destination(new URI("file:out.ps"));
 790                 } catch (URISyntaxException e) {
 791                     return null;
 792                 }
 793             }
 794         } else if (category == JobName.class) {
 795             return new JobName("Java Printing", null);
 796         } else if (category == JobSheets.class) {
 797             JobSheets[] arr = new JobSheets[2];
 798             arr[0] = JobSheets.NONE;
 799             arr[1] = JobSheets.STANDARD;
 800             return arr;
 801         } else if (category == RequestingUserName.class) {
 802             String userName = "";
 803             try {
 804               userName = System.getProperty("user.name", "");
 805             } catch (SecurityException se) {
 806             }
 807             return new RequestingUserName(userName, null);
 808         } else if (category == OrientationRequested.class) {
 809             if (flavor == null || isServiceFormattedFlavor(flavor)) {
 810                 OrientationRequested []arr = new OrientationRequested[3];
 811                 arr[0] = OrientationRequested.PORTRAIT;
 812                 arr[1] = OrientationRequested.LANDSCAPE;
 813                 arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
 814                 return arr;
 815             } else {
 816                 return null;
 817             }


< prev index next >