Skip to content

Commit 9d85cef

Browse files
committed
Remove deprecated SecurityManager, AccessController, SecurityException #184
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
1 parent eca8e08 commit 9d85cef

File tree

14 files changed

+81
-519
lines changed

14 files changed

+81
-519
lines changed

core/src/main/java/org/eclipse/angus/mail/auth/OAuth2SaslClientFactory.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -73,11 +73,7 @@ public String[] getMechanismNames(Map<String, ?> props) {
7373
* If we're not allowed to add this provider, just give up silently.
7474
*/
7575
public static void init() {
76-
try {
77-
if (Security.getProvider(PROVIDER_NAME) == null)
78-
Security.addProvider(new OAuth2Provider());
79-
} catch (SecurityException ex) {
80-
// oh well...
81-
}
76+
if (Security.getProvider(PROVIDER_NAME) == null)
77+
Security.addProvider(new OAuth2Provider());
8278
}
8379
}

core/src/main/java/org/eclipse/angus/mail/util/PropUtil.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -108,27 +108,7 @@ public static boolean getBooleanSessionProperty(Session session,
108108
* @return the property value
109109
*/
110110
public static boolean getBooleanSystemProperty(String name, boolean def) {
111-
try {
112-
return getBoolean(getProp(System.getProperties(), name), def);
113-
} catch (SecurityException sex) {
114-
// fall through...
115-
}
116-
117-
/*
118-
* If we can't get the entire System Properties object because
119-
* of a SecurityException, just ask for the specific property.
120-
*/
121-
try {
122-
String value = System.getProperty(name);
123-
if (value == null)
124-
return def;
125-
if (def)
126-
return !value.equalsIgnoreCase("false");
127-
else
128-
return value.equalsIgnoreCase("true");
129-
} catch (SecurityException sex) {
130-
return def;
131-
}
111+
return getBoolean(getProp(System.getProperties(), name), def);
132112
}
133113

134114
/**

core/src/main/java/org/eclipse/angus/mail/util/SocketFetcher.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -37,10 +37,8 @@
3737
import java.net.SocketTimeoutException;
3838
import java.nio.channels.SocketChannel;
3939
import java.nio.charset.StandardCharsets;
40-
import java.security.AccessController;
4140
import java.security.cert.CertificateException;
4241
import java.security.GeneralSecurityException;
43-
import java.security.PrivilegedAction;
4442
import java.security.cert.CertificateParsingException;
4543
import java.security.cert.X509Certificate;
4644
import java.util.ArrayList;
@@ -963,22 +961,9 @@ private static String[] stringArray(String s) {
963961

964962
/**
965963
* Convenience method to get our context class loader.
966-
* Assert any privileges we might have and then call the
967-
* Thread.getContextClassLoader method.
968964
*/
969965
private static ClassLoader getContextClassLoader() {
970-
return
971-
AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
972-
@Override
973-
public ClassLoader run() {
974-
ClassLoader cl = null;
975-
try {
976-
cl = Thread.currentThread().getContextClassLoader();
977-
} catch (SecurityException ex) {
978-
}
979-
return cl;
980-
}
981-
});
966+
return Thread.currentThread().getContextClassLoader();
982967
}
983968

