author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 715 | f16baef3a20e |
child 10779 | 5037da29fa26 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.print; |
|
27 |
||
28 |
import java.io.BufferedReader; |
|
29 |
import java.io.FileInputStream; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.InputStreamReader; |
|
32 |
import java.io.IOException; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Vector; |
|
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedActionException; |
|
37 |
import java.security.PrivilegedExceptionAction; |
|
38 |
import javax.print.DocFlavor; |
|
39 |
import javax.print.MultiDocPrintService; |
|
40 |
import javax.print.PrintService; |
|
41 |
import javax.print.PrintServiceLookup; |
|
42 |
import javax.print.attribute.Attribute; |
|
43 |
import javax.print.attribute.AttributeSet; |
|
44 |
import javax.print.attribute.HashPrintRequestAttributeSet; |
|
45 |
import javax.print.attribute.HashPrintServiceAttributeSet; |
|
46 |
import javax.print.attribute.PrintRequestAttribute; |
|
47 |
import javax.print.attribute.PrintRequestAttributeSet; |
|
48 |
import javax.print.attribute.PrintServiceAttribute; |
|
49 |
import javax.print.attribute.PrintServiceAttributeSet; |
|
50 |
import javax.print.attribute.standard.PrinterName; |
|
51 |
import java.io.File; |
|
52 |
import java.io.FileReader; |
|
53 |
import java.net.URL; |
|
54 |
||
55 |
/* |
|
56 |
* Remind: This class uses solaris commands. We also need a linux |
|
57 |
* version |
|
58 |
*/ |
|
59 |
public class UnixPrintServiceLookup extends PrintServiceLookup |
|
60 |
implements BackgroundServiceLookup, Runnable { |
|
61 |
||
62 |
/* Remind: the current implementation is static, as its assumed |
|
63 |
* its preferable to minimise creation of PrintService instances. |
|
64 |
* Later we should add logic to add/remove services on the fly which |
|
65 |
* will take a hit of needing to regather the list of services. |
|
66 |
*/ |
|
67 |
private String defaultPrinter; |
|
68 |
private PrintService defaultPrintService; |
|
69 |
private PrintService[] printServices; /* includes the default printer */ |
|
70 |
private Vector lookupListeners = null; |
|
71 |
private static String debugPrefix = "UnixPrintServiceLookup>> "; |
|
72 |
private static boolean pollServices = true; |
|
73 |
private static final int DEFAULT_MINREFRESH = 120; // 2 minutes |
|
74 |
private static int minRefreshTime = DEFAULT_MINREFRESH; |
|
75 |
||
76 |
||
77 |
static String osname; |
|
78 |
||
79 |
static { |
|
80 |
/* The system property "sun.java2d.print.polling" |
|
81 |
* can be used to force the printing code to poll or not poll |
|
82 |
* for PrintServices. |
|
83 |
*/ |
|
84 |
String pollStr = java.security.AccessController.doPrivileged( |
|
85 |
new sun.security.action.GetPropertyAction("sun.java2d.print.polling")); |
|
86 |
||
87 |
if (pollStr != null) { |
|
88 |
if (pollStr.equalsIgnoreCase("true")) { |
|
89 |
pollServices = true; |
|
90 |
} else if (pollStr.equalsIgnoreCase("false")) { |
|
91 |
pollServices = false; |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
/* The system property "sun.java2d.print.minRefreshTime" |
|
96 |
* can be used to specify minimum refresh time (in seconds) |
|
97 |
* for polling PrintServices. The default is 120. |
|
98 |
*/ |
|
99 |
String refreshTimeStr = java.security.AccessController.doPrivileged( |
|
100 |
new sun.security.action.GetPropertyAction( |
|
101 |
"sun.java2d.print.minRefreshTime")); |
|
102 |
||
103 |
if (refreshTimeStr != null) { |
|
104 |
try { |
|
105 |
minRefreshTime = (new Integer(refreshTimeStr)).intValue(); |
|
106 |
} catch (NumberFormatException e) { |
|
107 |
} |
|
108 |
if (minRefreshTime < DEFAULT_MINREFRESH) { |
|
109 |
minRefreshTime = DEFAULT_MINREFRESH; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
osname = java.security.AccessController.doPrivileged( |
|
114 |
new sun.security.action.GetPropertyAction("os.name")); |
|
115 |
} |
|
116 |
||
117 |
static boolean isSysV() { |
|
118 |
return osname.equals("SunOS"); |
|
119 |
} |
|
120 |
||
121 |
static boolean isBSD() { |
|
122 |
return osname.equals("Linux"); |
|
123 |
} |
|
124 |
||
125 |
static final int UNINITIALIZED = -1; |
|
126 |
static final int BSD_LPD = 0; |
|
127 |
static final int BSD_LPD_NG = 1; |
|
128 |
||
129 |
static int cmdIndex = UNINITIALIZED; |
|
130 |
||
131 |
String[] lpcFirstCom = { |
|
132 |
"/usr/sbin/lpc status | grep : | sed -ne '1,1 s/://p'", |
|
133 |
"/usr/sbin/lpc status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'" |
|
134 |
}; |
|
135 |
||
136 |
String[] lpcAllCom = { |
|
137 |
"/usr/sbin/lpc status | grep : | sed -e 's/://'", |
|
138 |
"/usr/sbin/lpc -a status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}' | sort" |
|
139 |
}; |
|
140 |
||
141 |
String[] lpcNameCom = { |
|
142 |
"| grep : | sed -ne 's/://p'", |
|
143 |
"| grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'" |
|
144 |
}; |
|
145 |
||
146 |
||
147 |
static int getBSDCommandIndex() { |
|
148 |
String command = "/usr/sbin/lpc status"; |
|
149 |
String[] names = execCmd(command); |
|
150 |
||
151 |
if ((names == null) || (names.length == 0)) { |
|
152 |
return BSD_LPD_NG; |
|
153 |
} |
|
154 |
||
155 |
for (int i=0; i<names.length; i++) { |
|
156 |
if (names[i].indexOf('@') != -1) { |
|
157 |
return BSD_LPD_NG; |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
return BSD_LPD; |
|
162 |
} |
|
163 |
||
164 |
||
165 |
public UnixPrintServiceLookup() { |
|
166 |
// start the printer listener thread |
|
167 |
if (pollServices) { |
|
168 |
PrinterChangeListener thr = new PrinterChangeListener(); |
|
169 |
thr.setDaemon(true); |
|
170 |
thr.start(); |
|
171 |
IPPPrintService.debug_println(debugPrefix+"polling turned on"); |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
/* Want the PrintService which is default print service to have |
|
176 |
* equality of reference with the equivalent in list of print services |
|
177 |
* This isn't required by the API and there's a risk doing this will |
|
178 |
* lead people to assume its guaranteed. |
|
179 |
*/ |
|
180 |
public synchronized PrintService[] getPrintServices() { |
|
181 |
SecurityManager security = System.getSecurityManager(); |
|
182 |
if (security != null) { |
|
183 |
security.checkPrintJobAccess(); |
|
184 |
} |
|
185 |
||
186 |
if (printServices == null || !pollServices) { |
|
187 |
refreshServices(); |
|
188 |
} |
|
189 |
if (printServices == null) { |
|
190 |
return new PrintService[0]; |
|
191 |
} else { |
|
192 |
return printServices; |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
||
197 |
// refreshes "printServices" |
|
198 |
public synchronized void refreshServices() { |
|
542
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
199 |
/* excludes the default printer */ |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
200 |
String[] printers = null; // array of printer names |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
201 |
String[] printerURIs = null; //array of printer URIs |
2 | 202 |
|
203 |
getDefaultPrintService(); |
|
204 |
if (CUPSPrinter.isCupsRunning()) { |
|
542
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
205 |
printerURIs = CUPSPrinter.getAllPrinters(); |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
206 |
if ((printerURIs != null) && (printerURIs.length > 0)) { |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
207 |
printers = new String[printerURIs.length]; |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
208 |
for (int i=0; i<printerURIs.length; i++) { |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
209 |
int lastIndex = printerURIs[i].lastIndexOf("/"); |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
210 |
printers[i] = printerURIs[i].substring(lastIndex+1); |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
211 |
} |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
212 |
} |
2 | 213 |
} else { |
214 |
if (isSysV()) { |
|
215 |
printers = getAllPrinterNamesSysV(); |
|
216 |
} else { //BSD |
|
217 |
printers = getAllPrinterNamesBSD(); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
if (printers == null) { |
|
222 |
if (defaultPrintService != null) { |
|
223 |
printServices = new PrintService[1]; |
|
224 |
printServices[0] = defaultPrintService; |
|
225 |
} else { |
|
226 |
printServices = null; |
|
227 |
} |
|
228 |
return; |
|
229 |
} |
|
230 |
||
231 |
ArrayList printerList = new ArrayList(); |
|
232 |
int defaultIndex = -1; |
|
233 |
for (int p=0; p<printers.length; p++) { |
|
234 |
if (printers[p] == null) { |
|
235 |
continue; |
|
236 |
} |
|
237 |
if ((defaultPrintService != null) |
|
238 |
&& printers[p].equals(defaultPrintService.getName())) { |
|
239 |
printerList.add(defaultPrintService); |
|
240 |
defaultIndex = printerList.size() - 1; |
|
241 |
} else { |
|
242 |
if (printServices == null) { |
|
243 |
IPPPrintService.debug_println(debugPrefix+ |
|
244 |
"total# of printers = "+printers.length); |
|
245 |
||
246 |
if (CUPSPrinter.isCupsRunning()) { |
|
247 |
try { |
|
542
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
248 |
printerList.add(new IPPPrintService(printers[p], |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
249 |
printerURIs[p], |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
250 |
true)); |
2 | 251 |
} catch (Exception e) { |
252 |
IPPPrintService.debug_println(debugPrefix+ |
|
253 |
" getAllPrinters Exception "+ |
|
254 |
e); |
|
255 |
||
256 |
} |
|
257 |
} else { |
|
258 |
printerList.add(new UnixPrintService(printers[p])); |
|
259 |
} |
|
260 |
} else { |
|
261 |
int j; |
|
262 |
for (j=0; j<printServices.length; j++) { |
|
263 |
if ((printServices[j] != null) && |
|
264 |
(printers[p].equals(printServices[j].getName()))) { |
|
265 |
printerList.add(printServices[j]); |
|
266 |
printServices[j] = null; |
|
267 |
break; |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
if (j == printServices.length) { // not found? |
|
272 |
if (CUPSPrinter.isCupsRunning()) { |
|
273 |
try { |
|
542
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
274 |
printerList.add(new IPPPrintService( |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
275 |
printers[p], |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
276 |
printerURIs[p], |
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
277 |
true)); |
2 | 278 |
} catch (Exception e) { |
279 |
IPPPrintService.debug_println(debugPrefix+ |
|
280 |
" getAllPrinters Exception "+ |
|
281 |
e); |
|
282 |
||
283 |
} |
|
284 |
} else { |
|
285 |
printerList.add(new UnixPrintService(printers[p])); |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
// Look for deleted services and invalidate these |
|
293 |
if (printServices != null) { |
|
294 |
for (int j=0; j < printServices.length; j++) { |
|
295 |
if ((printServices[j] instanceof UnixPrintService) && |
|
296 |
(!printServices[j].equals(defaultPrintService))) { |
|
297 |
((UnixPrintService)printServices[j]).invalidateService(); |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
//if defaultService is not found in printerList |
|
303 |
if (defaultIndex == -1 && defaultPrintService != null) { |
|
304 |
//add default to the list |
|
305 |
printerList.add(defaultPrintService); |
|
306 |
defaultIndex = printerList.size() - 1; |
|
307 |
} |
|
308 |
||
309 |
printServices = (PrintService[])printerList.toArray( |
|
310 |
new PrintService[] {}); |
|
311 |
||
312 |
// swap default with the first in the list |
|
313 |
if (defaultIndex > 0) { |
|
314 |
PrintService saveService = printServices[0]; |
|
315 |
printServices[0] = printServices[defaultIndex]; |
|
316 |
printServices[defaultIndex] = saveService; |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
private boolean matchesAttributes(PrintService service, |
|
321 |
PrintServiceAttributeSet attributes) { |
|
322 |
||
323 |
Attribute [] attrs = attributes.toArray(); |
|
324 |
Attribute serviceAttr; |
|
325 |
for (int i=0; i<attrs.length; i++) { |
|
326 |
serviceAttr |
|
327 |
= service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory()); |
|
328 |
if (serviceAttr == null || !serviceAttr.equals(attrs[i])) { |
|
329 |
return false; |
|
330 |
} |
|
331 |
} |
|
332 |
return true; |
|
333 |
} |
|
334 |
||
335 |
/* This checks for validity of the printer name before passing as |
|
336 |
* parameter to a shell command. |
|
337 |
*/ |
|
338 |
private boolean checkPrinterName(String s) { |
|
339 |
char c; |
|
340 |
||
341 |
for (int i=0; i < s.length(); i++) { |
|
342 |
c = s.charAt(i); |
|
343 |
if (Character.isLetterOrDigit(c) || |
|
344 |
c == '-' || c == '_' || c == '.' || c == '/') { |
|
345 |
continue; |
|
346 |
} else { |
|
347 |
return false; |
|
348 |
} |
|
349 |
} |
|
350 |
return true; |
|
351 |
} |
|
352 |
||
353 |
/* On a network with many (hundreds) of network printers, it |
|
354 |
* can save several seconds if you know all you want is a particular |
|
355 |
* printer, to ask for that printer rather than retrieving all printers. |
|
356 |
*/ |
|
357 |
private PrintService getServiceByName(PrinterName nameAttr) { |
|
358 |
String name = nameAttr.getValue(); |
|
359 |
PrintService printer = null; |
|
360 |
if (name == null || name.equals("") || !checkPrinterName(name)) { |
|
361 |
return null; |
|
362 |
} |
|
363 |
if (isSysV()) { |
|
364 |
printer = getNamedPrinterNameSysV(name); |
|
365 |
} else { |
|
366 |
printer = getNamedPrinterNameBSD(name); |
|
367 |
} |
|
368 |
return printer; |
|
369 |
} |
|
370 |
||
371 |
private PrintService[] |
|
372 |
getPrintServices(PrintServiceAttributeSet serviceSet) { |
|
373 |
||
374 |
if (serviceSet == null || serviceSet.isEmpty()) { |
|
375 |
return getPrintServices(); |
|
376 |
} |
|
377 |
||
378 |
/* Typically expect that if a service attribute is specified that |
|
379 |
* its a printer name and there ought to be only one match. |
|
380 |
* Directly retrieve that service and confirm |
|
381 |
* that it meets the other requirements. |
|
382 |
* If printer name isn't mentioned then go a slow path checking |
|
383 |
* all printers if they meet the reqiremements. |
|
384 |
*/ |
|
385 |
PrintService[] services; |
|
386 |
PrinterName name = (PrinterName)serviceSet.get(PrinterName.class); |
|
387 |
PrintService defService; |
|
388 |
if (name != null && (defService = getDefaultPrintService()) != null) { |
|
389 |
/* To avoid execing a unix command see if the client is asking |
|
390 |
* for the default printer by name, since we already have that |
|
391 |
* initialised. |
|
392 |
*/ |
|
393 |
||
394 |
PrinterName defName = |
|
395 |
(PrinterName)defService.getAttribute(PrinterName.class); |
|
396 |
||
397 |
if (defName != null && name.equals(defName)) { |
|
398 |
if (matchesAttributes(defService, serviceSet)) { |
|
399 |
services = new PrintService[1]; |
|
400 |
services[0] = defService; |
|
401 |
return services; |
|
402 |
} else { |
|
403 |
return new PrintService[0]; |
|
404 |
} |
|
405 |
} else { |
|
406 |
/* Its not the default service */ |
|
407 |
PrintService service = getServiceByName(name); |
|
408 |
if (service != null && |
|
409 |
matchesAttributes(service, serviceSet)) { |
|
410 |
services = new PrintService[1]; |
|
411 |
services[0] = service; |
|
412 |
return services; |
|
413 |
} else { |
|
414 |
return new PrintService[0]; |
|
415 |
} |
|
416 |
} |
|
417 |
} else { |
|
418 |
/* specified service attributes don't include a name.*/ |
|
419 |
Vector matchedServices = new Vector(); |
|
420 |
services = getPrintServices(); |
|
421 |
for (int i = 0; i< services.length; i++) { |
|
422 |
if (matchesAttributes(services[i], serviceSet)) { |
|
423 |
matchedServices.add(services[i]); |
|
424 |
} |
|
425 |
} |
|
426 |
services = new PrintService[matchedServices.size()]; |
|
427 |
for (int i = 0; i< services.length; i++) { |
|
428 |
services[i] = (PrintService)matchedServices.elementAt(i); |
|
429 |
} |
|
430 |
return services; |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
/* |
|
435 |
* If service attributes are specified then there must be additional |
|
436 |
* filtering. |
|
437 |
*/ |
|
438 |
public PrintService[] getPrintServices(DocFlavor flavor, |
|
439 |
AttributeSet attributes) { |
|
440 |
SecurityManager security = System.getSecurityManager(); |
|
441 |
if (security != null) { |
|
442 |
security.checkPrintJobAccess(); |
|
443 |
} |
|
444 |
PrintRequestAttributeSet requestSet = null; |
|
445 |
PrintServiceAttributeSet serviceSet = null; |
|
446 |
||
447 |
if (attributes != null && !attributes.isEmpty()) { |
|
448 |
||
449 |
requestSet = new HashPrintRequestAttributeSet(); |
|
450 |
serviceSet = new HashPrintServiceAttributeSet(); |
|
451 |
||
452 |
Attribute[] attrs = attributes.toArray(); |
|
453 |
for (int i=0; i<attrs.length; i++) { |
|
454 |
if (attrs[i] instanceof PrintRequestAttribute) { |
|
455 |
requestSet.add(attrs[i]); |
|
456 |
} else if (attrs[i] instanceof PrintServiceAttribute) { |
|
457 |
serviceSet.add(attrs[i]); |
|
458 |
} |
|
459 |
} |
|
460 |
} |
|
461 |
||
462 |
PrintService[] services = getPrintServices(serviceSet); |
|
463 |
if (services.length == 0) { |
|
464 |
return services; |
|
465 |
} |
|
466 |
||
467 |
if (CUPSPrinter.isCupsRunning()) { |
|
468 |
ArrayList matchingServices = new ArrayList(); |
|
469 |
for (int i=0; i<services.length; i++) { |
|
470 |
try { |
|
471 |
if (services[i]. |
|
472 |
getUnsupportedAttributes(flavor, requestSet) == null) { |
|
473 |
matchingServices.add(services[i]); |
|
474 |
} |
|
475 |
} catch (IllegalArgumentException e) { |
|
476 |
} |
|
477 |
} |
|
478 |
services = new PrintService[matchingServices.size()]; |
|
479 |
return (PrintService[])matchingServices.toArray(services); |
|
480 |
||
481 |
} else { |
|
482 |
// We only need to compare 1 PrintService because all |
|
483 |
// UnixPrintServices are the same anyway. We will not use |
|
484 |
// default PrintService because it might be null. |
|
485 |
PrintService service = services[0]; |
|
486 |
if ((flavor == null || |
|
487 |
service.isDocFlavorSupported(flavor)) && |
|
488 |
service.getUnsupportedAttributes(flavor, requestSet) == null) |
|
489 |
{ |
|
490 |
return services; |
|
491 |
} else { |
|
492 |
return new PrintService[0]; |
|
493 |
} |
|
494 |
} |
|
495 |
} |
|
496 |
||
497 |
/* |
|
498 |
* return empty array as don't support multi docs |
|
499 |
*/ |
|
500 |
public MultiDocPrintService[] |
|
501 |
getMultiDocPrintServices(DocFlavor[] flavors, |
|
502 |
AttributeSet attributes) { |
|
503 |
SecurityManager security = System.getSecurityManager(); |
|
504 |
if (security != null) { |
|
505 |
security.checkPrintJobAccess(); |
|
506 |
} |
|
507 |
return new MultiDocPrintService[0]; |
|
508 |
} |
|
509 |
||
510 |
||
511 |
public synchronized PrintService getDefaultPrintService() { |
|
512 |
SecurityManager security = System.getSecurityManager(); |
|
513 |
if (security != null) { |
|
514 |
security.checkPrintJobAccess(); |
|
515 |
} |
|
516 |
||
517 |
// clear defaultPrintService |
|
518 |
defaultPrintService = null; |
|
519 |
||
520 |
IPPPrintService.debug_println("isRunning ? "+ |
|
521 |
(CUPSPrinter.isCupsRunning())); |
|
522 |
if (CUPSPrinter.isCupsRunning()) { |
|
523 |
defaultPrinter = CUPSPrinter.getDefaultPrinter(); |
|
524 |
} else { |
|
525 |
if (isSysV()) { |
|
526 |
defaultPrinter = getDefaultPrinterNameSysV(); |
|
527 |
} else { |
|
528 |
defaultPrinter = getDefaultPrinterNameBSD(); |
|
529 |
} |
|
530 |
} |
|
531 |
if (defaultPrinter == null) { |
|
532 |
return null; |
|
533 |
} |
|
534 |
defaultPrintService = null; |
|
535 |
if (printServices != null) { |
|
536 |
for (int j=0; j<printServices.length; j++) { |
|
537 |
if (defaultPrinter.equals(printServices[j].getName())) { |
|
538 |
defaultPrintService = printServices[j]; |
|
539 |
break; |
|
540 |
} |
|
541 |
} |
|
542 |
} |
|
543 |
if (defaultPrintService == null) { |
|
544 |
if (CUPSPrinter.isCupsRunning()) { |
|
545 |
try { |
|
546 |
PrintService defaultPS = |
|
547 |
new IPPPrintService(defaultPrinter, |
|
548 |
new URL("http://"+ |
|
549 |
CUPSPrinter.getServer()+":"+ |
|
550 |
CUPSPrinter.getPort()+"/"+ |
|
551 |
defaultPrinter)); |
|
552 |
defaultPrintService = defaultPS; |
|
553 |
} catch (Exception e) { |
|
554 |
} |
|
555 |
} else { |
|
556 |
defaultPrintService = new UnixPrintService(defaultPrinter); |
|
557 |
} |
|
558 |
} |
|
559 |
||
560 |
return defaultPrintService; |
|
561 |
} |
|
562 |
||
563 |
public synchronized void |
|
564 |
getServicesInbackground(BackgroundLookupListener listener) { |
|
565 |
if (printServices != null) { |
|
566 |
listener.notifyServices(printServices); |
|
567 |
} else { |
|
568 |
if (lookupListeners == null) { |
|
569 |
lookupListeners = new Vector(); |
|
570 |
lookupListeners.add(listener); |
|
571 |
Thread lookupThread = new Thread(this); |
|
572 |
lookupThread.start(); |
|
573 |
} else { |
|
574 |
lookupListeners.add(listener); |
|
575 |
} |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
/* This method isn't used in most cases because we rely on code in |
|
580 |
* javax.print.PrintServiceLookup. This is needed just for the cases |
|
581 |
* where those interfaces are by-passed. |
|
582 |
*/ |
|
583 |
private PrintService[] copyOf(PrintService[] inArr) { |
|
584 |
if (inArr == null || inArr.length == 0) { |
|
585 |
return inArr; |
|
586 |
} else { |
|
587 |
PrintService []outArr = new PrintService[inArr.length]; |
|
588 |
System.arraycopy(inArr, 0, outArr, 0, inArr.length); |
|
589 |
return outArr; |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
public void run() { |
|
594 |
PrintService[] services = getPrintServices(); |
|
595 |
synchronized (this) { |
|
596 |
BackgroundLookupListener listener; |
|
597 |
for (int i=0; i<lookupListeners.size(); i++) { |
|
598 |
listener = |
|
599 |
(BackgroundLookupListener)lookupListeners.elementAt(i); |
|
600 |
listener.notifyServices(copyOf(services)); |
|
601 |
} |
|
602 |
lookupListeners = null; |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
private String getDefaultPrinterNameBSD() { |
|
607 |
if (cmdIndex == UNINITIALIZED) { |
|
608 |
cmdIndex = getBSDCommandIndex(); |
|
609 |
} |
|
610 |
String[] names = execCmd(lpcFirstCom[cmdIndex]); |
|
611 |
if (names == null || names.length == 0) { |
|
612 |
return null; |
|
613 |
} |
|
614 |
||
615 |
if ((cmdIndex==BSD_LPD_NG) && |
|
616 |
(names[0].startsWith("missingprinter"))) { |
|
617 |
return null; |
|
618 |
} |
|
619 |
return names[0]; |
|
620 |
} |
|
621 |
||
622 |
private PrintService getNamedPrinterNameBSD(String name) { |
|
623 |
if (cmdIndex == UNINITIALIZED) { |
|
624 |
cmdIndex = getBSDCommandIndex(); |
|
625 |
} |
|
626 |
String command = "/usr/sbin/lpc status " + name + lpcNameCom[cmdIndex]; |
|
627 |
String[] result = execCmd(command); |
|
628 |
||
629 |
if (result == null || !(result[0].equals(name))) { |
|
630 |
return null; |
|
631 |
} |
|
632 |
return new UnixPrintService(name); |
|
633 |
} |
|
634 |
||
635 |
private String[] getAllPrinterNamesBSD() { |
|
636 |
if (cmdIndex == UNINITIALIZED) { |
|
637 |
cmdIndex = getBSDCommandIndex(); |
|
638 |
} |
|
639 |
String[] names = execCmd(lpcAllCom[cmdIndex]); |
|
640 |
if (names == null || names.length == 0) { |
|
641 |
return null; |
|
642 |
} |
|
643 |
return names; |
|
644 |
} |
|
645 |
||
646 |
private String getDefaultPrinterNameSysV() { |
|
647 |
String defaultPrinter = "lp"; |
|
648 |
String command = "/usr/bin/lpstat -d"; |
|
649 |
||
650 |
String [] names = execCmd(command); |
|
651 |
if (names == null || names.length == 0) { |
|
652 |
return defaultPrinter; |
|
653 |
} else { |
|
654 |
int index = names[0].indexOf(":"); |
|
655 |
if (index == -1 || (names[0].length() <= index+1)) { |
|
656 |
return null; |
|
657 |
} else { |
|
658 |
String name = names[0].substring(index+1).trim(); |
|
659 |
if (name.length() == 0) { |
|
660 |
return null; |
|
661 |
} else { |
|
662 |
return name; |
|
663 |
} |
|
664 |
} |
|
665 |
} |
|
666 |
} |
|
667 |
||
668 |
private PrintService getNamedPrinterNameSysV(String name) { |
|
669 |
||
670 |
String command = "/usr/bin/lpstat -v " + name; |
|
671 |
String []result = execCmd(command); |
|
672 |
||
673 |
if (result == null || result[0].indexOf("unknown printer") > 0) { |
|
674 |
return null; |
|
675 |
} else { |
|
676 |
return new UnixPrintService(name); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
private String[] getAllPrinterNamesSysV() { |
|
681 |
String defaultPrinter = "lp"; |
|
682 |
String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort"; |
|
683 |
||
684 |
String [] names = execCmd(command); |
|
685 |
ArrayList printerNames = new ArrayList(); |
|
686 |
for (int i=0; i < names.length; i++) { |
|
687 |
if (!names[i].equals("_default") && |
|
688 |
!names[i].equals(defaultPrinter) && |
|
689 |
!names[i].equals("")) { |
|
690 |
printerNames.add(names[i]); |
|
691 |
} |
|
692 |
} |
|
693 |
return (String[])printerNames.toArray(new String[printerNames.size()]); |
|
694 |
} |
|
695 |
||
696 |
static String[] execCmd(final String command) { |
|
697 |
ArrayList results = null; |
|
698 |
try { |
|
699 |
final String[] cmd = new String[3]; |
|
700 |
if (isSysV()) { |
|
701 |
cmd[0] = "/usr/bin/sh"; |
|
702 |
cmd[1] = "-c"; |
|
703 |
cmd[2] = "env LC_ALL=C " + command; |
|
704 |
} else { |
|
705 |
cmd[0] = "/bin/sh"; |
|
706 |
cmd[1] = "-c"; |
|
707 |
cmd[2] = "LC_ALL=C " + command; |
|
708 |
} |
|
709 |
||
710 |
results = (ArrayList)AccessController.doPrivileged( |
|
711 |
new PrivilegedExceptionAction() { |
|
712 |
public Object run() throws IOException { |
|
713 |
||
714 |
Process proc; |
|
715 |
BufferedReader bufferedReader = null; |
|
716 |
File f = File.createTempFile("prn","xc"); |
|
717 |
cmd[2] = cmd[2]+">"+f.getAbsolutePath(); |
|
718 |
||
719 |
proc = Runtime.getRuntime().exec(cmd); |
|
720 |
try { |
|
721 |
boolean done = false; // in case of interrupt. |
|
722 |
while (!done) { |
|
723 |
try { |
|
724 |
proc.waitFor(); |
|
725 |
done = true; |
|
726 |
} catch (InterruptedException e) { |
|
727 |
} |
|
728 |
} |
|
729 |
||
730 |
if (proc.exitValue() == 0) { |
|
731 |
FileReader reader = new FileReader(f); |
|
732 |
bufferedReader = new BufferedReader(reader); |
|
733 |
String line; |
|
734 |
ArrayList results = new ArrayList(); |
|
735 |
while ((line = bufferedReader.readLine()) |
|
736 |
!= null) { |
|
737 |
results.add(line); |
|
738 |
} |
|
739 |
return results; |
|
740 |
} |
|
741 |
} finally { |
|
742 |
f.delete(); |
|
743 |
// promptly close all streams. |
|
744 |
if (bufferedReader != null) { |
|
745 |
bufferedReader.close(); |
|
746 |
} |
|
747 |
proc.getInputStream().close(); |
|
748 |
proc.getErrorStream().close(); |
|
749 |
proc.getOutputStream().close(); |
|
750 |
} |
|
751 |
return null; |
|
752 |
} |
|
753 |
}); |
|
754 |
} catch (PrivilegedActionException e) { |
|
755 |
} |
|
756 |
if (results == null) { |
|
757 |
return new String[0]; |
|
758 |
} else { |
|
759 |
return (String[])results.toArray(new String[results.size()]); |
|
760 |
} |
|
761 |
} |
|
762 |
||
763 |
private class PrinterChangeListener extends Thread { |
|
764 |
||
765 |
public void run() { |
|
766 |
int refreshSecs; |
|
767 |
while (true) { |
|
768 |
try { |
|
769 |
refreshServices(); |
|
770 |
} catch (Exception se) { |
|
771 |
IPPPrintService.debug_println(debugPrefix+"Exception in refresh thread."); |
|
772 |
break; |
|
773 |
} |
|
774 |
||
775 |
if ((printServices != null) && |
|
776 |
(printServices.length > minRefreshTime)) { |
|
777 |
// compute new refresh time 1 printer = 1 sec |
|
778 |
refreshSecs = printServices.length; |
|
779 |
} else { |
|
780 |
refreshSecs = minRefreshTime; |
|
781 |
} |
|
782 |
try { |
|
783 |
sleep(refreshSecs * 1000); |
|
784 |
} catch (InterruptedException e) { |
|
785 |
break; |
|
786 |
} |
|
787 |
} |
|
788 |
} |
|
789 |
} |
|
790 |
} |