Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java
41152 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
24
*/
25
26
package sun.print;
27
28
import java.util.ArrayList;
29
30
import javax.print.DocFlavor;
31
import javax.print.MultiDocPrintService;
32
import javax.print.PrintService;
33
import javax.print.PrintServiceLookup;
34
import javax.print.attribute.Attribute;
35
import javax.print.attribute.AttributeSet;
36
import javax.print.attribute.HashPrintRequestAttributeSet;
37
import javax.print.attribute.HashPrintServiceAttributeSet;
38
import javax.print.attribute.PrintRequestAttribute;
39
import javax.print.attribute.PrintRequestAttributeSet;
40
import javax.print.attribute.PrintServiceAttribute;
41
import javax.print.attribute.PrintServiceAttributeSet;
42
import javax.print.attribute.standard.PrinterName;
43
44
@SuppressWarnings("removal")
45
public class PrintServiceLookupProvider extends PrintServiceLookup {
46
47
private PrintService defaultPrintService;
48
private PrintService[] printServices; /* includes the default printer */
49
50
static {
51
java.security.AccessController.doPrivileged(
52
new java.security.PrivilegedAction<Void>() {
53
public Void run() {
54
System.loadLibrary("awt");
55
return null;
56
}
57
});
58
}
59
60
/* The singleton win32 print lookup service.
61
* Code that is aware of this field and wants to use it must first
62
* see if its null, and if so instantiate it by calling a method such as
63
* javax.print.PrintServiceLookup.defaultPrintService() so that the
64
* same instance is stored there.
65
*/
66
private static PrintServiceLookupProvider win32PrintLUS;
67
68
/* Think carefully before calling this. Preferably don't call it. */
69
public static PrintServiceLookupProvider getWin32PrintLUS() {
70
if (win32PrintLUS == null) {
71
/* This call is internally synchronized.
72
* When it returns an instance of this class will have
73
* been instantiated - else there's a JDK internal error.
74
*/
75
PrintServiceLookup.lookupDefaultPrintService();
76
}
77
return win32PrintLUS;
78
}
79
80
public PrintServiceLookupProvider() {
81
82
if (win32PrintLUS == null) {
83
win32PrintLUS = this;
84
85
// start the local printer listener thread
86
Thread thr = new Thread(null, new PrinterChangeListener(),
87
"PrinterListener", 0, false);
88
thr.setDaemon(true);
89
thr.start();
90
91
// start the remote printer listener thread
92
Thread remThr = new Thread(null, new RemotePrinterChangeListener(),
93
"RemotePrinterListener", 0, false);
94
remThr.setDaemon(true);
95
remThr.start();
96
} /* else condition ought to never happen! */
97
}
98
99
/* Want the PrintService which is default print service to have
100
* equality of reference with the equivalent in list of print services
101
* This isn't required by the API and there's a risk doing this will
102
* lead people to assume its guaranteed.
103
*/
104
public synchronized PrintService[] getPrintServices() {
105
SecurityManager security = System.getSecurityManager();
106
if (security != null) {
107
security.checkPrintJobAccess();
108
}
109
if (printServices == null) {
110
refreshServices();
111
}
112
return printServices;
113
}
114
115
private synchronized void refreshServices() {
116
String[] printers = getAllPrinterNames();
117
if (printers == null) {
118
// In Windows it is safe to assume no default if printers == null so we
119
// don't get the default.
120
invalidateServices();
121
printServices = new PrintService[0];
122
return;
123
}
124
125
PrintService[] newServices = new PrintService[printers.length];
126
PrintService defService = getDefaultPrintService();
127
for (int p = 0; p < printers.length; p++) {
128
if (defService != null &&
129
printers[p].equals(defService.getName())) {
130
newServices[p] = defService;
131
} else {
132
if (printServices == null) {
133
newServices[p] = new Win32PrintService(printers[p]);
134
} else {
135
int j;
136
for (j = 0; j < printServices.length; j++) {
137
if ((printServices[j]!= null) &&
138
(printers[p].equals(printServices[j].getName()))) {
139
newServices[p] = printServices[j];
140
printServices[j] = null;
141
break;
142
}
143
}
144
if (j == printServices.length) {
145
newServices[p] = new Win32PrintService(printers[p]);
146
}
147
}
148
}
149
}
150
151
invalidateServices();
152
printServices = newServices;
153
}
154
155
private void invalidateServices() {
156
// Look for deleted services and invalidate these
157
if (printServices != null) {
158
for (int j=0; j < printServices.length; j++) {
159
if ((printServices[j] instanceof Win32PrintService) &&
160
(!printServices[j].equals(defaultPrintService))) {
161
162
((Win32PrintService)printServices[j]).invalidateService();
163
}
164
}
165
}
166
}
167
168
169
public synchronized PrintService getPrintServiceByName(String name) {
170
171
if (name == null || name.isEmpty()) {
172
return null;
173
} else {
174
/* getPrintServices() is now very fast. */
175
PrintService[] printServices = getPrintServices();
176
for (int i=0; i<printServices.length; i++) {
177
if (printServices[i].getName().equals(name)) {
178
return printServices[i];
179
}
180
}
181
return null;
182
}
183
}
184
185
@SuppressWarnings("unchecked") // Cast to Class<PrintServiceAttribute>
186
boolean matchingService(PrintService service,
187
PrintServiceAttributeSet serviceSet) {
188
if (serviceSet != null) {
189
Attribute [] attrs = serviceSet.toArray();
190
Attribute serviceAttr;
191
for (int i=0; i<attrs.length; i++) {
192
serviceAttr
193
= service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
194
if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
195
return false;
196
}
197
}
198
}
199
return true;
200
}
201
202
public PrintService[] getPrintServices(DocFlavor flavor,
203
AttributeSet attributes) {
204
205
SecurityManager security = System.getSecurityManager();
206
if (security != null) {
207
security.checkPrintJobAccess();
208
}
209
PrintRequestAttributeSet requestSet = null;
210
PrintServiceAttributeSet serviceSet = null;
211
212
if (attributes != null && !attributes.isEmpty()) {
213
214
requestSet = new HashPrintRequestAttributeSet();
215
serviceSet = new HashPrintServiceAttributeSet();
216
217
Attribute[] attrs = attributes.toArray();
218
for (int i=0; i<attrs.length; i++) {
219
if (attrs[i] instanceof PrintRequestAttribute) {
220
requestSet.add(attrs[i]);
221
} else if (attrs[i] instanceof PrintServiceAttribute) {
222
serviceSet.add(attrs[i]);
223
}
224
}
225
}
226
227
/*
228
* Special case: If client is asking for a particular printer
229
* (by name) then we can save time by getting just that service
230
* to check against the rest of the specified attributes.
231
*/
232
PrintService[] services = null;
233
if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
234
PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
235
PrintService service = getPrintServiceByName(name.getValue());
236
if (service == null || !matchingService(service, serviceSet)) {
237
services = new PrintService[0];
238
} else {
239
services = new PrintService[1];
240
services[0] = service;
241
}
242
} else {
243
services = getPrintServices();
244
}
245
246
if (services.length == 0) {
247
return services;
248
} else {
249
ArrayList<PrintService> matchingServices = new ArrayList<>();
250
for (int i=0; i<services.length; i++) {
251
try {
252
if (services[i].
253
getUnsupportedAttributes(flavor, requestSet) == null) {
254
matchingServices.add(services[i]);
255
}
256
} catch (IllegalArgumentException e) {
257
}
258
}
259
services = new PrintService[matchingServices.size()];
260
return matchingServices.toArray(services);
261
}
262
}
263
264
/*
265
* return empty array as don't support multi docs
266
*/
267
public MultiDocPrintService[]
268
getMultiDocPrintServices(DocFlavor[] flavors,
269
AttributeSet attributes) {
270
SecurityManager security = System.getSecurityManager();
271
if (security != null) {
272
security.checkPrintJobAccess();
273
}
274
return new MultiDocPrintService[0];
275
}
276
277
278
public synchronized PrintService getDefaultPrintService() {
279
SecurityManager security = System.getSecurityManager();
280
if (security != null) {
281
security.checkPrintJobAccess();
282
}
283
284
285
// Windows does not have notification for a change in default
286
// so we always get the latest.
287
String defaultPrinter = getDefaultPrinterName();
288
if (defaultPrinter == null) {
289
return null;
290
}
291
292
if ((defaultPrintService != null) &&
293
defaultPrintService.getName().equals(defaultPrinter)) {
294
295
return defaultPrintService;
296
}
297
298
// Not the same as default so proceed to get new PrintService.
299
300
// clear defaultPrintService
301
defaultPrintService = null;
302
303
if (printServices != null) {
304
for (int j=0; j<printServices.length; j++) {
305
if (defaultPrinter.equals(printServices[j].getName())) {
306
defaultPrintService = printServices[j];
307
break;
308
}
309
}
310
}
311
312
if (defaultPrintService == null) {
313
defaultPrintService = new Win32PrintService(defaultPrinter);
314
}
315
return defaultPrintService;
316
}
317
318
private final class PrinterChangeListener implements Runnable {
319
@Override
320
public void run() {
321
notifyLocalPrinterChange(); // busy loop in the native code
322
}
323
}
324
325
private final class RemotePrinterChangeListener implements Runnable {
326
@Override
327
public void run() {
328
notifyRemotePrinterChange(); // busy loop in the native code
329
}
330
}
331
332
private native String getDefaultPrinterName();
333
private native String[] getAllPrinterNames();
334
private native void notifyLocalPrinterChange();
335
private native void notifyRemotePrinterChange();
336
}
337
338