< prev index next >

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

Print this page
rev 59383 : [mq]: final
   1 /*
   2  * Copyright (c) 2000, 2019, 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


 205         }
 206         printer = 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") &&


 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();
 329         } else if (PrintServiceLookupProvider.isBSD()) {
 330             return getPrinterIsAcceptingJobsBSD();
 331         } else if (PrintServiceLookupProvider.isAIX()) {
 332             return getPrinterIsAcceptingJobsAIX();
 333         } else {
 334             return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 335         }
 336     }
 337 
 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();


 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();
 424         } else if (PrintServiceLookupProvider.isAIX()) {
 425             return getQueuedJobCountAIX();
 426         } else {
 427             return new QueuedJobCount(0);
 428         }
 429     }
 430 
 431     private PrintServiceAttributeSet getSysVServiceAttributes() {
 432         PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
 433         attrs.add(getQueuedJobCountSysV());
 434         attrs.add(getPrinterIsAcceptingJobsSysV());
 435         return attrs;
 436     }
 437 
 438     private PrintServiceAttributeSet getBSDServiceAttributes() {
 439         PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
 440         attrs.add(getQueuedJobCountBSD());
 441         attrs.add(getPrinterIsAcceptingJobsBSD());
 442         return attrs;
 443     }
 444 
 445     private PrintServiceAttributeSet getAIXServiceAttributes() {
 446         PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
 447         attrs.add(getQueuedJobCountAIX());
 448         attrs.add(getPrinterIsAcceptingJobsAIX());
 449         return attrs;
 450     }
 451 
 452     private boolean isSupportedCopies(Copies copies) {
 453         int numCopies = copies.getValue();
 454         return (numCopies > 0 && numCopies < MAXCOPIES);
 455     }
 456 
 457     private boolean isSupportedMedia(MediaSizeName msn) {
 458         for (int i=0; i<mediaSizes.length; i++) {
 459             if (msn.equals(mediaSizes[i])) {
 460                 return true;
 461             }
 462         }
 463         return false;
 464     }
 465 
 466     public DocPrintJob createPrintJob() {
 467       SecurityManager security = System.getSecurityManager();
 468       if (security != null) {
 469         security.checkPrintJobAccess();
 470       }
 471         return new UnixPrintJob(this);
 472     }
 473 
 474     private PrintServiceAttributeSet getDynamicAttributes() {
 475         if (PrintServiceLookupProvider.isSysV()) {
 476             return getSysVServiceAttributes();
 477         } else if (PrintServiceLookupProvider.isAIX()) {
 478             return getAIXServiceAttributes();
 479         } else {
 480             return getBSDServiceAttributes();
 481         }
 482     }
 483 
 484     public PrintServiceAttributeSet getUpdatedAttributes() {
 485         PrintServiceAttributeSet currSet = getDynamicAttributes();
 486         if (lastSet == null) {
 487             lastSet = currSet;
 488             return AttributeSetUtilities.unmodifiableView(currSet);
 489         } else {
 490             PrintServiceAttributeSet updates =
 491                 new HashPrintServiceAttributeSet();
 492             Attribute []attrs = currSet.toArray();
 493             Attribute attr;
 494             for (int i=0; i<attrs.length; i++) {
 495                 attr = attrs[i];
 496                 if (!lastSet.containsValue(attr)) {
 497                     updates.add(attr);


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


 205         }
 206         printer = 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 getPrinterIsAcceptingJobsBSD() {
 226         if (PrintServiceLookupProvider.cmdIndex ==
 227             PrintServiceLookupProvider.UNINITIALIZED) {
 228 
 229             PrintServiceLookupProvider.cmdIndex =
 230                 PrintServiceLookupProvider.getBSDCommandIndex();
 231         }
 232 
 233         String command = "/usr/sbin/lpc status " + printer
 234             + lpcStatusCom[PrintServiceLookupProvider.cmdIndex];
 235         String[] results= PrintServiceLookupProvider.execCmd(command);
 236 
 237         if (results != null && results.length > 0) {
 238             if (PrintServiceLookupProvider.cmdIndex ==
 239                 PrintServiceLookupProvider.BSD_LPD_NG) {
 240                 if (results[0].startsWith("enabled enabled")) {
 241                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS ;
 242                 }
 243             } else {
 244                 if ((results[1].trim().startsWith("queuing is enabled") &&


 282         String command = "/usr/bin/lpstat -a" + printer;
 283         String[] results= PrintServiceLookupProvider.execCmd(command);
 284 
 285         // Remove headers and bogus entries added by remote printers.
 286         results = filterPrinterNamesAIX(results);
 287 
 288         if (results != null && results.length > 0) {
 289             for (int i = 0; i < results.length; i++) {
 290                 if (results[i].contains("READY") ||
 291                     results[i].contains("RUNNING")) {
 292                     return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 293                 }
 294             }
 295         }
 296 
 297         return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS;
 298 
 299     }
 300 
 301     private PrinterIsAcceptingJobs getPrinterIsAcceptingJobs() {
 302         if (PrintServiceLookupProvider.isBSD()) {


 303             return getPrinterIsAcceptingJobsBSD();
 304         } else if (PrintServiceLookupProvider.isAIX()) {
 305             return getPrinterIsAcceptingJobsAIX();
 306         } else {
 307             return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
 308         }
 309     }
 310 
 311     private PrinterState getPrinterState() {
 312         if (isInvalid) {
 313             return PrinterState.STOPPED;
 314         } else {
 315             return null;
 316         }
 317     }
 318 
 319     private PrinterStateReasons getPrinterStateReasons() {
 320         if (isInvalid) {
 321             PrinterStateReasons psr = new PrinterStateReasons();
 322             psr.put(PrinterStateReason.SHUTDOWN, Severity.ERROR);
 323             return psr;
 324         } else {
 325             return null;
 326         }
 327     }
 328 








 329     private QueuedJobCount getQueuedJobCountBSD() {
 330         if (PrintServiceLookupProvider.cmdIndex ==
 331             PrintServiceLookupProvider.UNINITIALIZED) {
 332 
 333             PrintServiceLookupProvider.cmdIndex =
 334                 PrintServiceLookupProvider.getBSDCommandIndex();
 335         }
 336 
 337         int qlen = 0;
 338         String command = "/usr/sbin/lpc status " + printer
 339             + lpcQueueCom[PrintServiceLookupProvider.cmdIndex];
 340         String[] results = PrintServiceLookupProvider.execCmd(command);
 341 
 342         if (results != null && results.length > 0) {
 343             String queued;
 344             if (PrintServiceLookupProvider.cmdIndex ==
 345                 PrintServiceLookupProvider.BSD_LPD_NG) {
 346                 queued = results[0];
 347             } else {
 348                 queued = results[3].trim();


 365     private QueuedJobCount getQueuedJobCountAIX() {
 366         // On AIX there should not be a blank after '-a'.
 367         String command = "/usr/bin/lpstat -a" + printer;
 368         String[] results=  PrintServiceLookupProvider.execCmd(command);
 369 
 370         // Remove headers and bogus entries added by remote printers.
 371         results = filterPrinterNamesAIX(results);
 372 
 373         int qlen = 0;
 374         if (results != null && results.length > 0){
 375             for (int i = 0; i < results.length; i++) {
 376                 if (results[i].contains("QUEUED")){
 377                     qlen ++;
 378                 }
 379             }
 380         }
 381         return new QueuedJobCount(qlen);
 382     }
 383 
 384     private QueuedJobCount getQueuedJobCount() {
 385         if (PrintServiceLookupProvider.isBSD()) {


 386             return getQueuedJobCountBSD();
 387         } else if (PrintServiceLookupProvider.isAIX()) {
 388             return getQueuedJobCountAIX();
 389         } else {
 390             return new QueuedJobCount(0);
 391         }
 392     }
 393 







 394     private PrintServiceAttributeSet getBSDServiceAttributes() {
 395         PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
 396         attrs.add(getQueuedJobCountBSD());
 397         attrs.add(getPrinterIsAcceptingJobsBSD());
 398         return attrs;
 399     }
 400 
 401     private PrintServiceAttributeSet getAIXServiceAttributes() {
 402         PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
 403         attrs.add(getQueuedJobCountAIX());
 404         attrs.add(getPrinterIsAcceptingJobsAIX());
 405         return attrs;
 406     }
 407 
 408     private boolean isSupportedCopies(Copies copies) {
 409         int numCopies = copies.getValue();
 410         return (numCopies > 0 && numCopies < MAXCOPIES);
 411     }
 412 
 413     private boolean isSupportedMedia(MediaSizeName msn) {
 414         for (int i=0; i<mediaSizes.length; i++) {
 415             if (msn.equals(mediaSizes[i])) {
 416                 return true;
 417             }
 418         }
 419         return false;
 420     }
 421 
 422     public DocPrintJob createPrintJob() {
 423       SecurityManager security = System.getSecurityManager();
 424       if (security != null) {
 425         security.checkPrintJobAccess();
 426       }
 427         return new UnixPrintJob(this);
 428     }
 429 
 430     private PrintServiceAttributeSet getDynamicAttributes() {
 431         if (PrintServiceLookupProvider.isAIX()) {


 432             return getAIXServiceAttributes();
 433         } else {
 434             return getBSDServiceAttributes();
 435         }
 436     }
 437 
 438     public PrintServiceAttributeSet getUpdatedAttributes() {
 439         PrintServiceAttributeSet currSet = getDynamicAttributes();
 440         if (lastSet == null) {
 441             lastSet = currSet;
 442             return AttributeSetUtilities.unmodifiableView(currSet);
 443         } else {
 444             PrintServiceAttributeSet updates =
 445                 new HashPrintServiceAttributeSet();
 446             Attribute []attrs = currSet.toArray();
 447             Attribute attr;
 448             for (int i=0; i<attrs.length; i++) {
 449                 attr = attrs[i];
 450                 if (!lastSet.containsValue(attr)) {
 451                     updates.add(attr);


< prev index next >