Skip to content

Commit 6b53394

Browse files
committed
Print a warning about SecurityManager being deprecated
1 parent 8e83de7 commit 6b53394

File tree

3 files changed

+109
-105
lines changed

3 files changed

+109
-105
lines changed

src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -85,55 +85,56 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8585
getLog().debug("Unable to log project test classpath");
8686
}
8787

88-
if (groovyVersionSupportsAction()) {
89-
final SecurityManager sm = System.getSecurityManager();
90-
try {
91-
if (!allowSystemExits) {
92-
System.setSecurityManager(new NoExitSecurityManager());
93-
}
88+
if (!groovyVersionSupportsAction()) {
89+
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
90+
}
9491

95-
// get classes we need with reflection
96-
Class<?> consoleClass;
97-
try {
98-
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
99-
} catch (ClassNotFoundException e) {
100-
consoleClass = classWrangler.getClass("groovy.ui.Console");
101-
}
102-
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
92+
final SecurityManager sm = System.getSecurityManager();
93+
try {
94+
if (!allowSystemExits) {
95+
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.");
96+
System.setSecurityManager(new NoExitSecurityManager());
97+
}
98+
99+
// get classes we need with reflection
100+
Class<?> consoleClass;
101+
try {
102+
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
103+
} catch (ClassNotFoundException e) {
104+
consoleClass = classWrangler.getClass("groovy.ui.Console");
105+
}
106+
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
103107

104-
// create console to run
105-
Object console = setupConsole(consoleClass, bindingClass);
108+
// create console to run
109+
Object console = setupConsole(consoleClass, bindingClass);
106110

107-
// run the console
108-
invokeMethod(findMethod(consoleClass, "run"), console);
111+
// run the console
112+
invokeMethod(findMethod(consoleClass, "run"), console);
109113

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

113-
// open script file
114-
loadScript(consoleClass, console);
117+
// open script file
118+
loadScript(consoleClass, console);
115119

116-
// wait for console to be closed
117-
waitForConsoleClose();
118-
} catch (ClassNotFoundException e) {
119-
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);
120-
} catch (InvocationTargetException e) {
121-
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
122-
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);
123-
} else {
124-
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
125-
}
126-
} catch (IllegalAccessException e) {
127-
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
128-
} catch (InstantiationException e) {
129-
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
130-
} finally {
131-
if (!allowSystemExits) {
132-
System.setSecurityManager(sm);
133-
}
120+
// wait for console to be closed
121+
waitForConsoleClose();
122+
} catch (ClassNotFoundException e) {
123+
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);
124+
} catch (InvocationTargetException e) {
125+
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
126+
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);
127+
} else {
128+
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
129+
}
130+
} catch (IllegalAccessException e) {
131+
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
132+
} catch (InstantiationException e) {
133+
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
134+
} finally {
135+
if (!allowSystemExits) {
136+
System.setSecurityManager(sm);
134137
}
135-
} else {
136-
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
137138
}
138139
}
139140

src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -136,36 +136,38 @@ protected synchronized void doExecute() throws MojoExecutionException {
136136
getLog().debug("Unable to log project test classpath");
137137
}
138138

139-
if (groovyVersionSupportsAction()) {
140-
final SecurityManager sm = System.getSecurityManager();
141-
try {
142-
if (!allowSystemExits) {
143-
System.setSecurityManager(new NoExitSecurityManager());
144-
}
139+
if (!groovyVersionSupportsAction()) {
140+
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
141+
return;
142+
}
143+
144+
final SecurityManager sm = System.getSecurityManager();
145+
try {
146+
if (!allowSystemExits) {
147+
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.");
148+
System.setSecurityManager(new NoExitSecurityManager());
149+
}
145150

146-
// get classes we need with reflection
147-
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");
151+
// get classes we need with reflection
152+
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");
148153

149-
// create a GroovyShell to run scripts in
150-
Object shell = setupShell(groovyShellClass);
154+
// create a GroovyShell to run scripts in
155+
Object shell = setupShell(groovyShellClass);
151156

152-
// run the scripts
153-
executeScripts(groovyShellClass, shell);
154-
} catch (ClassNotFoundException e) {
155-
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);
156-
} catch (InvocationTargetException e) {
157-
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
158-
} catch (InstantiationException e) {
159-
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
160-
} catch (IllegalAccessException e) {
161-
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
162-
} finally {
163-
if (!allowSystemExits) {
164-
System.setSecurityManager(sm);
165-
}
157+
// run the scripts
158+
executeScripts(groovyShellClass, shell);
159+
} catch (ClassNotFoundException e) {
160+
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);
161+
} catch (InvocationTargetException e) {
162+
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
163+
} catch (InstantiationException e) {
164+
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
165+
} catch (IllegalAccessException e) {
166+
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
167+
} finally {
168+
if (!allowSystemExits) {
169+
System.setSecurityManager(sm);
166170
}
167-
} else {
168-
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
169171
}
170172
}
171173

