0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.print;
0N/A
0N/Aimport java.net.URI;
0N/Aimport java.io.BufferedInputStream;
0N/Aimport java.io.BufferedOutputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.Reader;
0N/Aimport java.net.URL;
0N/Aimport java.util.Vector;
0N/A
0N/Aimport javax.print.CancelablePrintJob;
0N/Aimport javax.print.Doc;
0N/Aimport javax.print.DocFlavor;
0N/Aimport javax.print.DocPrintJob;
0N/Aimport javax.print.PrintService;
0N/Aimport javax.print.PrintException;
0N/Aimport javax.print.event.PrintJobEvent;
0N/Aimport javax.print.event.PrintJobListener;
0N/Aimport javax.print.event.PrintJobAttributeListener;
0N/A
0N/Aimport javax.print.attribute.Attribute;
0N/Aimport javax.print.attribute.AttributeSet;
0N/Aimport javax.print.attribute.AttributeSetUtilities;
0N/Aimport javax.print.attribute.DocAttributeSet;
0N/Aimport javax.print.attribute.HashPrintJobAttributeSet;
0N/Aimport javax.print.attribute.HashPrintRequestAttributeSet;
0N/Aimport javax.print.attribute.PrintJobAttribute;
0N/Aimport javax.print.attribute.PrintJobAttributeSet;
0N/Aimport javax.print.attribute.PrintRequestAttribute;
0N/Aimport javax.print.attribute.PrintRequestAttributeSet;
0N/Aimport javax.print.attribute.standard.Copies;
0N/Aimport javax.print.attribute.standard.DocumentName;
0N/Aimport javax.print.attribute.standard.Fidelity;
0N/Aimport javax.print.attribute.standard.JobName;
0N/Aimport javax.print.attribute.standard.JobOriginatingUserName;
0N/Aimport javax.print.attribute.standard.Media;
0N/Aimport javax.print.attribute.standard.MediaSize;
0N/Aimport javax.print.attribute.standard.MediaSizeName;
0N/Aimport javax.print.attribute.standard.OrientationRequested;
0N/Aimport javax.print.attribute.standard.RequestingUserName;
0N/Aimport javax.print.attribute.standard.Destination;
0N/Aimport javax.print.attribute.standard.PrinterIsAcceptingJobs;
0N/Aimport javax.print.attribute.standard.PrinterState;
0N/Aimport javax.print.attribute.standard.PrinterStateReason;
0N/Aimport javax.print.attribute.standard.PrinterStateReasons;
0N/A
0N/Aimport java.awt.print.*;
0N/A
0N/Apublic class Win32PrintJob implements CancelablePrintJob {
0N/A
0N/A transient private Vector jobListeners;
0N/A transient private Vector attrListeners;
0N/A transient private Vector listenedAttributeSets;
0N/A
0N/A private Win32PrintService service;
0N/A private boolean fidelity;
0N/A private boolean printing = false;
0N/A private boolean printReturned = false;
0N/A private PrintRequestAttributeSet reqAttrSet = null;
0N/A private PrintJobAttributeSet jobAttrSet = null;
0N/A private PrinterJob job;
0N/A private Doc doc;
0N/A private String mDestination = null;
0N/A
0N/A /* these variables used globally to store reference to the print
0N/A * data retrieved as a stream. On completion these are always closed
0N/A * if non-null.
0N/A */
0N/A private InputStream instream = null;
0N/A private Reader reader = null;
0N/A
0N/A /* default values overridden by those extracted from the attributes */
0N/A private String jobName = "Java Printing";
0N/A private int copies = 0;
0N/A private MediaSizeName mediaName = null;
0N/A private MediaSize mediaSize = null;
0N/A private OrientationRequested orient = null;
0N/A
0N/A /* print job handle used by native code */
0N/A private long hPrintJob;
0N/A
0N/A /* buffer length for printing raw data */
0N/A private static final int PRINTBUFFERLEN = 8192;
0N/A
0N/A Win32PrintJob(Win32PrintService service) {
0N/A this.service = service;
0N/A }
0N/A
0N/A public PrintService getPrintService() {
0N/A return service;
0N/A }
0N/A
0N/A public PrintJobAttributeSet getAttributes() {
0N/A synchronized (this) {
0N/A if (jobAttrSet == null) {
0N/A /* just return an empty set until the job is submitted */
0N/A PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet();
0N/A return AttributeSetUtilities.unmodifiableView(jobSet);
0N/A } else {
0N/A return jobAttrSet;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void addPrintJobListener(PrintJobListener listener) {
0N/A synchronized (this) {
0N/A if (listener == null) {
0N/A return;
0N/A }
0N/A if (jobListeners == null) {
0N/A jobListeners = new Vector();
0N/A }
0N/A jobListeners.add(listener);
0N/A }
0N/A }
0N/A
0N/A public void removePrintJobListener(PrintJobListener listener) {
0N/A synchronized (this) {
0N/A if (listener == null || jobListeners == null ) {
0N/A return;
0N/A }
0N/A jobListeners.remove(listener);
0N/A if (jobListeners.isEmpty()) {
0N/A jobListeners = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /* Closes any stream already retrieved for the data.
0N/A * We want to avoid unnecessarily asking the Doc to create a stream only
0N/A * to get a reference in order to close it because the job failed.
0N/A * If the representation class is itself a "stream", this
0N/A * closes that stream too.
0N/A */
0N/A private void closeDataStreams() {
0N/A
0N/A if (doc == null) {
0N/A return;
0N/A }
0N/A
0N/A Object data = null;
0N/A
0N/A try {
0N/A data = doc.getPrintData();
0N/A } catch (IOException e) {
0N/A return;
0N/A }
0N/A
0N/A if (instream != null) {
0N/A try {
0N/A instream.close();
0N/A } catch (IOException e) {
0N/A } finally {
0N/A instream = null;
0N/A }
0N/A }
0N/A else if (reader != null) {
0N/A try {
0N/A reader.close();
0N/A } catch (IOException e) {
0N/A } finally {
0N/A reader = null;
0N/A }
0N/A }
0N/A else if (data instanceof InputStream) {
0N/A try {
0N/A ((InputStream)data).close();
0N/A } catch (IOException e) {
0N/A }
0N/A }
0N/A else if (data instanceof Reader) {
0N/A try {
0N/A ((Reader)data).close();
0N/A } catch (IOException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void notifyEvent(int reason) {
0N/A
0N/A /* since this method should always get called, here's where
0N/A * we will perform the clean up of any data stream supplied.
0N/A */
0N/A switch (reason) {
0N/A case PrintJobEvent.DATA_TRANSFER_COMPLETE:
0N/A case PrintJobEvent.JOB_CANCELED :
0N/A case PrintJobEvent.JOB_FAILED :
0N/A case PrintJobEvent.NO_MORE_EVENTS :
0N/A case PrintJobEvent.JOB_COMPLETE :
0N/A closeDataStreams();
0N/A }
0N/A
0N/A synchronized (this) {
0N/A if (jobListeners != null) {
0N/A PrintJobListener listener;
0N/A PrintJobEvent event = new PrintJobEvent(this, reason);
0N/A for (int i = 0; i < jobListeners.size(); i++) {
0N/A listener = (PrintJobListener)(jobListeners.elementAt(i));
0N/A switch (reason) {
0N/A
0N/A case PrintJobEvent.JOB_COMPLETE :
0N/A listener.printJobCompleted(event);
0N/A break;
0N/A
0N/A case PrintJobEvent.JOB_CANCELED :
0N/A listener.printJobCanceled(event);
0N/A break;
0N/A
0N/A case PrintJobEvent.JOB_FAILED :
0N/A listener.printJobFailed(event);
0N/A break;
0N/A
0N/A case PrintJobEvent.DATA_TRANSFER_COMPLETE :
0N/A listener.printDataTransferCompleted(event);
0N/A break;
0N/A
0N/A case PrintJobEvent.NO_MORE_EVENTS :
0N/A listener.printJobNoMoreEvents(event);
0N/A break;
0N/A
0N/A default:
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void addPrintJobAttributeListener(
0N/A PrintJobAttributeListener listener,
0N/A PrintJobAttributeSet attributes) {
0N/A synchronized (this) {
0N/A if (listener == null) {
0N/A return;
0N/A }
0N/A if (attrListeners == null) {
0N/A attrListeners = new Vector();
0N/A listenedAttributeSets = new Vector();
0N/A }
0N/A attrListeners.add(listener);
0N/A if (attributes == null) {
0N/A attributes = new HashPrintJobAttributeSet();
0N/A }
0N/A listenedAttributeSets.add(attributes);
0N/A }
0N/A }
0N/A
0N/A public void removePrintJobAttributeListener(
0N/A PrintJobAttributeListener listener) {
0N/A synchronized (this) {
0N/A if (listener == null || attrListeners == null ) {
0N/A return;
0N/A }
0N/A int index = attrListeners.indexOf(listener);
0N/A if (index == -1) {
0N/A return;
0N/A } else {
0N/A attrListeners.remove(index);
0N/A listenedAttributeSets.remove(index);
0N/A if (attrListeners.isEmpty()) {
0N/A attrListeners = null;
0N/A listenedAttributeSets = null;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void print(Doc doc, PrintRequestAttributeSet attributes)
0N/A throws PrintException {
0N/A
0N/A synchronized (this) {
0N/A if (printing) {
0N/A throw new PrintException("already printing");
0N/A } else {
0N/A printing = true;
0N/A }
0N/A }
0N/A
0N/A PrinterState prnState = (PrinterState)service.getAttribute(
0N/A PrinterState.class);
0N/A if (prnState == PrinterState.STOPPED) {
0N/A PrinterStateReasons prnStateReasons =
0N/A (PrinterStateReasons)service.getAttribute(
0N/A PrinterStateReasons.class);
0N/A if ((prnStateReasons != null) &&
0N/A (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
0N/A {
0N/A throw new PrintException("PrintService is no longer available.");
0N/A }
0N/A }
0N/A
0N/A if ((PrinterIsAcceptingJobs)(service.getAttribute(
0N/A PrinterIsAcceptingJobs.class)) ==
0N/A PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
0N/A throw new PrintException("Printer is not accepting job.");
0N/A }
0N/A
0N/A
0N/A this.doc = doc;
0N/A /* check if the parameters are valid before doing much processing */
0N/A DocFlavor flavor = doc.getDocFlavor();
0N/A Object data;
0N/A
0N/A try {
0N/A data = doc.getPrintData();
0N/A } catch (IOException e) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("can't get print data: " + e.toString());
0N/A }
0N/A
0N/A if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintJobFlavorException("invalid flavor", flavor);
0N/A }
0N/A
0N/A initializeAttributeSets(doc, attributes);
0N/A
0N/A getAttributeValues(flavor);
0N/A
0N/A String repClassName = flavor.getRepresentationClassName();
0N/A
0N/A if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
0N/A flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
0N/A flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
0N/A flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
0N/A flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
0N/A flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
0N/A try {
0N/A instream = doc.getStreamForBytes();
0N/A if (instream == null) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("No stream for data");
0N/A }
0N/A printableJob(new ImagePrinter(instream));
0N/A service.wakeNotifier();
0N/A return;
0N/A } catch (ClassCastException cce) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(cce);
0N/A } catch (IOException ioe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(ioe);
0N/A }
0N/A } else if (flavor.equals(DocFlavor.URL.GIF) ||
0N/A flavor.equals(DocFlavor.URL.JPEG) ||
0N/A flavor.equals(DocFlavor.URL.PNG)) {
0N/A try {
0N/A printableJob(new ImagePrinter((URL)data));
0N/A service.wakeNotifier();
0N/A return;
0N/A } catch (ClassCastException cce) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(cce);
0N/A }
0N/A } else if (repClassName.equals("java.awt.print.Pageable")) {
0N/A try {
0N/A pageableJob((Pageable)doc.getPrintData());
0N/A service.wakeNotifier();
0N/A return;
0N/A } catch (ClassCastException cce) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(cce);
0N/A } catch (IOException ioe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(ioe);
0N/A }
0N/A } else if (repClassName.equals("java.awt.print.Printable")) {
0N/A try {
0N/A printableJob((Printable)doc.getPrintData());
0N/A service.wakeNotifier();
0N/A return;
0N/A } catch (ClassCastException cce) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(cce);
0N/A } catch (IOException ioe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(ioe);
0N/A }
0N/A } else if (repClassName.equals("[B") ||
0N/A repClassName.equals("java.io.InputStream") ||
0N/A repClassName.equals("java.net.URL")) {
0N/A
0N/A if (repClassName.equals("java.net.URL")) {
0N/A URL url = (URL)data;
0N/A try {
0N/A instream = url.openStream();
0N/A } catch (IOException e) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(e.toString());
0N/A }
0N/A } else {
0N/A try {
0N/A instream = doc.getStreamForBytes();
0N/A } catch (IOException ioe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(ioe.toString());
0N/A }
0N/A }
0N/A
0N/A if (instream == null) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("No stream for data");
0N/A }
0N/A
0N/A if (mDestination != null) { // if destination attribute is set
0N/A try {
0N/A FileOutputStream fos = new FileOutputStream(mDestination);
0N/A byte []buffer = new byte[1024];
0N/A int cread;
0N/A
0N/A while ((cread = instream.read(buffer, 0, buffer.length)) >=0) {
0N/A fos.write(buffer, 0, cread);
0N/A }
0N/A fos.flush();
0N/A fos.close();
0N/A } catch (FileNotFoundException fnfe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(fnfe.toString());
0N/A } catch (IOException ioe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(ioe.toString());
0N/A }
0N/A notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
0N/A notifyEvent(PrintJobEvent.JOB_COMPLETE);
0N/A service.wakeNotifier();
0N/A return;
0N/A }
0N/A
0N/A if (!startPrintRawData(service.getName(), jobName)) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("Print job failed to start.");
0N/A }
0N/A BufferedInputStream bin = new BufferedInputStream(instream);
0N/A int bread = 0;
0N/A try {
0N/A byte[] buffer = new byte[PRINTBUFFERLEN];
0N/A
0N/A while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >=0) {
0N/A if (!printRawData(buffer, bread)) {
0N/A bin.close();
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException ("Problem while spooling data");
0N/A }
0N/A }
0N/A bin.close();
0N/A if (!endPrintRawData()) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("Print job failed to close properly.");
0N/A }
0N/A notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
0N/A } catch (IOException e) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException (e.toString());
0N/A } finally {
0N/A notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
0N/A }
0N/A } else {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("unrecognized class: "+repClassName);
0N/A }
0N/A service.wakeNotifier();
0N/A }
0N/A
0N/A public void printableJob(Printable printable) throws PrintException {
0N/A try {
0N/A synchronized(this) {
0N/A if (job != null) { // shouldn't happen
0N/A throw new PrintException("already printing");
0N/A } else {
0N/A job = new sun.awt.windows.WPrinterJob();
0N/A }
0N/A }
0N/A PrintService svc = getPrintService();
0N/A job.setPrintService(svc);
0N/A if (copies == 0) {
0N/A Copies c = (Copies)svc.getDefaultAttributeValue(Copies.class);
0N/A copies = c.getValue();
0N/A }
0N/A
0N/A if (mediaName == null) {
0N/A Object media = svc.getDefaultAttributeValue(Media.class);
0N/A if (media instanceof MediaSizeName) {
0N/A mediaName = (MediaSizeName) media;
0N/A mediaSize = MediaSize.getMediaSizeForName(mediaName);
0N/A }
0N/A }
0N/A
0N/A if (orient == null) {
0N/A orient =
0N/A (OrientationRequested)svc.getDefaultAttributeValue(OrientationRequested.class);
0N/A }
0N/A
0N/A job.setCopies(copies);
0N/A job.setJobName(jobName);
0N/A PageFormat pf = new PageFormat();
0N/A if (mediaSize != null) {
0N/A Paper p = new Paper();
0N/A p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
0N/A mediaSize.getY(MediaSize.INCH)*72.0);
0N/A p.setImageableArea(72.0, 72.0, p.getWidth()-144.0,
0N/A p.getHeight()-144.0);
0N/A pf.setPaper(p);
0N/A }
0N/A if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
0N/A pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
0N/A } else if (orient == OrientationRequested.LANDSCAPE) {
0N/A pf.setOrientation(PageFormat.LANDSCAPE);
0N/A }
0N/A job.setPrintable(printable, pf);
0N/A job.print(reqAttrSet);
0N/A notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
0N/A return;
0N/A } catch (PrinterException pe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(pe);
0N/A } finally {
0N/A printReturned = true;
0N/A notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
0N/A }
0N/A }
0N/A
0N/A public void pageableJob(Pageable pageable) throws PrintException {
0N/A try {
0N/A synchronized(this) {
0N/A if (job != null) { // shouldn't happen
0N/A throw new PrintException("already printing");
0N/A } else {
0N/A job = new sun.awt.windows.WPrinterJob();
0N/A }
0N/A }
0N/A PrintService svc = getPrintService();
0N/A job.setPrintService(svc);
0N/A if (copies == 0) {
0N/A Copies c = (Copies)svc.getDefaultAttributeValue(Copies.class);
0N/A copies = c.getValue();
0N/A }
0N/A job.setCopies(copies);
0N/A job.setJobName(jobName);
0N/A job.setPageable(pageable);
0N/A job.print(reqAttrSet);
0N/A notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
0N/A return;
0N/A } catch (PrinterException pe) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(pe);
0N/A } finally {
0N/A printReturned = true;
0N/A notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
0N/A }
0N/A }
0N/A
0N/A /* There's some inefficiency here as the job set is created even though
0N/A * it may never be requested.
0N/A */
0N/A private synchronized void
0N/A initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
0N/A
0N/A reqAttrSet = new HashPrintRequestAttributeSet();
0N/A jobAttrSet = new HashPrintJobAttributeSet();
0N/A
0N/A Attribute[] attrs;
0N/A if (reqSet != null) {
0N/A reqAttrSet.addAll(reqSet);
0N/A attrs = reqSet.toArray();
0N/A for (int i=0; i<attrs.length; i++) {
0N/A if (attrs[i] instanceof PrintJobAttribute) {
0N/A jobAttrSet.add(attrs[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A DocAttributeSet docSet = doc.getAttributes();
0N/A if (docSet != null) {
0N/A attrs = docSet.toArray();
0N/A for (int i=0; i<attrs.length; i++) {
0N/A if (attrs[i] instanceof PrintRequestAttribute) {
0N/A reqAttrSet.add(attrs[i]);
0N/A }
0N/A if (attrs[i] instanceof PrintJobAttribute) {
0N/A jobAttrSet.add(attrs[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* add the user name to the job */
0N/A String userName = "";
0N/A try {
0N/A userName = System.getProperty("user.name");
0N/A } catch (SecurityException se) {
0N/A }
0N/A
0N/A if (userName == null || userName.equals("")) {
0N/A RequestingUserName ruName =
0N/A (RequestingUserName)reqSet.get(RequestingUserName.class);
0N/A if (ruName != null) {
0N/A jobAttrSet.add(
0N/A new JobOriginatingUserName(ruName.getValue(),
0N/A ruName.getLocale()));
0N/A } else {
0N/A jobAttrSet.add(new JobOriginatingUserName("", null));
0N/A }
0N/A } else {
0N/A jobAttrSet.add(new JobOriginatingUserName(userName, null));
0N/A }
0N/A
0N/A /* if no job name supplied use doc name (if supplied), if none and
0N/A * its a URL use that, else finally anything .. */
0N/A if (jobAttrSet.get(JobName.class) == null) {
0N/A JobName jobName;
0N/A if (docSet != null && docSet.get(DocumentName.class) != null) {
0N/A DocumentName docName =
0N/A (DocumentName)docSet.get(DocumentName.class);
0N/A jobName = new JobName(docName.getValue(), docName.getLocale());
0N/A jobAttrSet.add(jobName);
0N/A } else {
0N/A String str = "JPS Job:" + doc;
0N/A try {
0N/A Object printData = doc.getPrintData();
0N/A if (printData instanceof URL) {
0N/A str = ((URL)(doc.getPrintData())).toString();
0N/A }
0N/A } catch (IOException e) {
0N/A }
0N/A jobName = new JobName(str, null);
0N/A jobAttrSet.add(jobName);
0N/A }
0N/A }
0N/A
0N/A jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
0N/A }
0N/A
0N/A private void getAttributeValues(DocFlavor flavor) throws PrintException {
0N/A
0N/A if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
0N/A fidelity = true;
0N/A } else {
0N/A fidelity = false;
0N/A }
0N/A
0N/A Class category;
0N/A Attribute [] attrs = reqAttrSet.toArray();
0N/A for (int i=0; i<attrs.length; i++) {
0N/A Attribute attr = attrs[i];
0N/A category = attr.getCategory();
0N/A if (fidelity == true) {
0N/A if (!service.isAttributeCategorySupported(category)) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintJobAttributeException(
0N/A "unsupported category: " + category, category, null);
0N/A } else if
0N/A (!service.isAttributeValueSupported(attr, flavor, null)) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintJobAttributeException(
0N/A "unsupported attribute: " + attr, null, attr);
0N/A }
0N/A }
0N/A if (category == Destination.class) {
0N/A URI uri = ((Destination)attr).getURI();
0N/A if (!"file".equals(uri.getScheme())) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException("Not a file: URI");
0N/A } else {
0N/A try {
0N/A mDestination = (new File(uri)).getPath();
0N/A } catch (Exception e) {
0N/A throw new PrintException(e);
0N/A }
0N/A // check write access
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A try {
0N/A security.checkWrite(mDestination);
0N/A } catch (SecurityException se) {
0N/A notifyEvent(PrintJobEvent.JOB_FAILED);
0N/A throw new PrintException(se);
0N/A }
0N/A }
0N/A }
0N/A } else if (category == JobName.class) {
0N/A jobName = ((JobName)attr).getValue();
0N/A } else if (category == Copies.class) {
0N/A copies = ((Copies)attr).getValue();
0N/A } else if (category == Media.class) {
0N/A if (attr instanceof MediaSizeName) {
0N/A mediaName = (MediaSizeName)attr;
0N/A // If requested MediaSizeName is not supported,
0N/A // get the corresponding media size - this will
0N/A // be used to create a new PageFormat.
0N/A if (!service.isAttributeValueSupported(attr, null, null)) {
0N/A mediaSize = MediaSize.getMediaSizeForName(mediaName);
0N/A }
0N/A }
0N/A } else if (category == OrientationRequested.class) {
0N/A orient = (OrientationRequested)attr;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private native boolean startPrintRawData(String printerName,
0N/A String jobName);
0N/A private native boolean printRawData(byte[] data, int count);
0N/A private native boolean endPrintRawData();
0N/A
0N/A /* Cancel PrinterJob jobs that haven't yet completed. */
0N/A public void cancel() throws PrintException {
0N/A synchronized (this) {
0N/A if (!printing) {
0N/A throw new PrintException("Job is not yet submitted.");
0N/A } else if (job != null && !printReturned) {
0N/A job.cancel();
0N/A notifyEvent(PrintJobEvent.JOB_CANCELED);
0N/A return;
0N/A } else {
0N/A throw new PrintException("Job could not be cancelled.");
0N/A }
0N/A }
0N/A }
0N/A}