jdk/src/solaris/classes/sun/print/UnixPrintJob.java
author prr
Wed, 05 Feb 2014 09:06:54 -0800
changeset 23283 fbd991796648
parent 22584 eed64ee05369
child 25125 1195f75805bf
permissions -rw-r--r--
8032227: ClassCastException in UnixPrintJob for ipp printer Reviewed-by: bae, jgodinez
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2386
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2386
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2386
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2386
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2386
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.BufferedWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
    41
import java.io.PrintWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.Reader;
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
    43
import java.io.StringWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.UnsupportedEncodingException;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 10865
diff changeset
    45
import java.nio.file.Files;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.print.CancelablePrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.print.Doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.print.DocPrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.print.PrintException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.print.event.PrintJobEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.print.event.PrintJobListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.print.event.PrintJobAttributeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.print.attribute.AttributeSetUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.print.attribute.DocAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.print.attribute.HashPrintJobAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.print.attribute.PrintJobAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.print.attribute.PrintJobAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.print.attribute.PrintRequestAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.print.attribute.PrintRequestAttributeSet;
19013
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 14751
diff changeset
    68
import javax.print.attribute.PrintServiceAttributeSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.print.attribute.standard.Copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.print.attribute.standard.Destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.print.attribute.standard.DocumentName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.print.attribute.standard.Fidelity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.print.attribute.standard.JobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.print.attribute.standard.JobOriginatingUserName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.print.attribute.standard.JobSheets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import javax.print.attribute.standard.OrientationRequested;
19013
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 14751
diff changeset
    80
