mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8241829: Cleanup the code for PrinterJob on windows
Reviewed-by: prr, aivanov
This commit is contained in:
parent
84fc4850a6
commit
a62b24f573
3 changed files with 79 additions and 76 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -25,10 +25,10 @@
|
||||||
|
|
||||||
package sun.print;
|
package sun.print;
|
||||||
|
|
||||||
import java.security.AccessController;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
import javax.print.DocFlavor;
|
import javax.print.DocFlavor;
|
||||||
import javax.print.MultiDocPrintService;
|
import javax.print.MultiDocPrintService;
|
||||||
import javax.print.PrintService;
|
import javax.print.PrintService;
|
||||||
|
@ -120,13 +120,6 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
|
||||||
if (win32PrintLUS == null) {
|
if (win32PrintLUS == null) {
|
||||||
win32PrintLUS = this;
|
win32PrintLUS = this;
|
||||||
|
|
||||||
String osName = AccessController.doPrivileged(
|
|
||||||
new sun.security.action.GetPropertyAction("os.name"));
|
|
||||||
// There's no capability for Win98 to refresh printers.
|
|
||||||
// See "OpenPrinter" for more info.
|
|
||||||
if (osName != null && osName.startsWith("Windows 98")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// start the local printer listener thread
|
// start the local printer listener thread
|
||||||
Thread thr = new Thread(null, new PrinterChangeListener(),
|
Thread thr = new Thread(null, new PrinterChangeListener(),
|
||||||
"PrinterListener", 0, false);
|
"PrinterListener", 0, false);
|
||||||
|
@ -356,29 +349,10 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
|
||||||
return defaultPrintService;
|
return defaultPrintService;
|
||||||
}
|
}
|
||||||
|
|
||||||
class PrinterChangeListener implements Runnable {
|
private final class PrinterChangeListener implements Runnable {
|
||||||
long chgObj;
|
|
||||||
PrinterChangeListener() {
|
|
||||||
chgObj = notifyFirstPrinterChange(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (chgObj != -1) {
|
notifyLocalPrinterChange(); // busy loop in the native code
|
||||||
while (true) {
|
|
||||||
// wait for configuration to change
|
|
||||||
if (notifyPrinterChange(chgObj) != 0) {
|
|
||||||
try {
|
|
||||||
refreshServices();
|
|
||||||
} catch (SecurityException se) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
notifyClosePrinterChange(chgObj);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -446,8 +420,6 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
|
||||||
|
|
||||||
private native String getDefaultPrinterName();
|
private native String getDefaultPrinterName();
|
||||||
private native String[] getAllPrinterNames();
|
private native String[] getAllPrinterNames();
|
||||||
private native long notifyFirstPrinterChange(String printer);
|
private native void notifyLocalPrinterChange();
|
||||||
private native void notifyClosePrinterChange(long chgObj);
|
|
||||||
private native int notifyPrinterChange(long chgObj);
|
|
||||||
private native String[] getRemotePrintersNames();
|
private native String[] getRemotePrintersNames();
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,28 +185,20 @@ Java_sun_print_PrintServiceLookupProvider_getRemotePrintersNames(JNIEnv *env,
|
||||||
return getPrinterNames(env, PRINTER_ENUM_CONNECTIONS);
|
return getPrinterNames(env, PRINTER_ENUM_CONNECTIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_sun_print_PrintServiceLookupProvider_notifyLocalPrinterChange(JNIEnv *env,
|
||||||
|
jobject peer)
|
||||||
|
{
|
||||||
|
jclass cls = env->GetObjectClass(peer);
|
||||||
|
CHECK_NULL(cls);
|
||||||
|
jmethodID refresh = env->GetMethodID(cls, "refreshServices", "()V");
|
||||||
|
CHECK_NULL(refresh);
|
||||||
|
|
||||||
JNIEXPORT jlong JNICALL
|
|
||||||
Java_sun_print_PrintServiceLookupProvider_notifyFirstPrinterChange(JNIEnv *env,
|
|
||||||
jobject peer,
|
|
||||||
jstring printer) {
|
|
||||||
HANDLE hPrinter;
|
HANDLE hPrinter;
|
||||||
|
LPTSTR printerName = NULL; // NULL indicates the local printer server
|
||||||
LPTSTR printerName = NULL;
|
if (!::OpenPrinter(printerName, &hPrinter, NULL)) {
|
||||||
if (printer != NULL) {
|
return;
|
||||||
printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
|
|
||||||
printer,
|
|
||||||
NULL);
|
|
||||||
JNU_ReleaseStringPlatformChars(env, printer, printerName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// printerName - "Win NT/2K/XP: If NULL, it indicates the local printer
|
|
||||||
// server" - MSDN. Win9x : OpenPrinter returns 0.
|
|
||||||
BOOL ret = OpenPrinter(printerName, &hPrinter, NULL);
|
|
||||||
if (!ret) {
|
|
||||||
return (jlong)-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// PRINTER_CHANGE_PRINTER = PRINTER_CHANGE_ADD_PRINTER |
|
// PRINTER_CHANGE_PRINTER = PRINTER_CHANGE_ADD_PRINTER |
|
||||||
// PRINTER_CHANGE_SET_PRINTER |
|
// PRINTER_CHANGE_SET_PRINTER |
|
||||||
// PRINTER_CHANGE_DELETE_PRINTER |
|
// PRINTER_CHANGE_DELETE_PRINTER |
|
||||||
|
@ -215,32 +207,23 @@ Java_sun_print_PrintServiceLookupProvider_notifyFirstPrinterChange(JNIEnv *env,
|
||||||
PRINTER_CHANGE_PRINTER,
|
PRINTER_CHANGE_PRINTER,
|
||||||
0,
|
0,
|
||||||
NULL);
|
NULL);
|
||||||
return (chgObj == INVALID_HANDLE_VALUE) ? (jlong)-1 : (jlong)chgObj;
|
if (chgObj != INVALID_HANDLE_VALUE) {
|
||||||
}
|
BOOL keepMonitoring;
|
||||||
|
do {
|
||||||
|
keepMonitoring = FALSE;
|
||||||
|
if (WaitForSingleObject(chgObj, INFINITE) == WAIT_OBJECT_0) {
|
||||||
|
DWORD dwChange;
|
||||||
|
keepMonitoring = FindNextPrinterChangeNotification(
|
||||||
|
chgObj, &dwChange, NULL, NULL);
|
||||||
|
}
|
||||||
|
if (keepMonitoring) {
|
||||||
|
env->CallVoidMethod(peer, refresh);
|
||||||
|
}
|
||||||
|
} while (keepMonitoring && !env->ExceptionCheck());
|
||||||
|
|
||||||
|
FindClosePrinterChangeNotification(chgObj);
|
||||||
|
|
||||||
JNIEXPORT void JNICALL
|
|
||||||
Java_sun_print_PrintServiceLookupProvider_notifyClosePrinterChange(JNIEnv *env,
|
|
||||||
jobject peer,
|
|
||||||
jlong chgObject) {
|
|
||||||
FindClosePrinterChangeNotification((HANDLE)chgObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL
|
|
||||||
Java_sun_print_PrintServiceLookupProvider_notifyPrinterChange(JNIEnv *env,
|
|
||||||
jobject peer,
|
|
||||||
jlong chgObject) {
|
|
||||||
DWORD dwChange;
|
|
||||||
|
|
||||||
DWORD ret = WaitForSingleObject((HANDLE)chgObject, INFINITE);
|
|
||||||
if (ret == WAIT_OBJECT_0) {
|
|
||||||
return(FindNextPrinterChangeNotification((HANDLE)chgObject,
|
|
||||||
&dwChange, NULL, NULL));
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
::ClosePrinter(hPrinter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
48
test/jdk/java/awt/print/PrintServicesSecurityManager.java
Normal file
48
test/jdk/java/awt/print/PrintServicesSecurityManager.java
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import javax.print.PrintServiceLookup;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @bug 8241829
|
||||||
|
*/
|
||||||
|
public final class PrintServicesSecurityManager {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
System.setSecurityManager(new SecurityManager());
|
||||||
|
test();
|
||||||
|
Thread.sleep(3000); // to be sure the pooling thread started
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void test() {
|
||||||
|
Object[] services = PrintServiceLookup.lookupPrintServices(null, null);
|
||||||
|
if (services.length != 0) {
|
||||||
|
System.err.println("services = " + Arrays.toString(services));
|
||||||
|
throw new RuntimeException("The array of Services must be empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue