From e7afddbbf80da4645f529c245db26800ab526fea Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Mon, 26 Feb 2024 15:23:45 -0500 Subject: [PATCH] Added a warning about Security Manager deprecation and handle exception in Java 17+ (closes #286) --- .../org/codehaus/gmavenplus/mojo/ConsoleMojo.java | 15 ++++++++++++--- .../org/codehaus/gmavenplus/mojo/ExecuteMojo.java | 15 ++++++++++++--- .../org/codehaus/gmavenplus/mojo/ShellMojo.java | 15 ++++++++++++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java index 49da7cf0..67d4f096 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java @@ -86,10 +86,15 @@ public void execute() throws MojoExecutionException, MojoFailureException { } if (groovyVersionSupportsAction()) { - final SecurityManager sm = System.getSecurityManager(); + final SecurityManager defaultSecurityManager = System.getSecurityManager(); try { if (!allowSystemExits) { - System.setSecurityManager(new NoExitSecurityManager()); + getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal."); + try { + System.setSecurityManager(new NoExitSecurityManager()); + } catch (UnsupportedOperationException e) { + getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=disallow` to override this."); + } } // get classes we need with reflection @@ -129,7 +134,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e); } finally { if (!allowSystemExits) { - System.setSecurityManager(sm); + try { + System.setSecurityManager(defaultSecurityManager); + } catch (UnsupportedOperationException e) { + getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=disallow` to override this."); + } } } } else { diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java index 976b2618..8052c46c 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java @@ -137,10 +137,15 @@ protected synchronized void doExecute() throws MojoExecutionException { } if (groovyVersionSupportsAction()) { - final SecurityManager sm = System.getSecurityManager(); + final SecurityManager defaultSecurityManager = System.getSecurityManager(); try { if (!allowSystemExits) { - System.setSecurityManager(new NoExitSecurityManager()); + getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal."); + try { + System.setSecurityManager(new NoExitSecurityManager()); + } catch (UnsupportedOperationException e) { + getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=disallow` to override this."); + } } // get classes we need with reflection @@ -161,7 +166,11 @@ protected synchronized void doExecute() throws MojoExecutionException { throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e); } finally { if (!allowSystemExits) { - System.setSecurityManager(sm); + try { + System.setSecurityManager(defaultSecurityManager); + } catch (UnsupportedOperationException e) { + getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=disallow` to override this."); + } } } } else { diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java index 112bf639..09941193 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java @@ -91,10 +91,15 @@ public void execute() throws MojoExecutionException { } if (groovyVersionSupportsAction()) { - final SecurityManager sm = System.getSecurityManager(); + final SecurityManager defaultSecurityManager = System.getSecurityManager(); try { if (!allowSystemExits) { - System.setSecurityManager(new NoExitSecurityManager()); + getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal."); + try { + System.setSecurityManager(new NoExitSecurityManager()); + } catch (UnsupportedOperationException e) { + getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=disallow` to override this."); + } } // get classes we need with reflection @@ -123,7 +128,11 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e); } finally { if (!allowSystemExits) { - System.setSecurityManager(sm); + try { + System.setSecurityManager(defaultSecurityManager); + } catch (UnsupportedOperationException e) { + getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=disallow` to override this."); + } } } } else {