PrintServiceLookup.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* PrintServiceLookup.java --
  2. Copyright (C) 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.print;
  32. import gnu.classpath.ServiceFactory;
  33. import gnu.javax.print.CupsPrintServiceLookup;
  34. import java.util.ArrayList;
  35. import java.util.Arrays;
  36. import java.util.HashSet;
  37. import java.util.Iterator;
  38. import javax.print.attribute.AttributeSet;
  39. /**
  40. * <code>PrintServiceLookup</code> implementations provide a way to lookup
  41. * print services based on different constraints.
  42. * <p>
  43. * Implementations are located and loaded automatically through the SPI JAR
  44. * file specification. Therefore implementation classes must provide a default
  45. * constructor for instantiation. Furthermore, applications are able to
  46. * register further instances directly at runtime.
  47. * </p><p>
  48. * If an SecurityManager is installed implementors should call
  49. * <code>checkPrintJobAccess()</code> to disable access for untrusted code.
  50. * This check is to be made in every lookup service implementation for
  51. * flexibility. Print services registered by applications through
  52. * <code>registerService(PrintService)</code> are suppressed in the
  53. * lookup results if a security manager is installed and disallows access.
  54. * </p>
  55. *
  56. * @author Michael Koch (konqueror@gmx.de)
  57. * @author Wolfgang Baer (WBaer@gmx.de)
  58. */
  59. public abstract class PrintServiceLookup
  60. {
  61. private static final CupsPrintServiceLookup systemProvider;
  62. private static final HashSet printServices;
  63. private static final HashSet printServiceLookups;
  64. static
  65. {
  66. systemProvider = new CupsPrintServiceLookup();
  67. printServices = new HashSet();
  68. printServiceLookups = new HashSet();
  69. // check for service providers
  70. Iterator it = ServiceFactory.lookupProviders(PrintServiceLookup.class);
  71. while (it.hasNext())
  72. printServiceLookups.add(it.next());
  73. }
  74. /**
  75. * Constructs a <code>PrintServiceLookup</code> object.
  76. */
  77. public PrintServiceLookup()
  78. {
  79. // nothing to do here
  80. }
  81. /**
  82. * Explicitly registers the provided print service lookup implementation.
  83. * <p>
  84. * The registration will silently fail (returning <code>false</code>) if
  85. * the lookup service is already registered or the registration somehow
  86. * else fails.
  87. * </p>
  88. *
  89. * @param sp the print service lookup implementation to register.
  90. * @return <code>true</code> if registered, <code>false</code> otherwise.
  91. */
  92. public static boolean registerServiceProvider(PrintServiceLookup sp)
  93. {
  94. return printServiceLookups.add(sp);
  95. }
  96. /**
  97. * Explicitly registers the provided print service instance.
  98. * <p>
  99. * The registration will silently fail (returning <code>false</code>) if
  100. * the print service instance is already registered or the registration
  101. * somehow else fails.
  102. * </p>
  103. * @param service the single print service to register.
  104. * @return <code>true</code> if registered, <code>false</code> otherwise.
  105. */
  106. public static boolean registerService(PrintService service)
  107. {
  108. if (service instanceof StreamPrintService)
  109. return false;
  110. // security
  111. try
  112. {
  113. SecurityManager sm = System.getSecurityManager();
  114. if (sm != null)
  115. sm.checkPrintJobAccess();
  116. return printServices.add(service);
  117. }
  118. catch (SecurityException se)
  119. {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Searches print services capable of printing in the given document flavor
  125. * which supports the specified printing attributes.
  126. *
  127. * @param flavor the document flavor to support. If <code>null</code> this
  128. * constraint is ignored during lookup.
  129. * @param attributes the printing attributes to support. If
  130. * <code>null</code> this constraint is ignored during lookup.
  131. * @return The resulting available print services, or an array of length 0
  132. * if none is found.
  133. */
  134. public static final PrintService[] lookupPrintServices(DocFlavor flavor,
  135. AttributeSet attributes)
  136. {
  137. ArrayList result = new ArrayList();
  138. PrintService[] services =
  139. systemProvider.getPrintServices(flavor, attributes);
  140. result.addAll(Arrays.asList(services));
  141. for (Iterator it = printServiceLookups.iterator(); it.hasNext(); )
  142. {
  143. PrintServiceLookup lookup = (PrintServiceLookup) it.next();
  144. services = lookup.getPrintServices(flavor, attributes);
  145. result.addAll(Arrays.asList(services));
  146. }
  147. for (Iterator it = printServices.iterator(); it.hasNext(); )
  148. {
  149. PrintService service = (PrintService) it.next();
  150. if (systemProvider.checkPrintService(flavor, attributes, service))
  151. result.add(service);
  152. }
  153. return (PrintService[]) result.toArray(new PrintService[result.size()]);
  154. }
  155. /**
  156. * Searches print services capable of multi document printing in all of the
  157. * given document flavors and supporting the specified printing attributes.
  158. *
  159. * @param flavors the document flavors to support. If <code>null</code> this
  160. * constraint is ignored during lookup.
  161. * @param attributes the printing attributes to support. If
  162. * <code>null</code> this constraint is ignored during lookup.
  163. * @return The resulting available multi document print services, or an
  164. * array of length 0 if none is found.
  165. */
  166. public static final MultiDocPrintService[] lookupMultiDocPrintServices(
  167. DocFlavor[] flavors, AttributeSet attributes)
  168. {
  169. ArrayList result = new ArrayList();
  170. MultiDocPrintService[] services =
  171. systemProvider.getMultiDocPrintServices(flavors, attributes);
  172. result.addAll(Arrays.asList(services));
  173. for (Iterator it = printServiceLookups.iterator(); it.hasNext(); )
  174. {
  175. PrintServiceLookup lookup = (PrintServiceLookup) it.next();
  176. services = lookup.getMultiDocPrintServices(flavors, attributes);
  177. result.addAll(Arrays.asList(services));
  178. }
  179. for (Iterator it = printServices.iterator(); it.hasNext(); )
  180. {
  181. PrintService service = (PrintService) it.next();
  182. if (systemProvider.checkMultiDocPrintService(flavors, attributes, service))
  183. result.add(service);
  184. }
  185. return (MultiDocPrintService[]) result.toArray(
  186. new MultiDocPrintService[result.size()]);
  187. }
  188. /**
  189. * Searches the default print service in the current environment.
  190. * <p>
  191. * If multiple lookup services are registered and each has a default
  192. * print service the result is not specified. Usually the default
  193. * print service of the native platform lookup service is returned.
  194. * </p><p>
  195. * The GNU classpath implementation will return the CUPS default
  196. * printing service as the default print service, if available.
  197. * </p><p>
  198. * The default print service may be overriden by users through
  199. * the property <code>javax.print.defaultPrinter</code>. A service
  200. * specified must be found to be returned as the default.
  201. * </p>
  202. *
  203. * @return The default print service, or <code>null</code> if none found.
  204. */
  205. public static final PrintService lookupDefaultPrintService()
  206. {
  207. // TODO Find out what the property controls and use it
  208. // String defaultPrinter = System.getProperty("javax.print.defaultPrinter");
  209. // first test for platform specified default services
  210. PrintService service = systemProvider.getDefaultPrintService();
  211. if (service != null)
  212. return service;
  213. // none available by systemDefaultProvider
  214. // search in other registered ones and take first
  215. for (Iterator it = printServiceLookups.iterator(); it.hasNext(); )
  216. {
  217. service = ((PrintServiceLookup) it.next()).getDefaultPrintService();
  218. if (service != null)
  219. return service;
  220. }
  221. return null;
  222. }
  223. /**
  224. * Not to be called directly by applications.
  225. *
  226. * @return The default lookup service of the implementing lookup service or
  227. * <code>null</code> if there is no default one.
  228. */
  229. public abstract PrintService getDefaultPrintService();
  230. /**
  231. * Not to be called directly by applications.
  232. *
  233. * @param flavors the document flavors which have to be supported.
  234. * @param attributes the attributes which have to be supported.
  235. *
  236. * @return The multidoc print services of the implementing lookup service
  237. * for the given parameters, or an array of length 0 if none is available.
  238. */
  239. public abstract MultiDocPrintService[]
  240. getMultiDocPrintServices(DocFlavor[] flavors, AttributeSet attributes);
  241. /**
  242. * Not to be called directly by applications.
  243. *
  244. * @return All known print services of the implementing lookup service
  245. * regardless of supported features, or an array of length 0 if none is
  246. * available.
  247. */
  248. public abstract PrintService[] getPrintServices();
  249. /**
  250. * Not to be called directly by applications.
  251. *
  252. * @param flavor the document flavor which has to be supported.
  253. * @param attributes the attributes which have to be supported.
  254. *
  255. * @return The print services of the implementing lookup service
  256. * for the given parameters, or an array of length 0 if none is available.
  257. */
  258. public abstract PrintService[]
  259. getPrintServices(DocFlavor flavor, AttributeSet attributes);
  260. }