0N/A/*
2362N/A * Copyright (c) 2000, 2007, 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 javax.print;
0N/A
0N/Aimport java.io.OutputStream;
0N/A
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Iterator;
0N/A
0N/Aimport javax.print.DocFlavor;
0N/A
0N/Aimport sun.awt.AppContext;
0N/Aimport java.util.ServiceLoader;
0N/Aimport java.util.ServiceConfigurationError;
0N/A
0N/A/**
0N/A * A <code>StreamPrintServiceFactory</code> is the factory for
0N/A * {@link StreamPrintService} instances,
0N/A * which can print to an output stream in a particular
0N/A * document format described as a mime type.
0N/A * A typical output document format may be Postscript(TM).
0N/A * <p>
0N/A * This class is implemented by a service and located by the
0N/A * implementation using the
0N/A * <a href="../../../technotes/guides/jar/jar.html#Service Provider">
0N/A * SPI JAR File specification</a>.
0N/A * <p>
0N/A * Applications locate instances of this class by calling the
0N/A * {@link #lookupStreamPrintServiceFactories(DocFlavor, String)} method.
0N/A * <p>
0N/A * Applications can use a <code>StreamPrintService</code> obtained from a
0N/A * factory in place of a <code>PrintService</code> which represents a
0N/A * physical printer device.
0N/A */
0N/A
0N/Apublic abstract class StreamPrintServiceFactory {
0N/A
0N/A static class Services {
0N/A private ArrayList listOfFactories = null;
0N/A }
0N/A
0N/A private static Services getServices() {
0N/A Services services =
0N/A (Services)AppContext.getAppContext().get(Services.class);
0N/A if (services == null) {
0N/A services = new Services();
0N/A AppContext.getAppContext().put(Services.class, services);
0N/A }
0N/A return services;
0N/A }
0N/A
0N/A private static ArrayList getListOfFactories() {
0N/A return getServices().listOfFactories;
0N/A }
0N/A
0N/A private static ArrayList initListOfFactories() {
0N/A ArrayList listOfFactories = new ArrayList();
0N/A getServices().listOfFactories = listOfFactories;
0N/A return listOfFactories;
0N/A }
0N/A
0N/A /**
0N/A * Locates factories for print services that can be used with
0N/A * a print job to output a stream of data in the
0N/A * format specified by {@code outputMimeType}.
0N/A * <p>
0N/A * The {@code outputMimeType} parameter describes the document type that
0N/A * you want to create, whereas the {@code flavor} parameter describes the
0N/A * format in which the input data will be provided by the application
0N/A * to the {@code StreamPrintService}.
0N/A * <p>
0N/A * Although null is an acceptable value to use in the lookup of stream
0N/A * printing services, it's typical to search for a particular
0N/A * desired format, such as Postscript(TM).
0N/A * <p>
0N/A * @param flavor of the input document type - null means match all
0N/A * types.
0N/A * @param outputMimeType representing the required output format, used to
0N/A * identify suitable stream printer factories. A value of null means
0N/A * match all formats.
0N/A * @return - matching factories for stream print service instance,
0N/A * empty if no suitable factories could be located.
0N/A */
0N/A public static StreamPrintServiceFactory[]
0N/A lookupStreamPrintServiceFactories(DocFlavor flavor,
0N/A String outputMimeType) {
0N/A
0N/A ArrayList list = getFactories(flavor, outputMimeType);
0N/A return (StreamPrintServiceFactory[])
0N/A (list.toArray(new StreamPrintServiceFactory[list.size()]));
0N/A }
0N/A
0N/A /** Queries the factory for the document format that is emitted
0N/A * by printers obtained from this factory.
0N/A *
0N/A * @return the output format described as a mime type.
0N/A */
0N/A public abstract String getOutputFormat();
0N/A
0N/A /**
0N/A * Queries the factory for the document flavors that can be accepted
0N/A * by printers obtained from this factory.
0N/A * @return array of supported doc flavors.
0N/A */
0N/A public abstract DocFlavor[] getSupportedDocFlavors();
0N/A
0N/A /**
0N/A * Returns a <code>StreamPrintService</code> that can print to
0N/A * the specified output stream.
0N/A * The output stream is created and managed by the application.
0N/A * It is the application's responsibility to close the stream and
0N/A * to ensure that this Printer is not reused.
0N/A * The application should not close this stream until any print job
0N/A * created from the printer is complete. Doing so earlier may generate
0N/A * a <code>PrinterException</code> and an event indicating that the
0N/A * job failed.
0N/A * <p>
0N/A * Whereas a <code>PrintService</code> connected to a physical printer
0N/A * can be reused,
0N/A * a <code>StreamPrintService</code> connected to a stream cannot.
0N/A * The underlying <code>StreamPrintService</code> may be disposed by
0N/A * the print system with
0N/A * the {@link StreamPrintService#dispose() dispose} method
0N/A * before returning from the
0N/A * {@link DocPrintJob#print(Doc, javax.print.attribute.PrintRequestAttributeSet) print}
0N/A * method of <code>DocPrintJob</code> so that the print system knows
0N/A * this printer is no longer usable.
0N/A * This is equivalent to a physical printer going offline - permanently.
0N/A * Applications may supply a null print stream to create a queryable
0N/A * service. It is not valid to create a PrintJob for such a stream.
0N/A * Implementations which allocate resources on construction should examine
0N/A * the stream and may wish to only allocate resources if the stream is
0N/A * non-null.
0N/A * <p>
0N/A * @param out destination stream for generated output.
0N/A * @return a PrintService which will generate the format specified by the
0N/A * DocFlavor supported by this Factory.
0N/A */
0N/A public abstract StreamPrintService getPrintService(OutputStream out);
0N/A
0N/A
0N/A private static ArrayList getAllFactories() {
0N/A synchronized (StreamPrintServiceFactory.class) {
0N/A
0N/A ArrayList listOfFactories = getListOfFactories();
0N/A if (listOfFactories != null) {
0N/A return listOfFactories;
0N/A } else {
0N/A listOfFactories = initListOfFactories();
0N/A }
0N/A
0N/A try {
0N/A java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedExceptionAction() {
0N/A public Object run() {
0N/A Iterator<StreamPrintServiceFactory> iterator =
0N/A ServiceLoader.load
0N/A (StreamPrintServiceFactory.class).iterator();
0N/A ArrayList lof = getListOfFactories();
0N/A while (iterator.hasNext()) {
0N/A try {
0N/A lof.add(iterator.next());
0N/A } catch (ServiceConfigurationError err) {
0N/A /* In the applet case, we continue */
0N/A if (System.getSecurityManager() != null) {
0N/A err.printStackTrace();
0N/A } else {
0N/A throw err;
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A } catch (java.security.PrivilegedActionException e) {
0N/A }
0N/A return listOfFactories;
0N/A }
0N/A }
0N/A
0N/A private static boolean isMember(DocFlavor flavor, DocFlavor[] flavors) {
0N/A for (int f=0; f<flavors.length; f++ ) {
0N/A if (flavor.equals(flavors[f])) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A private static ArrayList getFactories(DocFlavor flavor, String outType) {
0N/A
0N/A if (flavor == null && outType == null) {
0N/A return getAllFactories();
0N/A }
0N/A
0N/A ArrayList list = new ArrayList();
0N/A Iterator iterator = getAllFactories().iterator();
0N/A while (iterator.hasNext()) {
0N/A StreamPrintServiceFactory factory =
0N/A (StreamPrintServiceFactory)iterator.next();
0N/A if ((outType == null ||
0N/A outType.equalsIgnoreCase(factory.getOutputFormat())) &&
0N/A (flavor == null ||
0N/A isMember(flavor, factory.getSupportedDocFlavors()))) {
0N/A list.add(factory);
0N/A }
0N/A }
0N/A
0N/A return list;
0N/A }
0N/A
0N/A}