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