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