Exemple #1
0
  /* Closes any stream already retrieved for the data.
   * We want to avoid unnecessarily asking the Doc to create a stream only
   * to get a reference in order to close it because the job failed.
   * If the representation class is itself a "stream", this
   * closes that stream too.
   */
  private void closeDataStreams() {

    if (doc == null) {
      return;
    }

    Object data = null;

    try {
      data = doc.getPrintData();
    } catch (IOException e) {
      return;
    }

    if (instream != null) {
      try {
        instream.close();
      } catch (IOException e) {
      } finally {
        instream = null;
      }
    } else if (reader != null) {
      try {
        reader.close();
      } catch (IOException e) {
      } finally {
        reader = null;
      }
    } else if (data instanceof InputStream) {
      try {
        ((InputStream) data).close();
      } catch (IOException e) {
      }
    } else if (data instanceof Reader) {
      try {
        ((Reader) data).close();
      } catch (IOException e) {
      }
    }
  }
Exemple #2
0
  /* There's some inefficiency here as the job set is created even though
   * it may never be requested.
   */
  private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {

    reqAttrSet = new HashPrintRequestAttributeSet();
    jobAttrSet = new HashPrintJobAttributeSet();

    Attribute[] attrs;
    if (reqSet != null) {
      reqAttrSet.addAll(reqSet);
      attrs = reqSet.toArray();
      for (int i = 0; i < attrs.length; i++) {
        if (attrs[i] instanceof PrintJobAttribute) {
          jobAttrSet.add(attrs[i]);
        }
      }
    }

    DocAttributeSet docSet = doc.getAttributes();
    if (docSet != null) {
      attrs = docSet.toArray();
      for (int i = 0; i < attrs.length; i++) {
        if (attrs[i] instanceof PrintRequestAttribute) {
          reqAttrSet.add(attrs[i]);
        }
        if (attrs[i] instanceof PrintJobAttribute) {
          jobAttrSet.add(attrs[i]);
        }
      }
    }

    /* add the user name to the job */
    String userName = "";
    try {
      userName = System.getProperty("user.name");
    } catch (SecurityException se) {
    }

    if (userName == null || userName.equals("")) {
      RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class);
      if (ruName != null) {
        jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
      } else {
        jobAttrSet.add(new JobOriginatingUserName("", null));
      }
    } else {
      jobAttrSet.add(new JobOriginatingUserName(userName, null));
    }

    /* if no job name supplied use doc name (if supplied), if none and
     * its a URL use that, else finally anything .. */
    if (jobAttrSet.get(JobName.class) == null) {
      JobName jobName;
      if (docSet != null && docSet.get(DocumentName.class) != null) {
        DocumentName docName = (DocumentName) docSet.get(DocumentName.class);
        jobName = new JobName(docName.getValue(), docName.getLocale());
        jobAttrSet.add(jobName);
      } else {
        String str = "JPS Job:" + doc;
        try {
          Object printData = doc.getPrintData();
          if (printData instanceof URL) {
            str = ((URL) (doc.getPrintData())).toString();
          }
        } catch (IOException e) {
        }
        jobName = new JobName(str, null);
        jobAttrSet.add(jobName);
      }
    }

    jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
  }
Exemple #3
0
  public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {

    synchronized (this) {
      if (printing) {
        throw new PrintException("already printing");
      } else {
        printing = true;
      }
    }

    PrinterState prnState = (PrinterState) service.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
      PrinterStateReasons prnStateReasons =
          (PrinterStateReasons) service.getAttribute(PrinterStateReasons.class);
      if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
        throw new PrintException("PrintService is no longer available.");
      }
    }

    if ((PrinterIsAcceptingJobs) (service.getAttribute(PrinterIsAcceptingJobs.class))
        == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
      throw new PrintException("Printer is not accepting job.");
    }

    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;

    try {
      data = doc.getPrintData();
    } catch (IOException e) {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintException("can't get print data: " + e.toString());
    }

    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintJobFlavorException("invalid flavor", flavor);
    }

    initializeAttributeSets(doc, attributes);

    getAttributeValues(flavor);

    String repClassName = flavor.getRepresentationClassName();

    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF)
        || flavor.equals(DocFlavor.INPUT_STREAM.JPEG)
        || flavor.equals(DocFlavor.INPUT_STREAM.PNG)
        || flavor.equals(DocFlavor.BYTE_ARRAY.GIF)
        || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG)
        || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
      try {
        instream = doc.getStreamForBytes();
        if (instream == null) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException("No stream for data");
        }
        printableJob(new ImagePrinter(instream));
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      } catch (IOException ioe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(ioe);
      }
    } else if (flavor.equals(DocFlavor.URL.GIF)
        || flavor.equals(DocFlavor.URL.JPEG)
        || flavor.equals(DocFlavor.URL.PNG)) {
      try {
        printableJob(new ImagePrinter((URL) data));
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
      try {
        pageableJob((Pageable) doc.getPrintData());
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      } catch (IOException ioe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(ioe);
      }
    } else if (repClassName.equals("java.awt.print.Printable")) {
      try {
        printableJob((Printable) doc.getPrintData());
        service.wakeNotifier();
        return;
      } catch (ClassCastException cce) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(cce);
      } catch (IOException ioe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(ioe);
      }
    } else if (repClassName.equals("[B")
        || repClassName.equals("java.io.InputStream")
        || repClassName.equals("java.net.URL")) {

      if (repClassName.equals("java.net.URL")) {
        URL url = (URL) data;
        try {
          instream = url.openStream();
        } catch (IOException e) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(e.toString());
        }
      } else {
        try {
          instream = doc.getStreamForBytes();
        } catch (IOException ioe) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(ioe.toString());
        }
      }

      if (instream == null) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("No stream for data");
      }

      if (mDestination != null) { // if destination attribute is set
        try {
          FileOutputStream fos = new FileOutputStream(mDestination);
          byte[] buffer = new byte[1024];
          int cread;

          while ((cread = instream.read(buffer, 0, buffer.length)) >= 0) {
            fos.write(buffer, 0, cread);
          }
          fos.flush();
          fos.close();
        } catch (FileNotFoundException fnfe) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(fnfe.toString());
        } catch (IOException ioe) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException(ioe.toString());
        }
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        notifyEvent(PrintJobEvent.JOB_COMPLETE);
        service.wakeNotifier();
        return;
      }

      if (!startPrintRawData(service.getName(), jobName)) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("Print job failed to start.");
      }
      BufferedInputStream bin = new BufferedInputStream(instream);
      int bread = 0;
      try {
        byte[] buffer = new byte[PRINTBUFFERLEN];

        while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >= 0) {
          if (!printRawData(buffer, bread)) {
            bin.close();
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("Problem while spooling data");
          }
        }
        bin.close();
        if (!endPrintRawData()) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException("Print job failed to close properly.");
        }
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
      } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(e.toString());
      } finally {
        notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
      }
    } else {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintException("unrecognized class: " + repClassName);
    }
    service.wakeNotifier();
  }