author | mcimadamore |
Tue, 11 Aug 2009 01:12:13 +0100 | |
changeset 3555 | a6fd77fe81df |
parent 1737 | 90d4fe987b09 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.print; |
|
27 |
||
28 |
import java.net.URL; |
|
29 |
import java.net.HttpURLConnection; |
|
30 |
import java.io.OutputStream; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.HashMap; |
|
34 |
import sun.print.IPPPrintService; |
|
35 |
import sun.print.CustomMediaSizeName; |
|
36 |
import sun.print.CustomMediaTray; |
|
37 |
import javax.print.attribute.standard.Media; |
|
38 |
import javax.print.attribute.standard.MediaSizeName; |
|
39 |
import javax.print.attribute.standard.MediaSize; |
|
40 |
import javax.print.attribute.standard.MediaTray; |
|
41 |
import javax.print.attribute.standard.MediaPrintableArea; |
|
42 |
import javax.print.attribute.Size2DSyntax; |
|
43 |
import javax.print.attribute.Attribute; |
|
44 |
import javax.print.attribute.EnumSyntax; |
|
45 |
import javax.print.attribute.standard.PrinterName; |
|
46 |
||
47 |
||
48 |
public class CUPSPrinter { |
|
1737
90d4fe987b09
6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents:
715
diff
changeset
|
49 |
private static final String debugPrefix = "CUPSPrinter>> "; |
2 | 50 |
private static final double PRINTER_DPI = 72.0; |
1737
90d4fe987b09
6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents:
715
diff
changeset
|
51 |
private boolean initialized; |
2 | 52 |
private static native String getCupsServer(); |
53 |
private static native int getCupsPort(); |
|
54 |
private static native boolean canConnect(String server, int port); |
|
55 |
private static native boolean initIDs(); |
|
56 |
// These functions need to be synchronized as |
|
57 |
// CUPS does not support multi-threading. |
|
58 |
private static synchronized native String[] getMedia(String printer); |
|
59 |
private static synchronized native float[] getPageSizes(String printer); |
|
60 |
//public static boolean useIPPMedia = false; will be used later |
|
61 |
||
62 |
private MediaPrintableArea[] cupsMediaPrintables; |
|
63 |
private MediaSizeName[] cupsMediaSNames; |
|
64 |
private CustomMediaSizeName[] cupsCustomMediaSNames; |
|
65 |
private MediaTray[] cupsMediaTrays; |
|
66 |
||
67 |
public int nPageSizes = 0; |
|
68 |
public int nTrays = 0; |
|
69 |
private String[] media; |
|
70 |
private float[] pageSizes; |
|
71 |
private String printer; |
|
72 |
||
73 |
private static boolean libFound; |
|
74 |
private static String cupsServer = null; |
|
75 |
private static int cupsPort = 0; |
|
76 |
||
77 |
static { |
|
78 |
// load awt library to access native code |
|
79 |
java.security.AccessController.doPrivileged( |
|
80 |
new sun.security.action.LoadLibraryAction("awt")); |
|
81 |
libFound = initIDs(); |
|
82 |
if (libFound) { |
|
83 |
cupsServer = getCupsServer(); |
|
84 |
cupsPort = getCupsPort(); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
||
89 |
CUPSPrinter (String printerName) { |
|
90 |
if (printerName == null) { |
|
91 |
throw new IllegalArgumentException("null printer name"); |
|
92 |
} |
|
93 |
printer = printerName; |
|
94 |
cupsMediaSNames = null; |
|
95 |
cupsMediaPrintables = null; |
|
96 |
cupsMediaTrays = null; |
|
97 |
initialized = false; |
|
98 |
||
99 |
if (!libFound) { |
|
100 |
throw new RuntimeException("cups lib not found"); |
|
101 |
} else { |
|
102 |
// get page + tray names |
|
103 |
media = getMedia(printer); |
|
104 |
if (media == null) { |
|
105 |
// either PPD file is not found or printer is unknown |
|
106 |
throw new RuntimeException("error getting PPD"); |
|
107 |
} |
|
108 |
||
109 |
// get sizes |
|
110 |
pageSizes = getPageSizes(printer); |
|
111 |
if (pageSizes != null) { |
|
112 |
nPageSizes = pageSizes.length/6; |
|
113 |
||
114 |
nTrays = media.length/2-nPageSizes; |
|
115 |
assert (nTrays >= 0); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
||
121 |
/** |
|
122 |
* Returns array of MediaSizeNames derived from PPD. |
|
123 |
*/ |
|
124 |
public MediaSizeName[] getMediaSizeNames() { |
|
125 |
initMedia(); |
|
126 |
return cupsMediaSNames; |
|
127 |
} |
|
128 |
||
129 |
||
130 |
/** |
|
131 |
* Returns array of Custom MediaSizeNames derived from PPD. |
|
132 |
*/ |
|
133 |
public CustomMediaSizeName[] getCustomMediaSizeNames() { |
|
134 |
initMedia(); |
|
135 |
return cupsCustomMediaSNames; |
|
136 |
} |
|
137 |
||
138 |
||
139 |
/** |
|
140 |
* Returns array of MediaPrintableArea derived from PPD. |
|
141 |
*/ |
|
142 |
public MediaPrintableArea[] getMediaPrintableArea() { |
|
143 |
initMedia(); |
|
144 |
return cupsMediaPrintables; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Returns array of MediaTrays derived from PPD. |
|
149 |
*/ |
|
150 |
public MediaTray[] getMediaTrays() { |
|
151 |
initMedia(); |
|
152 |
return cupsMediaTrays; |
|
153 |
} |
|
154 |
||
155 |
||
156 |
/** |
|
157 |
* Initialize media by translating PPD info to PrintService attributes. |
|
158 |
*/ |
|
1737
90d4fe987b09
6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents:
715
diff
changeset
|
159 |
private synchronized void initMedia() { |
2 | 160 |
if (initialized) { |
161 |
return; |
|
162 |
} else { |
|
163 |
initialized = true; |
|
164 |
} |
|
165 |
||
166 |
if (pageSizes == null) { |
|
167 |
return; |
|
168 |
} |
|
169 |
||
170 |
cupsMediaPrintables = new MediaPrintableArea[nPageSizes]; |
|
171 |
cupsMediaSNames = new MediaSizeName[nPageSizes]; |
|
172 |
cupsCustomMediaSNames = new CustomMediaSizeName[nPageSizes]; |
|
173 |
||
174 |
CustomMediaSizeName msn; |
|
175 |
MediaPrintableArea mpa; |
|
176 |
float length, width, x, y, w, h; |
|
177 |
||
178 |
// initialize names and printables |
|
179 |
for (int i=0; i<nPageSizes; i++) { |
|
180 |
// media width and length |
|
181 |
width = (float)(pageSizes[i*6]/PRINTER_DPI); |
|
182 |
length = (float)(pageSizes[i*6+1]/PRINTER_DPI); |
|
183 |
// media printable area |
|
184 |
x = (float)(pageSizes[i*6+2]/PRINTER_DPI); |
|
185 |
h = (float)(pageSizes[i*6+3]/PRINTER_DPI); |
|
186 |
w = (float)(pageSizes[i*6+4]/PRINTER_DPI); |
|
187 |
y = (float)(pageSizes[i*6+5]/PRINTER_DPI); |
|
188 |
||
189 |
msn = new CustomMediaSizeName(media[i*2], media[i*2+1], |
|
190 |
width, length); |
|
191 |
||
192 |
// add to list of standard MediaSizeNames |
|
193 |
if ((cupsMediaSNames[i] = msn.getStandardMedia()) == null) { |
|
194 |
// add custom if no matching standard media |
|
195 |
cupsMediaSNames[i] = msn; |
|
196 |
||
197 |
// add this new custom msn to MediaSize array |
|
198 |
if ((width > 0.0) && (length > 0.0)) { |
|
199 |
new MediaSize(width, length, |
|
200 |
Size2DSyntax.INCH, msn); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
// add to list of custom MediaSizeName |
|
205 |
// for internal use of IPPPrintService |
|
206 |
cupsCustomMediaSNames[i] = msn; |
|
207 |
||
208 |
mpa = null; |
|
209 |
try { |
|
210 |
mpa = new MediaPrintableArea(x, y, w, h, |
|
211 |
MediaPrintableArea.INCH); |
|
212 |
} catch (IllegalArgumentException e) { |
|
213 |
if (width > 0 && length > 0) { |
|
214 |
mpa = new MediaPrintableArea(0, 0, width, length, |
|
215 |
MediaPrintableArea.INCH); |
|
216 |
} |
|
217 |
} |
|
218 |
cupsMediaPrintables[i] = mpa; |
|
219 |
} |
|
220 |
||
221 |
// initialize trays |
|
222 |
cupsMediaTrays = new MediaTray[nTrays]; |
|
223 |
||
224 |
MediaTray mt; |
|
225 |
for (int i=0; i<nTrays; i++) { |
|
226 |
mt = new CustomMediaTray(media[(nPageSizes+i)*2], |
|
227 |
media[(nPageSizes+i)*2+1]); |
|
228 |
cupsMediaTrays[i] = mt; |
|
229 |
} |
|
230 |
||
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Get CUPS default printer using IPP. |
|
235 |
*/ |
|
236 |
public static String getDefaultPrinter() { |
|
237 |
try { |
|
238 |
URL url = new URL("http", getServer(), getPort(), ""); |
|
239 |
final HttpURLConnection urlConnection = |
|
240 |
IPPPrintService.getIPPConnection(url); |
|
241 |
||
242 |
if (urlConnection != null) { |
|
243 |
OutputStream os = (OutputStream)java.security.AccessController. |
|
244 |
doPrivileged(new java.security.PrivilegedAction() { |
|
245 |
public Object run() { |
|
246 |
try { |
|
247 |
return urlConnection.getOutputStream(); |
|
248 |
} catch (Exception e) { |
|
249 |
} |
|
250 |
return null; |
|
251 |
} |
|
252 |
}); |
|
253 |
||
254 |
if (os == null) { |
|
255 |
return null; |
|
256 |
} |
|
257 |
||
258 |
AttributeClass attCl[] = { |
|
259 |
AttributeClass.ATTRIBUTES_CHARSET, |
|
260 |
AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, |
|
261 |
new AttributeClass("requested-attributes", |
|
262 |
AttributeClass.TAG_KEYWORD, |
|
263 |
"printer-name") |
|
264 |
}; |
|
265 |
||
266 |
if (IPPPrintService.writeIPPRequest(os, |
|
267 |
IPPPrintService.OP_CUPS_GET_DEFAULT, |
|
268 |
attCl)) { |
|
269 |
||
270 |
HashMap defaultMap = null; |
|
271 |
InputStream is = urlConnection.getInputStream(); |
|
272 |
HashMap[] responseMap = IPPPrintService.readIPPResponse( |
|
273 |
is); |
|
274 |
is.close(); |
|
275 |
||
276 |
if (responseMap.length > 0) { |
|
277 |
defaultMap = responseMap[0]; |
|
278 |
} |
|
279 |
||
280 |
if (defaultMap == null) { |
|
281 |
os.close(); |
|
282 |
urlConnection.disconnect(); |
|
283 |
return null; |
|
284 |
} |
|
285 |
||
286 |
AttributeClass attribClass = (AttributeClass) |
|
287 |
defaultMap.get("printer-name"); |
|
288 |
||
289 |
if (attribClass != null) { |
|
290 |
String nameStr = attribClass.getStringValue(); |
|
291 |
os.close(); |
|
292 |
urlConnection.disconnect(); |
|
293 |
return nameStr; |
|
294 |
} |
|
295 |
} |
|
296 |
os.close(); |
|
297 |
urlConnection.disconnect(); |
|
298 |
} |
|
299 |
} catch (Exception e) { |
|
300 |
} |
|
301 |
return null; |
|
302 |
} |
|
303 |
||
304 |
||
305 |
/** |
|
306 |
* Get list of all CUPS printers using IPP. |
|
307 |
*/ |
|
308 |
public static String[] getAllPrinters() { |
|
309 |
try { |
|
310 |
URL url = new URL("http", getServer(), getPort(), ""); |
|
311 |
||
312 |
final HttpURLConnection urlConnection = |
|
313 |
IPPPrintService.getIPPConnection(url); |
|
314 |
||
315 |
if (urlConnection != null) { |
|
316 |
OutputStream os = (OutputStream)java.security.AccessController. |
|
317 |
doPrivileged(new java.security.PrivilegedAction() { |
|
318 |
public Object run() { |
|
319 |
try { |
|
320 |
return urlConnection.getOutputStream(); |
|
321 |
} catch (Exception e) { |
|
322 |
} |
|
323 |
return null; |
|
324 |
} |
|
325 |
}); |
|
326 |
||
327 |
if (os == null) { |
|
328 |
return null; |
|
329 |
} |
|
330 |
||
331 |
AttributeClass attCl[] = { |
|
332 |
AttributeClass.ATTRIBUTES_CHARSET, |
|
333 |
AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, |
|
334 |
new AttributeClass("requested-attributes", |
|
335 |
AttributeClass.TAG_KEYWORD, |
|
542
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
336 |
"printer-uri-supported") |
2 | 337 |
}; |
338 |
||
339 |
if (IPPPrintService.writeIPPRequest(os, |
|
340 |
IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) { |
|
341 |
||
342 |
InputStream is = urlConnection.getInputStream(); |
|
343 |
HashMap[] responseMap = |
|
344 |
IPPPrintService.readIPPResponse(is); |
|
345 |
||
346 |
is.close(); |
|
347 |
os.close(); |
|
348 |
urlConnection.disconnect(); |
|
349 |
||
350 |
if (responseMap == null || responseMap.length == 0) { |
|
351 |
return null; |
|
352 |
} |
|
353 |
||
354 |
ArrayList printerNames = new ArrayList(); |
|
355 |
for (int i=0; i< responseMap.length; i++) { |
|
356 |
AttributeClass attribClass = (AttributeClass) |
|
542
eb75700cdf75
6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents:
2
diff
changeset
|
357 |
responseMap[i].get("printer-uri-supported"); |
2 | 358 |
|
359 |
if (attribClass != null) { |
|
360 |
String nameStr = attribClass.getStringValue(); |
|
361 |
printerNames.add(nameStr); |
|
362 |
} |
|
363 |
} |
|
364 |
return (String[])printerNames.toArray(new String[] {}); |
|
365 |
} else { |
|
366 |
os.close(); |
|
367 |
urlConnection.disconnect(); |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
} catch (Exception e) { |
|
372 |
} |
|
373 |
return null; |
|
374 |
||
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Returns CUPS server name. |
|
379 |
*/ |
|
380 |
public static String getServer() { |
|
381 |
return cupsServer; |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* Returns CUPS port number. |
|
386 |
*/ |
|
387 |
public static int getPort() { |
|
388 |
return cupsPort; |
|
389 |
} |
|
390 |
||
391 |
/** |
|
392 |
* Detects if CUPS is running. |
|
393 |
*/ |
|
394 |
public static boolean isCupsRunning() { |
|
1737
90d4fe987b09
6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents:
715
diff
changeset
|
395 |
IPPPrintService.debug_println(debugPrefix+"libFound "+libFound); |
2 | 396 |
if (libFound) { |
1737
90d4fe987b09
6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents:
715
diff
changeset
|
397 |
IPPPrintService.debug_println(debugPrefix+"CUPS server "+getServer()+ |
2 | 398 |
" port "+getPort()); |
399 |
return canConnect(getServer(), getPort()); |
|
400 |
} else { |
|
401 |
return false; |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
||
406 |
} |