From cdaee0f424d76595a5dd905d34aa4c6212c3fe3f 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 | 13 +++++++++++-- .../org/codehaus/gmavenplus/mojo/ExecuteMojo.java | 13 +++++++++++-- .../org/codehaus/gmavenplus/mojo/ShellMojo.java | 13 +++++++++++-- 3 files changed, 33 insertions(+), 6 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..5873bea3 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java @@ -89,7 +89,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { final SecurityManager sm = 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(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."); + } } } } 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..dad69d8c 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java @@ -140,7 +140,12 @@ protected synchronized void doExecute() throws MojoExecutionException { final SecurityManager sm = 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(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."); + } } } } 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..ab87a9e8 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java @@ -94,7 +94,12 @@ public void execute() throws MojoExecutionException { final SecurityManager sm = 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(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."); + } } } } else {