Skip to content

Commit

Permalink
Print a warning about SecurityManager being deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
keeganwitt committed Oct 23, 2023
1 parent 8e83de7 commit 5a972af
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 27 deletions.
55 changes: 46 additions & 9 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,53 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
}

if (allowSystemExits) {
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
// get classes we need with reflection
Class<?> consoleClass;
try {
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
} catch (ClassNotFoundException e) {
consoleClass = classWrangler.getClass("groovy.ui.Console");
}
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");

// create console to run
Object console = setupConsole(consoleClass, bindingClass);

// run the console
invokeMethod(findMethod(consoleClass, "run"), console);

// TODO: for some reason instantiating AntBuilder before calling run() causes its stdout and stderr streams to not be captured by the Console
bindAntBuilder(consoleClass, bindingClass, console);

// open script file
loadScript(consoleClass, console);

// wait for console to be closed
waitForConsoleClose();
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
throw new MojoExecutionException("Groovy 1.7.6 and 1.7.7 have a dependency on Ivy to run the console. Either change your Groovy version or add Ivy as a project or plugin dependency.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
}
} else {
final SecurityManager sm = System.getSecurityManager();
try {
getLog().warn("This feature relies on Java's SecurityManager, which is deprecated for removal in Java 17. Java 18 and later will require `-Djava.security.manager=allow` be used to continue using this feature.");
System.setSecurityManager(new NoExitSecurityManager());

// get classes we need with reflection
Class<?> consoleClass;
Expand Down Expand Up @@ -128,12 +169,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
System.setSecurityManager(sm);
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
}
}

Expand Down
37 changes: 28 additions & 9 deletions src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,35 @@ protected synchronized void doExecute() throws MojoExecutionException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
return;
}

if (allowSystemExits) {
try {
// get classes we need with reflection
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");

// create a GroovyShell to run scripts in
Object shell = setupShell(groovyShellClass);

// run the scripts
executeScripts(groovyShellClass, shell);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
}
} else {
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}
getLog().warn("This feature relies on Java's SecurityManager, which is deprecated for removal in Java 17. Java 18 and later will require `-Djava.security.manager=allow` be used to continue using this feature.");
System.setSecurityManager(new NoExitSecurityManager());

// get classes we need with reflection
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");
Expand All @@ -160,12 +183,8 @@ protected synchronized void doExecute() throws MojoExecutionException {
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
System.setSecurityManager(sm);
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
}
}

Expand Down
44 changes: 35 additions & 9 deletions src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,42 @@ public void execute() throws MojoExecutionException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a shell. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping shell startup.");
}

if (allowSystemExits) {
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
// get classes we need with reflection
Class<?> shellClass = classWrangler.getClass(groovyAtLeast(GROOVY_4_0_0_ALPHA1) ? "org.apache.groovy.groovysh.Groovysh" : "org.codehaus.groovy.tools.shell.Groovysh");
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
Class<?> ioClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO");
Class<?> verbosityClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO$Verbosity");
Class<?> loggerClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.util.Logger");

// create shell to run
Object shell = setupShell(shellClass, bindingClass, ioClass, verbosityClass, loggerClass);

// run the shell
invokeMethod(findMethod(shellClass, "run", String.class), shell, (String) null);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && e.getCause().getMessage() != null && e.getCause().getMessage().contains("jline")) {
throw new MojoExecutionException("Unable to get a JLine class from classpath. This might be because of a JLine version mismatch. If you are using Groovy < 2.2.0-beta-1, make sure you include JLine 1.0 as a runtime dependency in your project or the plugin.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
}
} else {
final SecurityManager sm = System.getSecurityManager();
try {
getLog().warn("This feature relies on Java's SecurityManager, which is deprecated for removal in Java 17. Java 18 and later will require `-Djava.security.manager=allow` be used to continue using this feature.");
System.setSecurityManager(new NoExitSecurityManager());

// get classes we need with reflection
Class<?> shellClass = classWrangler.getClass(groovyAtLeast(GROOVY_4_0_0_ALPHA1) ? "org.apache.groovy.groovysh.Groovysh" : "org.codehaus.groovy.tools.shell.Groovysh");
Expand All @@ -122,12 +152,8 @@ public void execute() throws MojoExecutionException {
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
System.setSecurityManager(sm);
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a shell. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping shell startup.");
}
}

Expand Down

0 comments on commit 5a972af

Please sign in to comment.