import javax.print.attribute.standard.PrinterName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import javax.print.attribute.standard.RequestingUserName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import javax.print.attribute.standard.NumberUp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import javax.print.attribute.standard.Sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import java.awt.print.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
public class UnixPrintJob implements CancelablePrintJob {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static String debugPrefix = "UnixPrintJob>> ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    transient private Vector jobListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    transient private Vector attrListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    transient private Vector listenedAttributeSets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private PrintService service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private boolean fidelity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean printing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private boolean printReturned = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private PrintRequestAttributeSet reqAttrSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private PrintJobAttributeSet jobAttrSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private PrinterJob job;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private Doc doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* these variables used globally to store reference to the print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * data retrieved as a stream. On completion these are always closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * if non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private InputStream instream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private Reader reader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /* default values overridden by those extracted from the attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private String jobName = "Java Printing";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int copies = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private MediaSizeName mediaName = MediaSizeName.NA_LETTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private MediaSize     mediaSize = MediaSize.NA.LETTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private CustomMediaTray     customTray = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private OrientationRequested orient = OrientationRequested.PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private NumberUp nUp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Sides sides = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    UnixPrintJob(PrintService service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.service = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        mDestination = service.getName();
19013
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 14751
diff changeset
   125
        if (UnixPrintServiceLookup.isMac()) {
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 14751
diff changeset
   126
            mDestination = ((IPPPrintService)service).getDest();
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 14751
diff changeset
   127
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        mDestType = UnixPrintJob.DESTPRINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public PrintService getPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public PrintJobAttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (jobAttrSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                /* just return an empty set until the job is submitted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return AttributeSetUtilities.unmodifiableView(jobSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
              return jobAttrSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void addPrintJobListener(PrintJobListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (jobListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                jobListeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            jobListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void removePrintJobListener(PrintJobListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (listener == null || jobListeners == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            jobListeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (jobListeners.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                jobListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /* Closes any stream already retrieved for the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * We want to avoid unnecessarily asking the Doc to create a stream only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * to get a reference in order to close it because the job failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * If the representation class is itself a "stream", this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * closes that stream too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private void closeDataStreams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (doc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Object data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            data = doc.getPrintData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (instream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                instream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                instream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        else if (reader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                reader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        else if (data instanceof InputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                ((InputStream)data).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        else if (data instanceof Reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                ((Reader)data).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private void notifyEvent(int reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        /* since this method should always get called, here's where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * we will perform the clean up of any data stream supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            case PrintJobEvent.DATA_TRANSFER_COMPLETE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            case PrintJobEvent.JOB_CANCELED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            case PrintJobEvent.JOB_FAILED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            case PrintJobEvent.NO_MORE_EVENTS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            case PrintJobEvent.JOB_COMPLETE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                closeDataStreams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (jobListeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                PrintJobListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                PrintJobEvent event = new PrintJobEvent(this, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                for (int i = 0; i < jobListeners.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    listener = (PrintJobListener)(jobListeners.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        case PrintJobEvent.JOB_CANCELED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            listener.printJobCanceled(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        case PrintJobEvent.JOB_FAILED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            listener.printJobFailed(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        case PrintJobEvent.DATA_TRANSFER_COMPLETE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            listener.printDataTransferCompleted(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        case PrintJobEvent.NO_MORE_EVENTS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            listener.printJobNoMoreEvents(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public void addPrintJobAttributeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                  PrintJobAttributeListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                  PrintJobAttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (attrListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                attrListeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                listenedAttributeSets = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            attrListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                attributes = new HashPrintJobAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            listenedAttributeSets.add(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public void removePrintJobAttributeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                        PrintJobAttributeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (listener == null || attrListeners == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            int index = attrListeners.indexOf(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                attrListeners.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                listenedAttributeSets.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                if (attrListeners.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    attrListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    listenedAttributeSets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void print(Doc doc, PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (printing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                throw new PrintException("already printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                printing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
   318
        if ((service.getAttribute(PrinterIsAcceptingJobs.class)) ==
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                         PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new PrintException("Printer is not accepting job.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        this.doc = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        /* check if the parameters are valid before doing much processing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        DocFlavor flavor = doc.getDocFlavor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Object data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            data = doc.getPrintData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new PrintException("can't get print data: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
19167
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 19013
diff changeset
   336
        if (data == null) {
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 19013
diff changeset
   337
            throw new PrintException("Null print data.");
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 19013
diff changeset
   338
        }
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 19013
diff changeset
   339
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new PrintJobFlavorException("invalid flavor", flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        initializeAttributeSets(doc, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        getAttributeValues(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // set up mOptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if ((service instanceof IPPPrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
             IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        "instanceof IPPPrintService");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
             if (mediaName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                 CustomMediaSizeName customMedia =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                     ((IPPPrintService)service).findCustomMedia(mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                 if (customMedia != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                     mOptions = " media="+ customMedia.getChoiceName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
             if (customTray != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                 customTray instanceof CustomMediaTray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                 String choice = customTray.getChoiceName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                 if (choice != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                     mOptions += " media="+choice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
             if (nUp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                 mOptions += " number-up="+nUp.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
2386
7e59f81fd05b 6750383: 2D_PrintingTiger\PrintDocOrientationTest fails, wrong orientated images are printed
jgodinez
parents: 715
diff changeset
   376
             if (orient != OrientationRequested.PORTRAIT &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                 (flavor != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                 !flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
2386
7e59f81fd05b 6750383: 2D_PrintingTiger\PrintDocOrientationTest fails, wrong orientated images are printed
jgodinez
parents: 715
diff changeset
   379
                 mOptions += " orientation-requested="+orient.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
             if (sides != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                 mOptions += " sides="+sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        IPPPrintService.debug_println(debugPrefix+"mOptions "+mOptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        String repClassName = flavor.getRepresentationClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        String val = flavor.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String encoding = "us-ascii";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (val != null && !val.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            encoding = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                instream = doc.getStreamForBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (instream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    throw new PrintException("No stream for data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (!(service instanceof IPPPrintService &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    ((IPPPrintService)service).isIPPSupportedImages(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                                flavor.getMimeType()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    printableJob(new ImagePrinter(instream));
23283
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   412
                    if (service instanceof IPPPrintService) {
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   413
                        ((IPPPrintService)service).wakeNotifier();
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   414
                    } else {
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   415
                        ((UnixPrintService)service).wakeNotifier();
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   416
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                throw new PrintException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else if (flavor.equals(DocFlavor.URL.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                   flavor.equals(DocFlavor.URL.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                   flavor.equals(DocFlavor.URL.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                URL url = (URL)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if ((service instanceof IPPPrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    ((IPPPrintService)service).isIPPSupportedImages(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                               flavor.getMimeType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    instream = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    printableJob(new ImagePrinter(url));
23283
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   437
                    if (service instanceof IPPPrintService) {
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   438
                        ((IPPPrintService)service).wakeNotifier();
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   439
                    } else {
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   440
                        ((UnixPrintService)service).wakeNotifier();
fbd991796648 8032227: ClassCastException in UnixPrintJob for ipp printer
prr
parents: 22584
diff changeset
   441
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                throw new PrintException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } else if (flavor.equals(DocFlavor.CHAR_ARRAY.TEXT_PLAIN) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                   flavor.equals(DocFlavor.READER.TEXT_PLAIN) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                   flavor.equals(DocFlavor.STRING.TEXT_PLAIN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                reader = doc.getReaderForText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (reader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                   notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                   throw new PrintException("No reader for data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                throw new PrintException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } else if (repClassName.equals("[B") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                   repClassName.equals("java.io.InputStream")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                instream = doc.getStreamForBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (instream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    throw new PrintException("No stream for data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                throw new PrintException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        } else if  (repClassName.equals("java.net.URL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
             * This extracts the data from the URL and passes it the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
             * directly to the print service as a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
             * This is appropriate for the current implementation where lp or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
             * lpr is always used to spool the data. We expect to revise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
             * implementation to provide more complete IPP support (ie not just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
             * CUPS) and at that time the job will be spooled via IPP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             * and the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
             * itself should be sent to the IPP print service not the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            URL url = (URL)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                instream = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                throw new PrintException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        } else if (repClassName.equals("java.awt.print.Pageable")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                pageableJob((Pageable)doc.getPrintData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (service instanceof IPPPrintService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    ((IPPPrintService)service).wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    ((UnixPrintService)service).wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                throw new PrintException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else if (repClassName.equals("java.awt.print.Printable")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                printableJob((Printable)doc.getPrintData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                if (service instanceof IPPPrintService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    ((IPPPrintService)service).wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    ((UnixPrintService)service).wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                throw new PrintException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            throw new PrintException("unrecognized class: "+repClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // now spool the print data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        PrinterOpener po = new PrinterOpener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        java.security.AccessController.doPrivileged(po);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (po.pex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            throw po.pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        OutputStream output = po.result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        /* There are three cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * 1) Text data from a Reader, just pass through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * 2) Text data from an input stream which we must read using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         *    correct encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * 3) Raw byte data from an InputStream we don't interpret as text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         *    just pass through: eg postscript.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        BufferedWriter bw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if ((instream == null && reader != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            BufferedReader br = new BufferedReader(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            OutputStreamWriter osw = new OutputStreamWriter(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            bw = new BufferedWriter(osw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            char []buffer = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            int cread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                while ((cread = br.read(buffer, 0, buffer.length)) >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    bw.write(buffer, 0, cread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                br.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                bw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                bw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                throw new PrintException (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } else if (instream != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                   flavor.getMediaType().equalsIgnoreCase("text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                InputStreamReader isr = new InputStreamReader(instream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                                              encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                BufferedReader br = new BufferedReader(isr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                OutputStreamWriter osw = new OutputStreamWriter(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                bw = new BufferedWriter(osw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                char []buffer = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                int cread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                while ((cread = br.read(buffer, 0, buffer.length)) >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    bw.write(buffer, 0, cread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                bw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                throw new PrintException (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    if (bw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        bw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        } else if (instream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            BufferedInputStream bin = new BufferedInputStream(instream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            BufferedOutputStream bout = new BufferedOutputStream(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            byte[] buffer = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int bread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                while ((bread = bin.read(buffer)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    bout.write(buffer, 0, bread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                bin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                bout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                bout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                throw new PrintException (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (mDestType == UnixPrintJob.DESTPRINTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            PrinterSpooler spooler = new PrinterSpooler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            java.security.AccessController.doPrivileged(spooler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (spooler.pex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                throw spooler.pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (service instanceof IPPPrintService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            ((IPPPrintService)service).wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            ((UnixPrintService)service).wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public void printableJob(Printable printable) throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                if (job != null) { // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    throw new PrintException("already printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    job = new PSPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            job.setPrintService(getPrintService());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            job.setCopies(copies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            job.setJobName(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            PageFormat pf = new PageFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (mediaSize != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                Paper p = new Paper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                          mediaSize.getY(MediaSize.INCH)*72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                p.setImageableArea(72.0, 72.0, p.getWidth()-144.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                   p.getHeight()-144.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                pf.setPaper(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            } else if (orient == OrientationRequested.LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                pf.setOrientation(PageFormat.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            job.setPrintable(printable, pf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            job.print(reqAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        } catch (PrinterException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            throw new PrintException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            printReturned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public void pageableJob(Pageable pageable) throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                if (job != null) { // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    throw new PrintException("already printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    job = new PSPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            job.setPrintService(getPrintService());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            job.setCopies(copies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            job.setJobName(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            job.setPageable(pageable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            job.print(reqAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        } catch (PrinterException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            throw new PrintException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            printReturned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /* There's some inefficiency here as the job set is created even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * it may never be requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    private synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        reqAttrSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        jobAttrSet = new HashPrintJobAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        Attribute[] attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (reqSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            reqAttrSet.addAll(reqSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            attrs = reqSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if (attrs[i] instanceof PrintJobAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    jobAttrSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        DocAttributeSet docSet = doc.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (docSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            attrs = docSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    reqAttrSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                if (attrs[i] instanceof PrintJobAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    jobAttrSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        /* add the user name to the job */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        String userName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
          userName = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (userName == null || userName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            RequestingUserName ruName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                (RequestingUserName)reqSet.get(RequestingUserName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (ruName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                jobAttrSet.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    new JobOriginatingUserName(ruName.getValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                               ruName.getLocale()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                jobAttrSet.add(new JobOriginatingUserName("", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            jobAttrSet.add(new JobOriginatingUserName(userName, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        /* if no job name supplied use doc name (if supplied), if none and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         * its a URL use that, else finally anything .. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (jobAttrSet.get(JobName.class) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            JobName jobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            if (docSet != null && docSet.get(DocumentName.class) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                DocumentName docName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    (DocumentName)docSet.get(DocumentName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                jobName = new JobName(docName.getValue(), docName.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                jobAttrSet.add(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                String str = "JPS Job:" + doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    Object printData = doc.getPrintData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    if (printData instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                        str = ((URL)(doc.getPrintData())).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                jobName = new JobName(str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                jobAttrSet.add(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    private void getAttributeValues(DocFlavor flavor) throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        Class category;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            fidelity = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            fidelity = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        Attribute []attrs = reqAttrSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            attr = attrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            category = attr.getCategory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if (fidelity == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                if (!service.isAttributeCategorySupported(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    throw new PrintJobAttributeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        "unsupported category: " + category, category, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                } else if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    throw new PrintJobAttributeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        "unsupported attribute: " + attr, null, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            if (category == Destination.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                URI uri = ((Destination)attr).getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                if (!"file".equals(uri.getScheme())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    throw new PrintException("Not a file: URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        mDestType = DESTFILE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        mDestination = (new File(uri)).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                        throw new PrintException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    // check write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        security.checkWrite(mDestination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                      } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                        notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        throw new PrintException(se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            } else if (category == JobSheets.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                if ((JobSheets)attr == JobSheets.NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                   mNoJobSheet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            } else if (category == JobName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                jobName = ((JobName)attr).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            } else if (category == Copies.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                copies = ((Copies)attr).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            } else if (category == Media.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                if (attr instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    mediaName = (MediaSizeName)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                                                  "mediaName "+mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                if (!service.isAttributeValueSupported(attr, null, null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    mediaSize = MediaSize.getMediaSizeForName(mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
              } else if (attr instanceof CustomMediaTray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                  customTray = (CustomMediaTray)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            } else if (category == OrientationRequested.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                orient = (OrientationRequested)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            } else if (category == NumberUp.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                nUp = (NumberUp)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            } else if (category == Sides.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                sides = (Sides)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    private String[] printExecCmd(String printer, String options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                 boolean noJobSheet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                 String banner, int copies, String spoolFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        int PRINTER = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        int OPTIONS = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        int BANNER  = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        int COPIES  = 0x8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        int NOSHEET  = 0x10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        int pFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        String execCmd[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        int ncomps = 2; // minimum number of print args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        // conveniently "lp" is the default destination for both lp and lpr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        if (printer != null && !printer.equals("") && !printer.equals("lp")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            pFlags |= PRINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (options != null && !options.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            pFlags |= OPTIONS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        if (banner != null && !banner.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            pFlags |= BANNER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (copies > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            pFlags |= COPIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if (noJobSheet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            pFlags |= NOSHEET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (UnixPrintServiceLookup.osname.equals("SunOS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            ncomps+=1; // lp uses 1 more arg than lpr (make a copy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            execCmd = new String[ncomps];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            execCmd[n++] = "/usr/bin/lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            execCmd[n++] = "-c";           // make a copy of the spool file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if ((pFlags & PRINTER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   889
                execCmd[n++] = "-d" + printer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if ((pFlags & BANNER) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                String quoteChar = "\"";
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   893
                execCmd[n++] = "-t "  + quoteChar+banner+quoteChar;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if ((pFlags & COPIES) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   896
                execCmd[n++] = "-n " + copies;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if ((pFlags & NOSHEET) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   899
                execCmd[n++] = "-o nobanner";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            if ((pFlags & OPTIONS) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   902
                execCmd[n++] = "-o " + options;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            execCmd = new String[ncomps];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            execCmd[n++] = "/usr/bin/lpr";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            if ((pFlags & PRINTER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   908
                execCmd[n++] = "-P" + printer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            if ((pFlags & BANNER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   911
                execCmd[n++] = "-J "  + banner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            if ((pFlags & COPIES) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   914
                execCmd[n++] = "-#" + copies;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if ((pFlags & NOSHEET) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   917
                execCmd[n++] = "-h";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            if ((pFlags & OPTIONS) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   920
                execCmd[n++] = "-o" + options;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        execCmd[n++] = spoolFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        if (IPPPrintService.debugPrint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            System.out.println("UnixPrintJob>> execCmd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            for (int i=0; i<execCmd.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                System.out.print(" "+execCmd[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        return execCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    private static int DESTPRINTER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    private static int DESTFILE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    private int mDestType = DESTPRINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    private File spoolFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    private String mDestination, mOptions="";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    private boolean mNoJobSheet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    // Inner class to run "privileged" to open the printer output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    private class PrinterOpener implements java.security.PrivilegedAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        PrintException pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        OutputStream result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                if (mDestType == UnixPrintJob.DESTFILE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    spoolFile = new File(mDestination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    /* Write to a temporary file which will be spooled to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                     * the printer then deleted. In the case that the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                     * is not removed for some reason, request that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                     * removed when the VM exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                     */
14751
82c075c346e8 8004316: Printer - tempfile having incorrect extension
jviswana
parents: 13041
diff changeset
   958
                    spoolFile = Files.createTempFile("javaprint", "").toFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    spoolFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                result = new FileOutputStream(spoolFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                // If there is an IOError we subvert it to a PrinterException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                pex = new PrintException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    // Inner class to run "privileged" to invoke the system print command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    private class PrinterSpooler implements java.security.PrivilegedAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        PrintException pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   977
        private void handleProcessFailure(final Process failedProcess,
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   978
                final String[] execCmd, final int result) throws IOException {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   979
            try (StringWriter sw = new StringWriter();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   980
                    PrintWriter pw = new PrintWriter(sw)) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   981
                pw.append("error=").append(Integer.toString(result));
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   982
                pw.append(" running:");
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   983
                for (String arg: execCmd) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   984
                    pw.append(" '").append(arg).append("'");
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   985
                }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   986
                try (InputStream is = failedProcess.getErrorStream();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   987
                        InputStreamReader isr = new InputStreamReader(is);
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   988
                        BufferedReader br = new BufferedReader(isr)) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   989
                    while (br.ready()) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   990
                        pw.println();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   991
                        pw.append("\t\t").append(br.readLine());
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   992
                    }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   993
                } finally {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   994
                    pw.flush();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   995
                    throw new IOException(sw.toString());
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   996
                }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   997
            }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   998
        }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   999
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        public Object run() {
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1001
            if (spoolFile == null || !spoolFile.exists()) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1002
               pex = new PrintException("No spool file");
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1003
               notifyEvent(PrintJobEvent.JOB_FAILED);
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1004
               return null;
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1005
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                 * Spool to the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                String fileName = spoolFile.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                String execCmd[] = printExecCmd(mDestination, mOptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                               mNoJobSheet, jobName, copies, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                Process process = Runtime.getRuntime().exec(execCmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                process.waitFor();
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1016
                final int result = process.exitValue();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1017
                if (0 != result) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1018
                    handleProcessFailure(process, execCmd, result);
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1019
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                // REMIND : 2d printing throws PrinterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                pex = new PrintException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                pex = new PrintException(ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            } finally {
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
  1029
                spoolFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    public void cancel() throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if (!printing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                throw new PrintException("Job is not yet submitted.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            } else if (job != null && !printReturned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                job.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                notifyEvent(PrintJobEvent.JOB_CANCELED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                throw new PrintException("Job could not be cancelled.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
}