KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > print > StreamPrintServiceFactory


1 /*
2  * @(#)StreamPrintServiceFactory.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.print;
9
10 import java.io.OutputStream JavaDoc;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import javax.print.DocFlavor JavaDoc;
16
17 import sun.awt.AppContext;
18 import sun.misc.Service;
19
20 /**
21  * A <code>StreamPrintServiceFactory</code> is the factory for
22  * {@link StreamPrintService} instances,
23  * which can print to an output stream in a particular
24  * document format described as a mime type.
25  * A typical output document format may be Postscript(TM).
26  * <p>
27  * This class is implemented by a service and located by the
28  * implementation using the
29  * <a HREF="http:/java.sun.com/products/jdk/1.3/docs/guide/jar/jar.html#Service Provider">
30  * SPI JAR File specification</a>.
31  * <p>
32  * Applications locate instances of this class by calling the
33  * {@link #lookupStreamPrintServiceFactories(DocFlavor, String)} method.
34  * <p>
35  * Applications can use a <code>StreamPrintService</code> obtained from a
36  * factory in place of a <code>PrintService</code> which represents a
37  * physical printer device.
38  */

39
40 public abstract class StreamPrintServiceFactory {
41
42     static class Services {
43     private ArrayList JavaDoc listOfFactories = null;
44     }
45
46     private static Services getServices() {
47     Services services =
48         (Services)AppContext.getAppContext().get(Services.class);
49     if (services == null) {
50         services = new Services();
51         AppContext.getAppContext().put(Services.class, services);
52     }
53     return services;
54     }
55
56     private static ArrayList JavaDoc getListOfFactories() {
57     return getServices().listOfFactories;
58     }
59
60     private static ArrayList JavaDoc initListOfFactories() {
61     ArrayList JavaDoc listOfFactories = new ArrayList JavaDoc();
62     getServices().listOfFactories = listOfFactories;
63     return listOfFactories;
64     }
65
66     /**
67      * Locates factories for print services that can be used with
68      * a print job to output a stream of data in the
69      * format specified by <code>flavor</code>.
70      * For example, the doc flavor is the document type that you want to
71      * create, not the flavor of the
72      * document before printing.
73      * <p>
74      * Although null is an acceptable value to use in the lookup of stream
75      * printing services, it's typical to search for a particular
76      * desired format, such as Postscript(TM).
77      * <p>
78      * @param flavor of the input document type - null means match all
79      * types.
80      * @param outputMimeType representing the required output format, used to
81      * identify suitable stream printer factories. A value of null means
82      * match all formats.
83      * @return - matching factories for stream print service instance,
84      * empty if no suitable factories could be located.
85      */

86      public static StreamPrintServiceFactory JavaDoc[]
87          lookupStreamPrintServiceFactories(DocFlavor JavaDoc flavor,
88                        String JavaDoc outputMimeType) {
89          
90          ArrayList JavaDoc list = getFactories(flavor, outputMimeType);
91      return (StreamPrintServiceFactory JavaDoc[])
92                (list.toArray(new StreamPrintServiceFactory JavaDoc[list.size()]));
93      }
94
95     /** Queries the factory for the document format that is emitted
96      * by printers obtained from this factory.
97      *
98      * @return the output format described as a mime type.
99      */

100     public abstract String JavaDoc getOutputFormat();
101
102     /**
103      * Queries the factory for the document flavors that can be accepted
104      * by printers obtained from this factory.
105      * @return array of supported doc flavors.
106      */

107     public abstract DocFlavor JavaDoc[] getSupportedDocFlavors();
108    
109     /**
110      * Returns a <code>StreamPrintService</code> that can print to
111      * the specified output stream.
112      * The output stream is created and managed by the application.
113      * It is the application's responsibility to close the stream and
114      * to ensure that this Printer is not reused.
115      * The application should not close this stream until any print job
116      * created from the printer is complete. Doing so earlier may generate
117      * a <code>PrinterException</code> and an event indicating that the
118      * job failed.
119      * <p>
120      * Whereas a <code>PrintService</code> connected to a physical printer
121      * can be reused,
122      * a <code>StreamPrintService</code> connected to a stream cannot.
123      * The underlying <code>StreamPrintService</code> may be disposed by
124      * the print system with
125      * the {@link StreamPrintService#dispose() dispose} method
126      * before returning from the
127      * {@link DocPrintJob#print(Doc, javax.print.attribute.PrintRequestAttributeSet) print}
128      * method of <code>DocPrintJob</code> so that the print system knows
129      * this printer is no longer usable.
130      * This is equivalent to a physical printer going offline - permanently.
131      * Applications may supply a null print stream to create a queryable
132      * service. It is not valid to create a PrintJob for such a stream.
133      * Implementations which allocate resources on construction should examine
134      * the stream and may wish to only allocate resources if the stream is
135      * non-null.
136      * <p>
137      * @param out destination stream for generated output.
138      * @return a PrintService which will generate the format specified by the
139      * DocFlavor supported by this Factory.
140      */

141     public abstract StreamPrintService JavaDoc getPrintService(OutputStream JavaDoc out);
142     
143
144     private static ArrayList JavaDoc getAllFactories() {
145     synchronized (StreamPrintServiceFactory JavaDoc.class) {
146
147       ArrayList JavaDoc listOfFactories = getListOfFactories();
148         if (listOfFactories != null) {
149         return listOfFactories;
150         } else {
151         listOfFactories = initListOfFactories();
152         }
153
154         try {
155         java.security.AccessController.doPrivileged(
156              new java.security.PrivilegedExceptionAction JavaDoc() {
157                         public Object JavaDoc run() {
158                 Iterator JavaDoc iterator =
159                 Service.providers(
160                     StreamPrintServiceFactory JavaDoc.class);
161                 ArrayList JavaDoc lof = getListOfFactories();
162                 while (iterator.hasNext()) {
163                 try {
164                     StreamPrintServiceFactory JavaDoc factory =
165                     (StreamPrintServiceFactory JavaDoc)
166                                iterator.next();
167                     lof.add(factory);
168                 } catch (Exception JavaDoc e) {
169                 }
170                 }
171                 return null;
172             }
173         });
174         } catch (java.security.PrivilegedActionException JavaDoc e) {
175         }
176         return listOfFactories;
177     }
178     }
179    
180     private static boolean isMember(DocFlavor JavaDoc flavor, DocFlavor JavaDoc[] flavors) {
181     for (int f=0; f<flavors.length; f++ ) {
182         if (flavor.equals(flavors[f])) {
183         return true;
184         }
185     }
186     return false;
187     }
188
189     private static ArrayList JavaDoc getFactories(DocFlavor JavaDoc flavor, String JavaDoc outType) {
190     
191         if (flavor == null && outType == null) {
192             return getAllFactories();
193         }
194
195         ArrayList JavaDoc list = new ArrayList JavaDoc();
196         Iterator JavaDoc iterator = getAllFactories().iterator();
197         while (iterator.hasNext()) {
198             StreamPrintServiceFactory JavaDoc factory =
199                 (StreamPrintServiceFactory JavaDoc)iterator.next();
200         if ((outType == null ||
201          outType.equalsIgnoreCase(factory.getOutputFormat())) &&
202         (flavor == null ||
203          isMember(flavor, factory.getSupportedDocFlavors()))) {
204                 list.add(factory);
205             }
206         }
207
208         return list;
209     }
210
211 }
212
Popular Tags