984969
/**
@@ -1061,7 +1046,7 @@ public boolean verify(String server, SSLSession ssls) {
10611046
}
10621047

10631048
// XXX - following is a *very* crude parse of the name and ignores
1064-
// all sorts of important issues such as quoting
1049+
// all sorts of important issues such as quoting
10651050
Pattern p = Pattern.compile("CN=([^,]*)");
10661051
Matcher m = p.matcher(cert.getSubjectX500Principal().getName());
10671052
if (m.find() && matchServer(server, m.group(1).trim()))

mailhandler/src/main/java/org/eclipse/angus/mail/util/logging/CollectorFormatter.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2013, 2023 Jason Mehrens. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -468,7 +468,6 @@ record = last;
468468
* Sets the message format string for this formatter.
469469
*
470470
* @param format the format pattern or null for default pattern.
471-
* @throws SecurityException if a security manager exists and the caller
472471
* does not have <code>LoggingPermission("control")</code>.
473472
* @since Angus Mail 2.0.3
474473
*/
@@ -494,7 +493,6 @@ private synchronized void setFormat0(String v) {
494493
* Gets the message format string for this formatter.
495494
*
496495
* @return a non null format pattern.
497-
* @throws SecurityException if a security manager exists and the caller
498496
* does not have <code>LoggingPermission("control")</code>.
499497
* @since Angus Mail 2.0.3
500498
*/
@@ -507,7 +505,6 @@ public synchronized String getFormat() {
507505
* Sets the target formatter.
508506
*
509507
* @param f the target formatter or null to use a LogManager default.
510-
* @throws SecurityException if a security manager exists and the caller
511508
* does not have <code>LoggingPermission("control")</code>.
512509
* @since Angus Mail 2.0.3
513510
*/
@@ -535,7 +532,6 @@ private synchronized void setFormatter0(Formatter f) {
535532
* Gets the target formatter.
536533
*
537534
* @return a non null formatter.
538-
* @throws SecurityException if a security manager exists and the caller
539535
* does not have <code>LoggingPermission("control")</code>.
540536
* @since Angus Mail 2.0.3
541537
*/
@@ -549,7 +545,6 @@ public synchronized Formatter getFormatter() {
549545
*
550546
* @param c the comparator used to order LogRecords. A null value can be
551547
* used to specify choosing the last seen record.
552-
* @throws SecurityException if a security manager exists and the caller
553548
* does not have <code>LoggingPermission("control")</code>.
554549
* @since Angus Mail 2.0.3
555550
*/
@@ -574,7 +569,6 @@ private synchronized void setComparator0(Comparator<? super LogRecord> c) {
574569
*
575570
* @return the comparator used to order LogRecords or null if choosing the
576571
* most recent record.
577-
* @throws SecurityException if a security manager exists and the caller
578572
* does not have <code>LoggingPermission("control")</code>.
579573
* @since Angus Mail 2.0.3
580574
*/

mailhandler/src/main/java/org/eclipse/angus/mail/util/logging/CompactFormatter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2013, 2024 Jason Mehrens. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -640,7 +640,6 @@ private boolean isReflection(final StackTraceElement s) {
640640
* Gets the format pattern for this formatter.
641641
*
642642
* @return the format pattern.
643-
* @throws SecurityException if a security manager exists and the caller
644643
* does not have <code>LoggingPermission("control")</code>.
645644
* @since Angus Mail 2.0.3
646645
*/
@@ -653,7 +652,6 @@ public String getFormat() {
653652
* Sets the format pattern for this formatter.
654653
*
655654
* @param format the format pattern. If null, the default pattern is used.
656-
* @throws SecurityException if a security manager exists and the caller
657655
* does not have <code>LoggingPermission("control")</code>.
658656
* @since Angus Mail 2.0.3
659657
*/

mailhandler/src/main/java/org/eclipse/angus/mail/util/logging/DurationFilter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2015, 2023 Jason Mehrens. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -211,7 +211,6 @@ public boolean isLoggable() {
211211
* Gets the max number of records per duration.
212212
*
213213
* @return the max number of records per duration.
214-
* @throws SecurityException if a security manager exists and the caller
215214
* does not have <code>LoggingPermission("control")</code>.
216215
* @since Angus Mail 2.0.3
217216
*/
@@ -224,7 +223,6 @@ public synchronized long getRecords() {
224223
* Sets the max number of records per duration.
225224
*
226225
* @param records the max number of records per duration.
227-
* @throws SecurityException if a security manager exists and the caller
228226
* does not have <code>LoggingPermission("control")</code>.
229227
* @since Angus Mail 2.0.3
230228
*/
@@ -237,7 +235,6 @@ public synchronized void setRecords(long records) {
237235
* Sets the duration in milliseconds used to determine the log record rate.
238236
*
239237
* @param duration in milliseconds to determine the log record rate.
240-
* @throws SecurityException if a security manager exists and the caller
241238
* does not have <code>LoggingPermission("control")</code>.
242239
* @since Angus Mail 2.0.3
243240
*/
@@ -250,7 +247,6 @@ public synchronized void setDurationMillis(long duration) {
250247
* Gets the duration in milliseconds used to determine the log record rate.
251248
*
252249
* @return duration in milliseconds used to determine the log record rate.
253-
* @throws SecurityException if a security manager exists and the caller
254250
* does not have <code>LoggingPermission("control")</code>.
255251
* @since Angus Mail 2.0.3
256252
*/

mailhandler/src/main/java/org/eclipse/angus/mail/util/logging/LogManagerProperties.java

Lines changed: 8 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2025 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2009, 2024 Jason Mehrens. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -25,7 +25,6 @@
2525
import java.lang.reflect.Method;
2626
import java.lang.reflect.Modifier;
2727
import java.lang.reflect.UndeclaredThrowableException;
28-
import java.security.PrivilegedAction;
2928
import java.util.Collections;
3029
import java.util.Comparator;
3130
import java.util.Enumeration;
@@ -237,10 +236,8 @@ static String fromLogManager(final String name) {
237236

238237
/**
239238
* Check that the current context is trusted to modify the logging
240-
* configuration. This requires LoggingPermission("control").
239+
* configuration.
241240
*
242-
* @throws SecurityException if a security manager exists and the caller
243-
* does not have {@code LoggingPermission("control")}.
244241
* @since JavaMail 1.5.3
245242
*/
246243
static void checkLogManagerAccess() {
@@ -254,10 +251,6 @@ static void checkLogManagerAccess() {
254251
checked = true;
255252
} catch (InvocationTargetException ite) {
256253
Throwable cause = ite.getCause();
257-
if (cause instanceof SecurityException) {
258-
checked = true;
259-
throw (SecurityException) cause;
260-
}
261254

262255
if (cause instanceof UnsupportedOperationException) {
263256
checked = true;
@@ -267,10 +260,6 @@ static void checkLogManagerAccess() {
267260
} catch (ReflectiveOperationException fallthrough) {
268261
}
269262
}
270-
} catch (final SecurityException notAllowed) {
271-
if (checked) {
272-
throw notAllowed;
273-
}
274263
} catch (final LinkageError | RuntimeException restricted) {
275264
} //GAE will forbid access to LogManager
276265
}
@@ -384,7 +373,6 @@ static Long getLongThreadID(final LogRecord record) {
384373
* @throws Exception if there is a problem.
385374
* @throws NoSuchMethodException if the given service does not have a method
386375
* to get the local host name as a string.
387-
* @throws SecurityException if unable to inspect properties of object.
388376
* @since JavaMail 1.5.3
389377
*/
390378
static String getLocalHost(final Object s) throws Exception {
@@ -418,8 +406,6 @@ static String getLocalHost(final Object s) throws Exception {
418406
* @throws ExceptionInInitializerError if the static initializer fails.
419407
* @throws Exception if there is a problem.
420408
* @throws NoSuchMethodException if the correct time methods are missing.
421-
* @throws SecurityException if reflective access to the java.time classes
422-
* are not allowed.
423409
* @since JavaMail 1.5.5
424410
*/
425411
static long parseDurationToMillis(final CharSequence value) throws Exception {
@@ -622,7 +608,6 @@ static ErrorManager newErrorManager(String name) throws Exception {
622608
* @throws LinkageError if the linkage fails.
623609
* @throws ExceptionInInitializerError if the static initializer fails.
624610
* @throws Exception to match the error method of the ErrorManager.
625-
* @throws SecurityException if unable to inspect properties of class.
626611
* @since JavaMail 1.5.2
627612
*/
628613
static boolean isStaticUtilityClass(String name) throws Exception {
@@ -656,7 +641,6 @@ static boolean isStaticUtilityClass(String name) throws Exception {
656641
* @throws LinkageError if the linkage fails.
657642
* @throws ExceptionInInitializerError if the static initializer fails.
658643
* @throws Exception to match the error method of the ErrorManager.
659-
* @throws SecurityException if unable to inspect properties of class.
660644
* @since JavaMail 1.5.2
661645
*/
662646
static boolean isReflectionClass(String name) throws Exception {
@@ -844,82 +828,15 @@ private static Class<?> tryLoad(String name, ClassLoader l) throws ClassNotFound
844828
}
845829

846830
/**
847-
* Gets the class loaders using elevated privileges.
831+
* Gets the class loaders.
848832
*
849-
* @return any array of class loaders. Indexes may be null.
833+
* @return any array of class loaders.
850834
*/
851835
private static ClassLoader[] getClassLoaders() {
852-
return runOrDoPrivileged(new PrivilegedAction<ClassLoader[]>() {
853-
854-
@SuppressWarnings("override") //JDK-6954234
855-
public ClassLoader[] run() {
856-
final ClassLoader[] loaders = new ClassLoader[2];
857-
try {
858-
loaders[0] = ClassLoader.getSystemClassLoader();
859-
} catch (SecurityException ignore) {
860-
loaders[0] = null;
861-
}
862-
863-
try {
864-
loaders[1] = Thread.currentThread().getContextClassLoader();
865-
} catch (SecurityException ignore) {
866-
loaders[1] = null;
867-
}
868-
return loaders;
869-
}
870-
});
871-
}
872-
873-
/**
874-
* Executes a PrivilegedAction without permissions then falling back to
875-
* running with elevated permissions.
876-
*
877-
* Any unchecked exceptions from the action are passed through this API.
878-
*
879-
* @param <T> the action return type.
880-
* @param a the PrivilegedAction object.
881-
* @return the result.
882-
* @throws NullPointerException if the given action is null.
883-
* @throws UndeclaredThrowableException if a checked exception is thrown.
884-
* @since Angus Mail 2.0.3
885-
*/
886-
static <T> T runOrDoPrivileged(final PrivilegedAction<T> a) {
887-
Objects.requireNonNull(a);
888-
try {
889-
return a.run();
890-
} catch (SecurityException sandbox) {
891-
return invokeAccessController(a);
892-
}
893-
}
894-
895-
/**
896-
* Reflective call to access controller for sandbox environments.
897-
* Any unchecked exceptions from the action are passed through this API.
898-
*
899-
* @param <T> the return type of the action.
900-
* @param a a non-null action.
901-
* @return the result.
902-
* @throws UnsupportedOperationException if not allowed.
903-
* @throws UndeclaredThrowableException if a checked exception is thrown.
904-
* @since Angus Mail 2.0.3
905-
*/
906-
@SuppressWarnings("unchecked")
907-
private static <T> T invokeAccessController(final PrivilegedAction<T> a) {
908-
assert a != null;
909-
try {
910-
Class<?> c = Class.forName("java.security.AccessController");
911-
return (T) c.getMethod("doPrivileged", PrivilegedAction.class)
912-
.invoke((Object) null, a);
913-
} catch (ReflectiveOperationException roe) {
914-
Throwable cause = roe.getCause();
915-
if (cause instanceof RuntimeException) {
916-
throw (RuntimeException) cause;
917-
} else if (cause instanceof Error) {
918-
throw (Error) cause;
919-
} else {
920-
throw new UndeclaredThrowableException(roe);
921-
}
922-
}
836+
final ClassLoader[] loaders = new ClassLoader[2];
837+
loaders[0] = ClassLoader.getSystemClassLoader();
838+
loaders[1] = Thread.currentThread().getContextClassLoader();
839+
return loaders;
923840
}
924841

925842
/**

0 commit comments

Comments
 (0)