forked from groovy/GMavenPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[groovy#286] disable SecurityManager starting with Java 21
- Loading branch information
Showing
6 changed files
with
147 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/org/codehaus/gmavenplus/util/AbstractSecurityManagerSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.codehaus.gmavenplus.util; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
|
||
public abstract class AbstractSecurityManagerSetter implements SecurityManagerSetter{ | ||
|
||
private final boolean allowSystemExits; | ||
|
||
private final Log log; | ||
|
||
public AbstractSecurityManagerSetter(final Log log, final boolean allowSystemExits) { | ||
this.log = log; | ||
this.allowSystemExits = allowSystemExits; | ||
} | ||
|
||
protected boolean getAllowSystemExits() { | ||
return allowSystemExits; | ||
} | ||
|
||
protected Log getLog() { | ||
return log; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/codehaus/gmavenplus/util/DefaultSecurityManagerSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.codehaus.gmavenplus.util; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.apache.maven.plugin.logging.Log; | ||
|
||
public class DefaultSecurityManagerSetter extends AbstractSecurityManagerSetter { | ||
|
||
private final AtomicReference<SecurityManager> previousSecurityManager = new AtomicReference<>(); | ||
|
||
public DefaultSecurityManagerSetter(Log log, final boolean allowSystemExits) { | ||
super(log, allowSystemExits); | ||
} | ||
|
||
@Override | ||
public void setNoExitSecurityManager() { | ||
if (this.getAllowSystemExits()) { | ||
return; | ||
} | ||
|
||
this.previousSecurityManager.set(System.getSecurityManager()); | ||
|
||
getLog().warn("Setting a security manager is deprecated. Running this build with Java 21 or newer might result in different behaviour."); | ||
System.setSecurityManager(new NoExitSecurityManager()); | ||
} | ||
|
||
@Override | ||
public void revertToPreviousSecurityManager() { | ||
if (this.getAllowSystemExits()) { | ||
return; | ||
} | ||
|
||
this.getPreviousSecurityManager().getAndUpdate((previousSecurityManager -> { | ||
if (previousSecurityManager == null) { | ||
return null; | ||
} | ||
|
||
System.setSecurityManager(previousSecurityManager); | ||
|
||
return null; | ||
})); | ||
} | ||
|
||
protected AtomicReference<SecurityManager> getPreviousSecurityManager() { | ||
return previousSecurityManager; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/codehaus/gmavenplus/util/SecurityManagerSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.codehaus.gmavenplus.util; | ||
|
||
/** | ||
* Sets a custom security manager and returns the previous instance. | ||
* Can also revert to the previous security Manager. | ||
* <p> | ||
* Implementation notice: For Java 21 and above, | ||
* the implementation must be a no-op and issue a warning. | ||
* | ||
*/ | ||
public interface SecurityManagerSetter { | ||
|
||
/** | ||
* For Java until 20, this method should set the given Security manager if property {@code allowSystemExits} is set. | ||
*/ | ||
void setNoExitSecurityManager(); | ||
|
||
void revertToPreviousSecurityManager(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java21/org/codehaus/gmavenplus/util/DefaultSecurityManagerSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.codehaus.gmavenplus.util; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.apache.maven.plugin.logging.Log; | ||
|
||
public class DefaultSecurityManagerSetter extends AbstractSecurityManagerSetter { | ||
|
||
public DefaultSecurityManagerSetter(Log log, final boolean allowSystemExits) { | ||
super(log, allowSystemExits); | ||
} | ||
|
||
@Override | ||
public void setNoExitSecurityManager() { | ||
if (this.getAllowSystemExits()) { | ||
return; | ||
} | ||
|
||
getLog().warn("Setting a security manager is not supported starting with Java 21."); | ||
} | ||
|
||
@Override | ||
public void revertToPreviousSecurityManager() { | ||
if (this.getAllowSystemExits()) { | ||
return; | ||
} | ||
|
||
getLog().warn("Setting a security manager is not supported starting with Java 21."); | ||
|
||
} | ||
} |