Skip to content

Commit

Permalink
Added a warning about Security Manager deprecation and handle excepti…
Browse files Browse the repository at this point in the history
…on in Java 17+ (closes #286)
  • Loading branch information
keeganwitt committed Feb 26, 2024
1 parent ad71ea1 commit e7afddb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e7afddb

Please sign in to comment.