Skip to content

Commit

Permalink
[ANT] Only attempt to set SecurityManager when it is supported
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HannesWell committed Dec 15, 2024
1 parent 8b8afca commit 1bdcba1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.tools.ant.Target;
import org.apache.tools.ant.TaskAdapter;
import org.apache.tools.ant.XmlLogger;
import org.apache.tools.ant.util.JavaEnvUtils;
import org.eclipse.ant.core.AntCorePlugin;
import org.eclipse.ant.core.AntCorePreferences;
import org.eclipse.ant.core.AntSecurityException;
Expand Down Expand Up @@ -82,6 +83,17 @@
@SuppressWarnings("removal") // SecurityManager
public class InternalAntRunner {

private static final boolean IS_SECURITY_MANAGER_SUPPORTED = isSecurityManagerAllowed();

private static boolean isSecurityManagerAllowed() {
String sm = System.getProperty("java.security.manager"); //$NON-NLS-1$
if (sm == null) { // default is 'disallow' since 18 and was 'allow' before
return !JavaEnvUtils.isAtLeastJavaVersion("18"); //$NON-NLS-1$
}
// Value is either 'disallow' or 'allow' or specifies the SecurityManager class to set
return !"disallow".equals(sm); //$NON-NLS-1$
}

private IProgressMonitor monitor;
private ArrayList<String> buildListeners;
private String buildFileLocation;
Expand Down Expand Up @@ -695,12 +707,11 @@ private void run(List<String> argList) {
if (extraArguments != null) {
printArguments(getCurrentProject());
}
try {
if (IS_SECURITY_MANAGER_SUPPORTED) {
// TODO: call SecurityManagerUtil.isSecurityManagerAllowed() once it's more fine-grained,
// i.e. once https://github.com/apache/ant/pull/216 is available.
System.setSecurityManager(new AntSecurityManager(originalSM, Thread.currentThread()));
}
catch (UnsupportedOperationException ex) {
AntCorePlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, 0, InternalAntMessages.InternalAntRunner_SecurityManagerError, ex));
}
if (targets == null) {
targets = new Vector<>(1);
}
Expand Down Expand Up @@ -1432,9 +1443,7 @@ protected void loadPropertyFiles() {
}
try {
List<Properties> allProperties = AntCoreUtil.loadPropertyFiles(propertyFiles, currentProject.getUserProperty("basedir"), getBuildFileLocation()); //$NON-NLS-1$
Iterator<Properties> iter = allProperties.iterator();
while (iter.hasNext()) {
Properties props = iter.next();
for (Properties props : allProperties) {
Enumeration<?> propertyNames = props.propertyNames();
while (propertyNames.hasMoreElements()) {
String name = (String) propertyNames.nextElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.ant.tests.core.testplugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

Expand Down Expand Up @@ -219,7 +220,7 @@ public String getUserProperty(String name) {
}

public List<String> getMessages() {
return messages;
return Collections.unmodifiableList(messages);
}

public List<String> getListeners() {
Expand Down

0 comments on commit 1bdcba1

Please sign in to comment.