Skip to content

Commit 67f18cc

Browse files
committed
8345142: Remove uses of SecurityManager in Printing related classes
Reviewed-by: azvegint, psadhukhan
1 parent 352201d commit 67f18cc

16 files changed

+37
-309
lines changed

src/java.desktop/share/classes/java/awt/print/PrinterJob.java

-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ public abstract class PrinterJob {
6464
* @return a new {@code PrinterJob}.
6565
*/
6666
public static PrinterJob getPrinterJob() {
67-
@SuppressWarnings("removal")
68-
SecurityManager security = System.getSecurityManager();
69-
if (security != null) {
70-
security.checkPrintJobAccess();
71-
}
7267
return sun.print.PlatformPrinterJobProxy.getPrinterJob();
7368
}
7469

src/java.desktop/share/classes/javax/print/PrintServiceLookup.java

+2-20
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,7 @@ private static ArrayList<PrintService> getServices(DocFlavor flavor,
393393
/*
394394
* add any directly registered services
395395
*/
396-
ArrayList<PrintService> registeredServices = null;
397-
try {
398-
@SuppressWarnings("removal")
399-
SecurityManager security = System.getSecurityManager();
400-
if (security != null) {
401-
security.checkPrintJobAccess();
402-
}
403-
registeredServices = getRegisteredServices();
404-
} catch (SecurityException se) {
405-
}
396+
ArrayList<PrintService> registeredServices = getRegisteredServices();
406397
if (registeredServices != null) {
407398
PrintService[] services = registeredServices.toArray(
408399
new PrintService[registeredServices.size()]);
@@ -458,16 +449,7 @@ private static ArrayList<MultiDocPrintService> getMultiDocServices(DocFlavor[] f
458449
/*
459450
* add any directly registered services
460451
*/
461-
ArrayList<PrintService> registeredServices = null;
462-
try {
463-
@SuppressWarnings("removal")
464-
SecurityManager security = System.getSecurityManager();
465-
if (security != null) {
466-
security.checkPrintJobAccess();
467-
}
468-
registeredServices = getRegisteredServices();
469-
} catch (Exception e) {
470-
}
452+
ArrayList<PrintService> registeredServices = getRegisteredServices();
471453
if (registeredServices != null) {
472454
PrintService[] services =
473455
registeredServices.toArray(new PrintService[registeredServices.size()]);

src/java.desktop/share/classes/javax/print/ServiceUI.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ public static PrintService printDialog(GraphicsConfiguration gc,
203203
flavor, attributes,
204204
owner);
205205
if (setOnTop) {
206-
try {
207-
dialog.setAlwaysOnTop(true);
208-
} catch (SecurityException e) {
209-
}
206+
dialog.setAlwaysOnTop(true);
210207
}
211208
Rectangle dlgBounds = dialog.getBounds();
212209

src/java.desktop/share/classes/sun/print/PSStreamPrintJob.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,7 @@ public void pageableJob(Pageable pageable,
434434
}
435435

436436
/* add the user name to the job */
437-
String userName = "";
438-
try {
439-
userName = System.getProperty("user.name");
440-
} catch (SecurityException se) {
441-
}
442-
437+
String userName = System.getProperty("user.name");
443438
if (userName == null || userName.isEmpty()) {
444439
RequestingUserName ruName =
445440
(RequestingUserName)reqSet.get(RequestingUserName.class);

src/java.desktop/share/classes/sun/print/PrintJob2D.java

+10-32
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,6 @@ private void initPrintJob2D(Frame frame, String doctitle,
310310
JobAttributes jobAttributes,
311311
PageAttributes pageAttributes) {
312312

313-
@SuppressWarnings("removal")
314-
SecurityManager security = System.getSecurityManager();
315-
if (security != null) {
316-
security.checkPrintJobAccess();
317-
}
318-
319313
if (frame == null &&
320314
(jobAttributes == null ||
321315
jobAttributes.getDialog() == DialogType.NATIVE)) {
@@ -366,11 +360,6 @@ private void initPrintJob2D(Frame frame, String doctitle,
366360
} catch (IOException ioe) {
367361
throw new IllegalArgumentException("Cannot write to file:"+
368362
destStr);
369-
} catch (SecurityException se) {
370-
//There is already file read/write access so at this point
371-
// only delete access is denied. Just ignore it because in
372-
// most cases the file created in createNewFile gets overwritten
373-
// anyway.
374363
}
375364

376365
File pFile = f.getParentFile();
@@ -676,29 +665,18 @@ private void copyAttributes(PrintService printServ) {
676665
attributes.add(defaultDest);
677666
} else {
678667
URI uri = null;
679-
try {
680-
if (fileName != null) {
681-
if (fileName.isEmpty()) {
682-
fileName = ".";
683-
}
684-
} else {
685-
// defaultDest should not be null. The following code
686-
// is only added to safeguard against a possible
687-
// buggy implementation of a PrintService having a
688-
// null default Destination.
689-
fileName = "out.prn";
690-
}
691-
uri = (new File(fileName)).toURI();
692-
} catch (SecurityException se) {
693-
try {
694-
// '\\' file separator is illegal character in opaque
695-
// part and causes URISyntaxException, so we replace
696-
// it with '/'
697-
fileName = fileName.replace('\\', '/');
698-
uri = new URI("file:"+fileName);
699-
} catch (URISyntaxException e) {
668+
if (fileName != null) {
669+
if (fileName.isEmpty()) {
670+
fileName = ".";
700671
}
672+
} else {
673+
// defaultDest should not be null. The following code
674+
// is only added to safeguard against a possible
675+
// buggy implementation of a PrintService having a
676+
// null default Destination.
677+
fileName = "out.prn";
701678
}
679+
uri = (new File(fileName)).toURI();
702680
if (uri != null) {
703681
attributes.add(new Destination(uri));
704682
}

src/java.desktop/share/classes/sun/print/RasterPrinterJob.java

+3-29
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,7 @@ public PageFormat pageDialog(final PrintRequestAttributeSet attributes)
816816
DocFlavor.SERVICE_FORMATTED.PAGEABLE,
817817
attributes, w);
818818
if (setOnTop) {
819-
try {
820-
pageDialog.setAlwaysOnTop(true);
821-
} catch (SecurityException e) {
822-
}
819+
pageDialog.setAlwaysOnTop(true);
823820
}
824821

825822
Rectangle dlgBounds = pageDialog.getBounds();
@@ -948,15 +945,6 @@ public boolean printDialog(final PrintRequestAttributeSet attributes)
948945

949946
}
950947

951-
/* A security check has already been performed in the
952-
* java.awt.print.printerJob.getPrinterJob method.
953-
* So by the time we get here, it is OK for the current thread
954-
* to print either to a file (from a Dialog we control!) or
955-
* to a chosen printer.
956-
*
957-
* We raise privilege when we put up the dialog, to avoid
958-
* the "warning applet window" banner.
959-
*/
960948
GraphicsConfiguration grCfg = null;
961949
Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
962950
if (w != null) {
@@ -1311,11 +1299,7 @@ protected void setAttributes(PrintRequestAttributeSet attributes)
13111299
(!fidelity && userName != null)) {
13121300
userNameAttr = userName.getValue();
13131301
} else {
1314-
try {
1315-
userNameAttr = getUserName();
1316-
} catch (SecurityException e) {
1317-
userNameAttr = "";
1318-
}
1302+
userNameAttr = getUserName();
13191303
}
13201304

13211305
/* OpenBook is used internally only when app uses Printable.
@@ -1654,11 +1638,6 @@ protected void validateDestination(String dest) throws PrinterException {
16541638
} catch (IOException ioe) {
16551639
throw new PrinterException("Cannot write to file:"+
16561640
dest);
1657-
} catch (SecurityException se) {
1658-
//There is already file read/write access so at this point
1659-
// only delete access is denied. Just ignore it because in
1660-
// most cases the file created in createNewFile gets overwritten
1661-
// anyway.
16621641
}
16631642

16641643
File pFile = f.getParentFile();
@@ -1818,7 +1797,6 @@ protected int getCopiesInt() {
18181797

18191798
/**
18201799
* Get the name of the printing user.
1821-
* The caller must have security permission to read system properties.
18221800
*/
18231801
public String getUserName() {
18241802
return System.getProperty("user.name");
@@ -1831,11 +1809,7 @@ protected String getUserNameInt() {
18311809
if (userNameAttr != null) {
18321810
return userNameAttr;
18331811
} else {
1834-
try {
1835-
return getUserName();
1836-
} catch (SecurityException e) {
1837-
return "";
1838-
}
1812+
return getUserName();
18391813
}
18401814
}
18411815

src/java.desktop/share/classes/sun/print/ServiceDialog.java

+3-20
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ void initPrintDialog(int x, int y,
169169
* on top property
170170
*/
171171
if ((getOwner() == null) || (owner.getOwner() != getOwner())) {
172-
try {
173-
setAlwaysOnTop(true);
174-
} catch (SecurityException e) {
175-
}
172+
setAlwaysOnTop(true);
176173
}
177174
}
178175
Container c = getContentPane();
@@ -255,10 +252,7 @@ void initPageDialog(int x, int y,
255252
/* See comments in same block in initPrintDialog */
256253
DialogOwner owner = (DialogOwner)attributes.get(DialogOwner.class);
257254
if ((getOwner() == null) || (owner.getOwner() != getOwner())) {
258-
try {
259-
setAlwaysOnTop(true);
260-
} catch (SecurityException e) {
261-
}
255+
setAlwaysOnTop(true);
262256
}
263257
}
264258

@@ -2937,13 +2931,7 @@ public void setSelected(boolean selected) {
29372931
private static class ValidatingFileChooser extends JFileChooser {
29382932
public void approveSelection() {
29392933
File selected = getSelectedFile();
2940-
boolean exists;
2941-
2942-
try {
2943-
exists = selected.exists();
2944-
} catch (SecurityException e) {
2945-
exists = false;
2946-
}
2934+
boolean exists = selected.exists();
29472935

29482936
if (exists) {
29492937
int val;
@@ -2966,11 +2954,6 @@ public void approveSelection() {
29662954
getMsg("dialog.owtitle"),
29672955
JOptionPane.WARNING_MESSAGE);
29682956
return;
2969-
} catch (SecurityException se) {
2970-
//There is already file read/write access so at this point
2971-
// only delete access is denied. Just ignore it because in
2972-
// most cases the file created in createNewFile gets
2973-
// overwritten anyway.
29742957
}
29752958
File pFile = selected.getParentFile();
29762959
if ((selected.exists() &&

src/java.desktop/share/classes/sun/print/ServiceNotifier.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ class ServiceNotifier extends Thread {
5555
super(null, null, service.getName() + " notifier", 0, false);
5656
this.service = service;
5757
listeners = new Vector<>();
58-
try {
59-
setPriority(Thread.NORM_PRIORITY-1);
60-
setDaemon(true);
61-
start();
62-
} catch (SecurityException e) {
63-
}
58+
setPriority(Thread.NORM_PRIORITY-1);
59+
setDaemon(true);
60+
start();
6461
}
6562

6663
void addListener(PrintServiceAttributeListener listener) {
@@ -93,10 +90,7 @@ void stopNotifier() {
9390
* immediate notification of listeners.
9491
*/
9592
void wake() {
96-
try {
97-
interrupt();
98-
} catch (SecurityException e) {
99-
}
93+
interrupt();
10094
}
10195

10296
/* A heuristic is used to calculate sleep time.

src/java.desktop/unix/classes/sun/print/IPPPrintService.java

+3-32
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,6 @@ private void initAttributes() {
511511

512512

513513
public DocPrintJob createPrintJob() {
514-
@SuppressWarnings("removal")
515-
SecurityManager security = System.getSecurityManager();
516-
if (security != null) {
517-
security.checkPrintJobAccess();
518-
}
519514
// REMIND: create IPPPrintJob
520515
return new UnixPrintJob(this);
521516
}
@@ -587,15 +582,7 @@ public DocPrintJob createPrintJob() {
587582
if (flavor == null ||
588583
flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
589584
flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
590-
try {
591585
return new Destination((new File("out.ps")).toURI());
592-
} catch (SecurityException se) {
593-
try {
594-
return new Destination(new URI("file:out.ps"));
595-
} catch (URISyntaxException e) {
596-
return null;
597-
}
598-
}
599586
}
600587
return null;
601588
} else if (category == Fidelity.class) {
@@ -797,11 +784,7 @@ public DocPrintJob createPrintJob() {
797784
return null;
798785
}
799786
} else if (category == RequestingUserName.class) {
800-
String userName = "";
801-
try {
802-
userName = System.getProperty("user.name", "");
803-
} catch (SecurityException se) {
804-
}
787+
String userName = System.getProperty("user.name", "");
805788
return new RequestingUserName(userName, null);
806789
} else if (category == Sides.class) {
807790
// The printer takes care of Sides so if short-edge
@@ -1574,15 +1557,7 @@ public boolean isAttributeValueSupported(Attribute attr,
15741557
} else if (category == Chromaticity.class) {
15751558
return Chromaticity.COLOR;
15761559
} else if (category == Destination.class) {
1577-
try {
1578-
return new Destination((new File("out.ps")).toURI());
1579-
} catch (SecurityException se) {
1580-
try {
1581-
return new Destination(new URI("file:out.ps"));
1582-
} catch (URISyntaxException e) {
1583-
return null;
1584-
}
1585-
}
1560+
return new Destination((new File("out.ps")).toURI());
15861561
} else if (category == Fidelity.class) {
15871562
return Fidelity.FIDELITY_FALSE;
15881563
} else if (category == Finishings.class) {
@@ -1674,11 +1649,7 @@ public boolean isAttributeValueSupported(Attribute attr,
16741649
return new PageRanges(1, Integer.MAX_VALUE);
16751650
}
16761651
} else if (category == RequestingUserName.class) {
1677-
String userName = "";
1678-
try {
1679-
userName = System.getProperty("user.name", "");
1680-
} catch (SecurityException se) {
1681-
}
1652+
String userName = System.getProperty("user.name", "");
16821653
return new RequestingUserName(userName, null);
16831654
} else if (category == SheetCollate.class) {
16841655
return SheetCollate.UNCOLLATED;

0 commit comments

Comments
 (0)