summaryrefslogtreecommitdiff
path: root/javax/print/PrintServiceLookup.java
blob: 1ea1f95473448045a4f9ce34e9b3994e2fc844cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* PrintServiceLookup.java --
   Copyright (C) 2004, 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.print;

import gnu.classpath.ServiceFactory;
import gnu.javax.print.CupsPrintServiceLookup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

import javax.print.attribute.AttributeSet;


/**
 * <code>PrintServiceLookup</code> implementations provide a way to lookup
 * print services based on different constraints.
 * <p>
 * Implementations are located and loaded automatically through the SPI JAR
 * file specification. Therefore implementation classes must provide a default
 * constructor for instantiation. Furthermore, applications are able to
 * register further instances directly at runtime.
 * </p><p>
 * If an SecurityManager is installed implementors should call
 * <code>checkPrintJobAccess()</code> to disable access for untrusted code.
 * This check is to be made in every lookup service implementation for
 * flexibility. Print services registered by applications through
 * <code>registerService(PrintService)</code> are suppressed in the
 * lookup results if a security manager is installed and disallows access.
 * </p>
 *
 * @author Michael Koch (konqueror@gmx.de)
 * @author Wolfgang Baer (WBaer@gmx.de)
 */
public abstract class PrintServiceLookup
{

  private static final CupsPrintServiceLookup systemProvider;
  private static final HashSet printServices;
  private static final HashSet printServiceLookups;

  static
  {
    systemProvider = new CupsPrintServiceLookup();

    printServices = new HashSet();
    printServiceLookups = new HashSet();

    // check for service providers
    Iterator it = ServiceFactory.lookupProviders(PrintServiceLookup.class);

    while (it.hasNext())
      printServiceLookups.add(it.next());
  }

  /**
   * Constructs a <code>PrintServiceLookup</code> object.
   */
  public PrintServiceLookup()
  {
    // nothing to do here
  }

  /**
   * Explicitly registers the provided print service lookup implementation.
   * <p>
   * The registration will silently fail (returning <code>false</code>) if
   * the lookup service is already registered or the registration somehow
   * else fails.
   * </p>
   *
   * @param sp the print service lookup implementation to register.
   * @return <code>true</code> if registered, <code>false</code> otherwise.
   */
  public static boolean registerServiceProvider(PrintServiceLookup sp)
  {
    return printServiceLookups.add(sp);
  }

  /**
   * Explicitly registers the provided print service instance.
   * <p>
   * The registration will silently fail (returning <code>false</code>) if
   * the print service instance is already registered or the registration
   * somehow else fails.
   * </p>
   * @param service the single print service to register.
   * @return <code>true</code> if registered, <code>false</code> otherwise.
   */
  public static boolean registerService(PrintService service)
  {
    if (service instanceof StreamPrintService)
      return false;

    // security
    try
      {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null)
          sm.checkPrintJobAccess();

        return printServices.add(service);
      }
    catch (SecurityException se)
      {
        return false;
      }
  }

  /**
   * Searches print services capable of printing in the given document flavor
   * which supports the specified printing attributes.
   *
   * @param flavor the document flavor to support. If <code>null</code> this
   * constraint is ignored during lookup.
   * @param attributes the printing attributes to support. If
   * <code>null</code> this constraint is ignored during lookup.
   * @return The resulting available print services, or an array of length 0
   * if none is found.
   */
  public static final PrintService[] lookupPrintServices(DocFlavor flavor,
    AttributeSet attributes)
  {
    ArrayList result = new ArrayList();

    PrintService[] services =
      systemProvider.getPrintServices(flavor, attributes);
    result.addAll(Arrays.asList(services));

    for (Iterator it = printServiceLookups.iterator(); it.hasNext(); )
      {
        PrintServiceLookup lookup = (PrintServiceLookup) it.next();
        services = lookup.getPrintServices(flavor, attributes);
        result.addAll(Arrays.asList(services));
      }

    for (Iterator it = printServices.iterator(); it.hasNext(); )
      {
        PrintService service = (PrintService) it.next();
        if (systemProvider.checkPrintService(flavor, attributes, service))
          result.add(service);
      }

    return (PrintService[]) result.toArray(new PrintService[result.size()]);
  }

  /**
   * Searches print services capable of multi document printing in all of the
   * given document flavors and supporting the specified printing attributes.
   *
   * @param flavors the document flavors to support. If <code>null</code> this
   * constraint is ignored during lookup.
   * @param attributes the printing attributes to support. If
   * <code>null</code> this constraint is ignored during lookup.
   * @return The resulting available multi document print services, or an
   * array of length 0 if none is found.
   */
  public static final MultiDocPrintService[] lookupMultiDocPrintServices(
    DocFlavor[] flavors, AttributeSet attributes)
  {
    ArrayList result = new ArrayList();

    MultiDocPrintService[] services =
      systemProvider.getMultiDocPrintServices(flavors, attributes);
    result.addAll(Arrays.asList(services));

    for (Iterator it = printServiceLookups.iterator(); it.hasNext(); )
      {
        PrintServiceLookup lookup = (PrintServiceLookup) it.next();
        services = lookup.getMultiDocPrintServices(flavors, attributes);
        result.addAll(Arrays.asList(services));
      }

    for (Iterator it = printServices.iterator(); it.hasNext(); )
      {
        PrintService service = (PrintService) it.next();
        if (systemProvider.checkMultiDocPrintService(flavors, attributes, service))
          result.add(service);
      }

    return (MultiDocPrintService[]) result.toArray(
      new MultiDocPrintService[result.size()]);
  }


  /**
   * Searches the default print service in the current environment.
   * <p>
   * If multiple lookup services are registered and each has a default
   * print service the result is not specified. Usually the default
   * print service of the native platform lookup service is returned.
   * </p><p>
   * The GNU classpath implementation will return the CUPS default
   * printing service as the default print service, if available.
   * </p><p>
   * The default print service may be overriden by users through
   * the property <code>javax.print.defaultPrinter</code>. A service
   * specified must be found to be returned as the default.
   * </p>
   *
   * @return The default print service, or <code>null</code> if none found.
   */
  public static final PrintService lookupDefaultPrintService()
  {
    // TODO Find out what the property controls and use it
    // String defaultPrinter = System.getProperty("javax.print.defaultPrinter");

    // first test for platform specified default services
    PrintService service = systemProvider.getDefaultPrintService();

    if (service != null)
      return service;

    // none available by systemDefaultProvider
    // search in other registered ones and take first
    for (Iterator it = printServiceLookups.iterator(); it.hasNext(); )
      {
        service = ((PrintServiceLookup) it.next()).getDefaultPrintService();
        if (service != null)
          return service;
      }

    return null;
  }

  /**
   * Not to be called directly by applications.
   *
   * @return The default lookup service of the implementing lookup service or
   * <code>null</code> if there is no default one.
   */
  public abstract PrintService getDefaultPrintService();

  /**
   * Not to be called directly by applications.
   *
   * @param flavors the document flavors which have to be supported.
   * @param attributes the attributes which have to be supported.
   *
   * @return The multidoc print services of the implementing lookup service
   * for the given parameters, or an array of length 0 if none is available.
   */
  public abstract MultiDocPrintService[]
    getMultiDocPrintServices(DocFlavor[] flavors, AttributeSet attributes);

  /**
   * Not to be called directly by applications.
   *
   * @return All known print services of the implementing lookup service
   * regardless of supported features, or an array of length 0 if none is
   * available.
   */
  public abstract PrintService[] getPrintServices();

  /**
   * Not to be called directly by applications.
   *
   * @param flavor the document flavor which has to be supported.
   * @param attributes the attributes which have to be supported.
   *
   * @return The print services of the implementing lookup service
   * for the given parameters, or an array of length 0 if none is available.
   */
  public abstract PrintService[]
    getPrintServices(DocFlavor flavor, AttributeSet attributes);
}