src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,45 +90,46 @@ public void execute() throws MojoExecutionException {
9090
getLog().debug("Unable to log project test classpath");
9191
}
9292

93-
if (groovyVersionSupportsAction()) {
94-
final SecurityManager sm = System.getSecurityManager();
95-
try {
96-
if (!allowSystemExits) {
97-
System.setSecurityManager(new NoExitSecurityManager());
98-
}
99-
100-
// get classes we need with reflection
101-
Class<?> shellClass = classWrangler.getClass(groovyAtLeast(GROOVY_4_0_0_ALPHA1) ? "org.apache.groovy.groovysh.Groovysh" : "org.codehaus.groovy.tools.shell.Groovysh");
102-
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
103-
Class<?> ioClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO");
104-
Class<?> verbosityClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO$Verbosity");
105-
Class<?> loggerClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.util.Logger");
106-
107-
// create shell to run
108-
Object shell = setupShell(shellClass, bindingClass, ioClass, verbosityClass, loggerClass);
109-
110-
// run the shell
111-
invokeMethod(findMethod(shellClass, "run", String.class), shell, (String) null);
112-
} catch (ClassNotFoundException e) {
113-
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);
114-
} catch (InvocationTargetException e) {
115-
if (e.getCause() instanceof NoClassDefFoundError && e.getCause().getMessage() != null && e.getCause().getMessage().contains("jline")) {
116-
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);
117-
} else {
118-
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
119-
}
120-
} catch (IllegalAccessException e) {
121-
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
122-
} catch (InstantiationException e) {
123-
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
124-
} finally {
125-
if (!allowSystemExits) {
126-
System.setSecurityManager(sm);
127-
}
128-
}
129-
} else {
93+
if (!groovyVersionSupportsAction()) {
13094
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a shell. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping shell startup.");
13195
}
96+
97+
final SecurityManager sm = System.getSecurityManager();
98+
try {
99+
if (!allowSystemExits) {
100+
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.");
101+
System.setSecurityManager(new NoExitSecurityManager());
102+
}
103+
104+
// get classes we need with reflection
105+
Class<?> shellClass = classWrangler.getClass(groovyAtLeast(GROOVY_4_0_0_ALPHA1) ? "org.apache.groovy.groovysh.Groovysh" : "org.codehaus.groovy.tools.shell.Groovysh");
106+
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
107+
Class<?> ioClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO");
108+
Class<?> verbosityClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO$Verbosity");
109+
Class<?> loggerClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.util.Logger");
110+
111+
// create shell to run
112+
Object shell = setupShell(shellClass, bindingClass, ioClass, verbosityClass, loggerClass);
113+
114+
// run the shell
115+
invokeMethod(findMethod(shellClass, "run", String.class), shell, (String) null);
116+
} catch (ClassNotFoundException e) {
117+
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);
118+
} catch (InvocationTargetException e) {
119+
if (e.getCause() instanceof NoClassDefFoundError && e.getCause().getMessage() != null && e.getCause().getMessage().contains("jline")) {
120+
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);
121+
} else {
122+
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
123+
}
124+
} catch (IllegalAccessException e) {
125+
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
126+
} catch (InstantiationException e) {
127+
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
128+
} finally {
129+
if (!allowSystemExits) {
130+
System.setSecurityManager(sm);
131+
}
132+
}
132133
}
133134

134135
/**

0 commit comments

Comments
 (0)