2 * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package de
.lohndirekt
.print
;
22 import java
.net
.URISyntaxException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.HashSet
;
25 import java
.util
.Iterator
;
26 import java
.util
.List
;
27 import java
.util
.Locale
;
29 import java
.util
.logging
.Level
;
30 import java
.util
.logging
.Logger
;
32 import javax
.print
.DocFlavor
;
33 import javax
.print
.MultiDocPrintService
;
34 import javax
.print
.PrintService
;
35 import javax
.print
.PrintServiceLookup
;
36 import javax
.print
.attribute
.Attribute
;
37 import javax
.print
.attribute
.AttributeSet
;
38 import javax
.print
.attribute
.standard
.RequestingUserName
;
40 import de
.lohndirekt
.print
.attribute
.auth
.RequestingUserPassword
;
47 * This is an implementation of the Java Print Service API for IPP.<p>
48 * To use this implementation do one of the following:
50 * 1. Set the system properties
52 * <li><code>de.lohndirekt.print.IppPrintService.uri</code></li>
53 * <li><code>de.lohndirekt.print.IppPrintService.username</code></li>
54 * <li><code>de.lohndirekt.print.IppPrintService.password</code></li>
56 * and use the default constructor
58 * 2. use the 3 parameter constructor
60 * then register the instance by invoking javax.print.PrintServiceLookup.registerServiceProvider<br>
62 * @see javax.print.PrintServiceLookup#registerServiceProvider
64 public class IppPrintServiceLookup
extends PrintServiceLookup
{
65 public final static String URI_KEY
= "de.lohndirekt.print.IppPrintService.uri";
66 public final static String USERNAME_KEY
= "de.lohndirekt.print.IppPrintService.username";
67 public final static String PASSWORD_KEY
= "de.lohndirekt.print.IppPrintService.password";
68 private List cupsServers
= new ArrayList();
69 private Logger log
= Logger
.getLogger(this.getClass().getName());
73 * default constructor uses the values set in
75 * <code>de.lohndirekt.print.IppPrintService.uri</code><br>
76 * <code>de.lohndirekt.print.IppPrintService.username</code><br>
77 * <code>de.lohndirekt.print.IppPrintService.password</code><br>
79 public IppPrintServiceLookup() {
80 String myUri
= (String
) System
.getProperties().get(URI_KEY
);
81 String user
= (String
) System
.getProperties().get(USERNAME_KEY
);
82 String password
= (String
) System
.getProperties().get(PASSWORD_KEY
);
85 throw new NullPointerException("System property " + URI_KEY
+ " not set!");
86 } else if (user
== null) {
87 throw new NullPointerException("System property " + USERNAME_KEY
+ " not set!");
88 } else if (password
== null) {
89 throw new NullPointerException("System property " + PASSWORD_KEY
+ " not set!");
93 } catch (URISyntaxException e
) {
94 throw new NullPointerException("System property " + URI_KEY
+ " - " + myUri
+ " is not a valid Uri!");
96 cupsServers
.add(new IppServer(uri
, new RequestingUserName(user
,Locale
.getDefault()), new RequestingUserPassword(password
,Locale
.getDefault())));
100 * This constructor uses the given parameters to connect to an IPP service
102 * @param uri the uri of the ipp service
103 * @param username used for authentication
104 * @param password used for authentication
106 public IppPrintServiceLookup(URI uri
, String username
, String password
) {
107 cupsServers
.add(new IppServer(uri
, new RequestingUserName(username
,Locale
.getDefault()),new RequestingUserPassword(password
,Locale
.getDefault())));
111 * @see javax.print.PrintServiceLookup#getPrintServices(javax.print.DocFlavor, javax.print.attribute.AttributeSet)
113 public PrintService
[] getPrintServices(DocFlavor flavor
, AttributeSet attributes
) {
114 PrintService
[] services
= getPrintServices();
115 List fittingServices
= new ArrayList();
116 for (int i
= 0; i
< services
.length
; i
++) {
117 IppPrintService service
= (IppPrintService
) services
[i
];
118 if (checkService(service
, flavor
, attributes
)) {
119 fittingServices
.add(service
);
122 PrintService
[] serviceArray
= new PrintService
[fittingServices
.size()];
123 return (PrintService
[]) fittingServices
.toArray(serviceArray
);
127 * @see javax.print.PrintServiceLookup#getPrintServices()
129 public PrintService
[] getPrintServices() {
130 List services
= new ArrayList();
132 for (Iterator iter
= cupsServers
.iterator(); iter
.hasNext();) {
133 IppServer server
= (IppServer
) iter
.next();
134 services
.addAll(server
.getPrintServices());
136 } catch (RuntimeException e
) {
137 log
.log(Level
.SEVERE
, e
.getMessage(), e
);
139 PrintService
[] serviceArray
= new PrintService
[services
.size()];
140 return (PrintService
[]) services
.toArray(serviceArray
);
149 private boolean checkService(IppPrintService service
, DocFlavor flavor
, AttributeSet attributes
) {
150 if (flavor
== null || service
.isDocFlavorSupported(flavor
)) {
151 if (attributes
!= null) {
152 Attribute
[] attrArray
= attributes
.toArray();
153 for (int j
= 0; j
< attrArray
.length
; j
++) {
154 Attribute attribute
= attrArray
[j
];
155 if (service
.isAttributeValueSupported(attribute
, flavor
, null)) {
167 * @see javax.print.PrintServiceLookup#getMultiDocPrintServices(javax.print.DocFlavor[], javax.print.attribute.AttributeSet)
169 public MultiDocPrintService
[] getMultiDocPrintServices(DocFlavor
[] flavors
, AttributeSet attributes
) {
170 Set multiDocServices
= new HashSet();
171 PrintService
[] services
= getPrintServices(null, attributes
);
172 for (int i
= 0; i
< services
.length
; i
++) {
173 PrintService service
= services
[i
];
174 if (flavors
!= null) {
175 for (int j
= 0; j
< flavors
.length
; j
++) {
176 DocFlavor flavor
= flavors
[j
];
177 if (!service
.isDocFlavorSupported(flavor
)) {
182 multiDocServices
.add(new IppMultiDocPrintService(((IppPrintService
) service
).getUri()));
184 return (MultiDocPrintService
[]) multiDocServices
.toArray(new MultiDocPrintService
[multiDocServices
.size()]);
189 * @see javax.print.PrintServiceLookup#getDefaultPrintService()
191 public PrintService
getDefaultPrintService() {
192 PrintService
[] services
= this.getPrintServices();
193 if (services
.length
> 0) {