Skip to content

Commit b67563a

Browse files
committed
[ANT] Only attempt to set SecurityManager when it is supported
This avoids trowing+catching an UnsupportedOperationException and additional (error) logs if setting a SecurityManager is disallowed. The latter breaks ant-tests that check the log output. Part of eclipse-platform/eclipse.platform.releng.aggregator#2623
1 parent b946e83 commit b67563a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntRunner.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.tools.ant.Target;
5353
import org.apache.tools.ant.TaskAdapter;
5454
import org.apache.tools.ant.XmlLogger;
55+
import org.apache.tools.ant.util.JavaEnvUtils;
5556
import org.eclipse.ant.core.AntCorePlugin;
5657
import org.eclipse.ant.core.AntCorePreferences;
5758
import org.eclipse.ant.core.AntSecurityException;
@@ -82,6 +83,17 @@
8283
@SuppressWarnings("removal") // SecurityManager
8384
public class InternalAntRunner {
8485

86+
private static final boolean IS_SECURITY_MANAGER_SUPPORTED = isSecurityManagerAllowed();
87+
88+
private static boolean isSecurityManagerAllowed() {
89+
String sm = System.getProperty("java.security.manager"); //$NON-NLS-1$
90+
if (sm == null) { // default is 'disallow' since 18 and was 'allow' before
91+
return !JavaEnvUtils.isAtLeastJavaVersion("18"); //$NON-NLS-1$
92+
}
93+
// Value is either 'disallow' or 'allow' or specifies the SecurityManager class to set
94+
return !"disallow".equals(sm); //$NON-NLS-1$
95+
}
96+
8597
private IProgressMonitor monitor;
8698
private ArrayList<String> buildListeners;
8799
private String buildFileLocation;
@@ -695,12 +707,11 @@ private void run(List<String> argList) {
695707
if (extraArguments != null) {
696708
printArguments(getCurrentProject());
697709
}
698-
try {
710+
if (IS_SECURITY_MANAGER_SUPPORTED) {
711+
// TODO: call SecurityManagerUtil.isSecurityManagerAllowed() once it's more fine-grained,
712+
// i.e. once https://github.com/apache/ant/pull/216 is available.
699713
System.setSecurityManager(new AntSecurityManager(originalSM, Thread.currentThread()));
700714
}
701-
catch (UnsupportedOperationException ex) {
702-
AntCorePlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, 0, InternalAntMessages.InternalAntRunner_SecurityManagerError, ex));
703-
}
704715
if (targets == null) {
705716
targets = new Vector<>(1);
706717
}
@@ -1432,9 +1443,7 @@ protected void loadPropertyFiles() {
14321443
}
14331444
try {
14341445
List<Properties> allProperties = AntCoreUtil.loadPropertyFiles(propertyFiles, currentProject.getUserProperty("basedir"), getBuildFileLocation()); //$NON-NLS-1$
1435-
Iterator<Properties> iter = allProperties.iterator();
1436-
while (iter.hasNext()) {
1437-
Properties props = iter.next();
1446+
for (Properties props : allProperties) {
14381447
Enumeration<?> propertyNames = props.propertyNames();
14391448
while (propertyNames.hasMoreElements()) {
14401449
String name = (String) propertyNames.nextElement();

ant/org.eclipse.ant.tests.core/test plugin/org/eclipse/ant/tests/core/testplugin/AntTestChecker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.ant.tests.core.testplugin;
1515

1616
import java.util.ArrayList;
17+
import java.util.Collections;
1718
import java.util.Hashtable;
1819
import java.util.List;
1920

@@ -219,7 +220,7 @@ public String getUserProperty(String name) {
219220
}
220221

221222
public List<String> getMessages() {
222-
return messages;
223+
return Collections.unmodifiableList(messages);
223224
}
224225

225226
public List<String> getListeners() {

0 commit comments

Comments
 (0)