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
843 }
844 } else if (category == OrientationRequested.class) {
845 orient = (OrientationRequested)attr;
846 } else if (category == NumberUp.class) {
847 nUp = (NumberUp)attr;
848 } else if (category == Sides.class) {
849 sides = (Sides)attr;
850 }
851 }
852 }
853
854 private String[] printExecCmd(String printer, String options,
855 boolean noJobSheet,
856 String jobTitle, int copies, String spoolFile) {
857 int PRINTER = 0x1;
858 int OPTIONS = 0x2;
859 int JOBTITLE = 0x4;
860 int COPIES = 0x8;
861 int NOSHEET = 0x10;
862 int pFlags = 0;
863 String execCmd[];
864 int ncomps = 2; // minimum number of print args
865 int n = 0;
866
867 // conveniently "lp" is the default destination for both lp and lpr.
868 if (printer != null && !printer.equals("") && !printer.equals("lp")) {
869 pFlags |= PRINTER;
870 ncomps+=1;
871 }
872 if (options != null && !options.equals("")) {
873 pFlags |= OPTIONS;
874 ncomps+=1;
875 }
876 if (jobTitle != null && !jobTitle.equals("")) {
877 pFlags |= JOBTITLE;
878 ncomps+=1;
879 }
880 if (copies > 1) {
881 pFlags |= COPIES;
882 ncomps+=1;
883 }
1005 pw.append("\t\t").append(br.readLine());
1006 }
1007 } finally {
1008 pw.flush();
1009 }
1010 throw new IOException(sw.toString());
1011 }
1012 }
1013
1014 public Object run() {
1015 if (spoolFile == null || !spoolFile.exists()) {
1016 pex = new PrintException("No spool file");
1017 notifyEvent(PrintJobEvent.JOB_FAILED);
1018 return null;
1019 }
1020 try {
1021 /**
1022 * Spool to the printer.
1023 */
1024 String fileName = spoolFile.getAbsolutePath();
1025 String execCmd[] = printExecCmd(mDestination, mOptions,
1026 mNoJobSheet, jobName, copies, fileName);
1027
1028 Process process = Runtime.getRuntime().exec(execCmd);
1029 process.waitFor();
1030 final int result = process.exitValue();
1031 if (0 != result) {
1032 handleProcessFailure(process, execCmd, result);
1033 }
1034 notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
1035 } catch (IOException ex) {
1036 notifyEvent(PrintJobEvent.JOB_FAILED);
1037 // REMIND : 2d printing throws PrinterException
1038 pex = new PrintException(ex);
1039 } catch (InterruptedException ie) {
1040 notifyEvent(PrintJobEvent.JOB_FAILED);
1041 pex = new PrintException(ie);
1042 } finally {
1043 spoolFile.delete();
1044 notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
1045 }
|
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
843 }
844 } else if (category == OrientationRequested.class) {
845 orient = (OrientationRequested)attr;
846 } else if (category == NumberUp.class) {
847 nUp = (NumberUp)attr;
848 } else if (category == Sides.class) {
849 sides = (Sides)attr;
850 }
851 }
852 }
853
854 private String[] printExecCmd(String printer, String options,
855 boolean noJobSheet,
856 String jobTitle, int copies, String spoolFile) {
857 int PRINTER = 0x1;
858 int OPTIONS = 0x2;
859 int JOBTITLE = 0x4;
860 int COPIES = 0x8;
861 int NOSHEET = 0x10;
862 int pFlags = 0;
863 String[] execCmd;
864 int ncomps = 2; // minimum number of print args
865 int n = 0;
866
867 // conveniently "lp" is the default destination for both lp and lpr.
868 if (printer != null && !printer.equals("") && !printer.equals("lp")) {
869 pFlags |= PRINTER;
870 ncomps+=1;
871 }
872 if (options != null && !options.equals("")) {
873 pFlags |= OPTIONS;
874 ncomps+=1;
875 }
876 if (jobTitle != null && !jobTitle.equals("")) {
877 pFlags |= JOBTITLE;
878 ncomps+=1;
879 }
880 if (copies > 1) {
881 pFlags |= COPIES;
882 ncomps+=1;
883 }
1005 pw.append("\t\t").append(br.readLine());
1006 }
1007 } finally {
1008 pw.flush();
1009 }
1010 throw new IOException(sw.toString());
1011 }
1012 }
1013
1014 public Object run() {
1015 if (spoolFile == null || !spoolFile.exists()) {
1016 pex = new PrintException("No spool file");
1017 notifyEvent(PrintJobEvent.JOB_FAILED);
1018 return null;
1019 }
1020 try {
1021 /**
1022 * Spool to the printer.
1023 */
1024 String fileName = spoolFile.getAbsolutePath();
1025 String[] execCmd = printExecCmd(mDestination, mOptions,
1026 mNoJobSheet, jobName, copies, fileName);
1027
1028 Process process = Runtime.getRuntime().exec(execCmd);
1029 process.waitFor();
1030 final int result = process.exitValue();
1031 if (0 != result) {
1032 handleProcessFailure(process, execCmd, result);
1033 }
1034 notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
1035 } catch (IOException ex) {
1036 notifyEvent(PrintJobEvent.JOB_FAILED);
1037 // REMIND : 2d printing throws PrinterException
1038 pex = new PrintException(ex);
1039 } catch (InterruptedException ie) {
1040 notifyEvent(PrintJobEvent.JOB_FAILED);
1041 pex = new PrintException(ie);
1042 } finally {
1043 spoolFile.delete();
1044 notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
1045 }
|