Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/print/StreamPrintServiceFactory.java
38829 views
/*1* Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.print;2627import java.io.OutputStream;2829import java.util.ArrayList;30import java.util.Iterator;3132import javax.print.DocFlavor;3334import sun.awt.AppContext;35import java.util.ServiceLoader;36import java.util.ServiceConfigurationError;3738/**39* A <code>StreamPrintServiceFactory</code> is the factory for40* {@link StreamPrintService} instances,41* which can print to an output stream in a particular42* document format described as a mime type.43* A typical output document format may be Postscript(TM).44* <p>45* This class is implemented by a service and located by the46* implementation using the47* <a href="../../../technotes/guides/jar/jar.html#Service Provider">48* SPI JAR File specification</a>.49* <p>50* Applications locate instances of this class by calling the51* {@link #lookupStreamPrintServiceFactories(DocFlavor, String)} method.52* <p>53* Applications can use a <code>StreamPrintService</code> obtained from a54* factory in place of a <code>PrintService</code> which represents a55* physical printer device.56*/5758public abstract class StreamPrintServiceFactory {5960static class Services {61private ArrayList listOfFactories = null;62}6364private static Services getServices() {65Services services =66(Services)AppContext.getAppContext().get(Services.class);67if (services == null) {68services = new Services();69AppContext.getAppContext().put(Services.class, services);70}71return services;72}7374private static ArrayList getListOfFactories() {75return getServices().listOfFactories;76}7778private static ArrayList initListOfFactories() {79ArrayList listOfFactories = new ArrayList();80getServices().listOfFactories = listOfFactories;81return listOfFactories;82}8384/**85* Locates factories for print services that can be used with86* a print job to output a stream of data in the87* format specified by {@code outputMimeType}.88* <p>89* The {@code outputMimeType} parameter describes the document type that90* you want to create, whereas the {@code flavor} parameter describes the91* format in which the input data will be provided by the application92* to the {@code StreamPrintService}.93* <p>94* Although null is an acceptable value to use in the lookup of stream95* printing services, it's typical to search for a particular96* desired format, such as Postscript(TM).97* <p>98* @param flavor of the input document type - null means match all99* types.100* @param outputMimeType representing the required output format, used to101* identify suitable stream printer factories. A value of null means102* match all formats.103* @return - matching factories for stream print service instance,104* empty if no suitable factories could be located.105*/106public static StreamPrintServiceFactory[]107lookupStreamPrintServiceFactories(DocFlavor flavor,108String outputMimeType) {109110ArrayList list = getFactories(flavor, outputMimeType);111return (StreamPrintServiceFactory[])112(list.toArray(new StreamPrintServiceFactory[list.size()]));113}114115/** Queries the factory for the document format that is emitted116* by printers obtained from this factory.117*118* @return the output format described as a mime type.119*/120public abstract String getOutputFormat();121122/**123* Queries the factory for the document flavors that can be accepted124* by printers obtained from this factory.125* @return array of supported doc flavors.126*/127public abstract DocFlavor[] getSupportedDocFlavors();128129/**130* Returns a <code>StreamPrintService</code> that can print to131* the specified output stream.132* The output stream is created and managed by the application.133* It is the application's responsibility to close the stream and134* to ensure that this Printer is not reused.135* The application should not close this stream until any print job136* created from the printer is complete. Doing so earlier may generate137* a <code>PrinterException</code> and an event indicating that the138* job failed.139* <p>140* Whereas a <code>PrintService</code> connected to a physical printer141* can be reused,142* a <code>StreamPrintService</code> connected to a stream cannot.143* The underlying <code>StreamPrintService</code> may be disposed by144* the print system with145* the {@link StreamPrintService#dispose() dispose} method146* before returning from the147* {@link DocPrintJob#print(Doc, javax.print.attribute.PrintRequestAttributeSet) print}148* method of <code>DocPrintJob</code> so that the print system knows149* this printer is no longer usable.150* This is equivalent to a physical printer going offline - permanently.151* Applications may supply a null print stream to create a queryable152* service. It is not valid to create a PrintJob for such a stream.153* Implementations which allocate resources on construction should examine154* the stream and may wish to only allocate resources if the stream is155* non-null.156* <p>157* @param out destination stream for generated output.158* @return a PrintService which will generate the format specified by the159* DocFlavor supported by this Factory.160*/161public abstract StreamPrintService getPrintService(OutputStream out);162163164private static ArrayList getAllFactories() {165synchronized (StreamPrintServiceFactory.class) {166167ArrayList listOfFactories = getListOfFactories();168if (listOfFactories != null) {169return listOfFactories;170} else {171listOfFactories = initListOfFactories();172}173174try {175java.security.AccessController.doPrivileged(176new java.security.PrivilegedExceptionAction() {177public Object run() {178Iterator<StreamPrintServiceFactory> iterator =179ServiceLoader.load180(StreamPrintServiceFactory.class).iterator();181ArrayList lof = getListOfFactories();182while (iterator.hasNext()) {183try {184lof.add(iterator.next());185} catch (ServiceConfigurationError err) {186/* In the applet case, we continue */187if (System.getSecurityManager() != null) {188err.printStackTrace();189} else {190throw err;191}192}193}194return null;195}196});197} catch (java.security.PrivilegedActionException e) {198}199return listOfFactories;200}201}202203private static boolean isMember(DocFlavor flavor, DocFlavor[] flavors) {204for (int f=0; f<flavors.length; f++ ) {205if (flavor.equals(flavors[f])) {206return true;207}208}209return false;210}211212private static ArrayList getFactories(DocFlavor flavor, String outType) {213214if (flavor == null && outType == null) {215return getAllFactories();216}217218ArrayList list = new ArrayList();219Iterator iterator = getAllFactories().iterator();220while (iterator.hasNext()) {221StreamPrintServiceFactory factory =222(StreamPrintServiceFactory)iterator.next();223if ((outType == null ||224outType.equalsIgnoreCase(factory.getOutputFormat())) &&225(flavor == null ||226isMember(flavor, factory.getSupportedDocFlavors()))) {227list.add(factory);228}229}230231return list;232}233234}235236237