Repository: groovy Updated Branches: refs/heads/master 2532d29e8 -> 341f0ac19
Create classloader inside doPrivileged block Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/341f0ac1 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/341f0ac1 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/341f0ac1 Branch: refs/heads/master Commit: 341f0ac199ec92c9fd4689a7ca01f751c6aeafae Parents: 2532d29 Author: sunlan <[email protected]> Authored: Tue Dec 5 10:58:17 2017 +0800 Committer: sunlan <[email protected]> Committed: Tue Dec 5 10:58:17 2017 +0800 ---------------------------------------------------------------------- .../groovy/security/SecurityTestSupport.java | 24 ++++++++++----- .../codehaus/groovy/classgen/TestSupport.java | 26 +++++++++------- .../codehaus/groovy/ant/CompileTaskSupport.java | 12 ++++++-- .../java/org/codehaus/groovy/ant/Groovy.java | 11 ++++++- .../java/org/codehaus/groovy/ant/Groovyc.java | 24 ++++++++++++--- .../groovy/jsr223/GroovyScriptEngineImpl.java | 9 +++++- .../text/markup/MarkupTemplateEngine.java | 12 ++++++-- .../src/main/java/groovy/util/AllTestSuite.java | 12 +++++++- .../main/java/groovy/util/GroovyTestSuite.java | 12 +++++++- .../apache/groovy/parser/AbstractParser.java | 31 ++++++++++++++++++-- 10 files changed, 142 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/src/test/groovy/security/SecurityTestSupport.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/security/SecurityTestSupport.java b/src/test/groovy/security/SecurityTestSupport.java index 5a161e8..b08342c 100644 --- a/src/test/groovy/security/SecurityTestSupport.java +++ b/src/test/groovy/security/SecurityTestSupport.java @@ -31,9 +31,13 @@ import junit.textui.ResultPrinter; import org.codehaus.groovy.runtime.InvokerHelper; import java.io.File; -import java.io.PrintStream; import java.io.IOException; -import java.security.*; +import java.io.PrintStream; +import java.security.AccessControlException; +import java.security.AccessController; +import java.security.Permission; +import java.security.Policy; +import java.security.PrivilegedAction; import java.util.Enumeration; /** @@ -88,11 +92,15 @@ public abstract class SecurityTestSupport extends GroovyTestCase { } } - protected GroovyClassLoader loader = (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return new GroovyClassLoader(SecurityTestSupport.class.getClassLoader()); - } - }); + protected GroovyClassLoader loader = + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(SecurityTestSupport.class.getClassLoader()); + } + } + ); private SecurityManager securityManager; private ClassLoader currentClassLoader; @@ -131,6 +139,7 @@ public abstract class SecurityTestSupport extends GroovyTestCase { } currentClassLoader = Thread.currentThread().getContextClassLoader(); AccessController.doPrivileged(new PrivilegedAction() { + @Override public Object run() { Thread.currentThread().setContextClassLoader(loader); return null; @@ -140,6 +149,7 @@ public abstract class SecurityTestSupport extends GroovyTestCase { protected void tearDown() { AccessController.doPrivileged(new PrivilegedAction() { + @Override public Object run() { System.setSecurityManager(securityManager); Thread.currentThread().setContextClassLoader(currentClassLoader); http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/src/test/org/codehaus/groovy/classgen/TestSupport.java ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/classgen/TestSupport.java b/src/test/org/codehaus/groovy/classgen/TestSupport.java index 579562d..46cb4c7 100644 --- a/src/test/org/codehaus/groovy/classgen/TestSupport.java +++ b/src/test/org/codehaus/groovy/classgen/TestSupport.java @@ -56,11 +56,14 @@ public class TestSupport extends GroovyTestCase implements Opcodes { // ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader parentLoader = getClass().getClassLoader(); protected final GroovyClassLoader loader = - (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return new GroovyClassLoader(parentLoader); - } - }); + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(parentLoader); + } + } + ); final CompileUnit unit = new CompileUnit(loader, new CompilerConfiguration()); final ModuleNode module = new ModuleNode(unit); @@ -147,11 +150,14 @@ public class TestSupport extends GroovyTestCase implements Opcodes { protected void assertScript(final String text, final String scriptName) throws Exception { log.info("About to execute script"); log.info(text); - GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return new GroovyCodeSource(text, scriptName, "/groovy/testSupport"); - } - }); + GroovyCodeSource gcs = AccessController.doPrivileged( + new PrivilegedAction<GroovyCodeSource>() { + @Override + public GroovyCodeSource run() { + return new GroovyCodeSource(text, scriptName, "/groovy/testSupport"); + } + } + ); Class groovyClass = loader.parseClass(gcs); Script script = InvokerHelper.createScript(groovyClass, new Binding()); script.run(); http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/CompileTaskSupport.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/CompileTaskSupport.java b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/CompileTaskSupport.java index 65177a1..cf80813 100644 --- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/CompileTaskSupport.java +++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/CompileTaskSupport.java @@ -29,6 +29,8 @@ import org.codehaus.groovy.tools.ErrorReporter; import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; +import java.security.AccessController; +import java.security.PrivilegedAction; /** * Support for compilation related tasks. @@ -134,8 +136,14 @@ public abstract class CompileTaskSupport } protected GroovyClassLoader createClassLoader() { - ClassLoader parent = ClassLoader.getSystemClassLoader(); - GroovyClassLoader gcl = new GroovyClassLoader(parent, config); + GroovyClassLoader gcl = + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(ClassLoader.getSystemClassLoader(), config); + } + }); Path path = getClasspath(); if (path != null) { http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java index 8973d85..38ac0b3 100644 --- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java +++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java @@ -50,6 +50,8 @@ import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.lang.reflect.Field; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Vector; /** @@ -446,7 +448,14 @@ public class Groovy extends Java { } final String scriptName = computeScriptName(); - final GroovyClassLoader classLoader = new GroovyClassLoader(baseClassLoader); + final GroovyClassLoader classLoader = + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(baseClassLoader); + } + }); addClassPathes(classLoader); configureCompiler(); final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration); http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java index cf6408c..4f0751c 100644 --- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java +++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java @@ -53,6 +53,8 @@ import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Enumeration; import java.util.LinkedHashSet; @@ -1299,9 +1301,16 @@ public class Groovyc extends MatchingTask { if (!fork && !getIncludeantruntime()) { throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false"); } - ClassLoader parent = getIncludeantruntime() - ? getClass().getClassLoader() - : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath()); + final ClassLoader parent = + AccessController.doPrivileged( + new PrivilegedAction<ClassLoader>() { + @Override + public ClassLoader run() { + return getIncludeantruntime() + ? getClass().getClassLoader() + : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath()); + } + }); if (parent instanceof AntClassLoader) { AntClassLoader antLoader = (AntClassLoader) parent; String[] pathElm = antLoader.getClasspath().split(File.pathSeparator); @@ -1334,7 +1343,14 @@ public class Groovyc extends MatchingTask { } } - GroovyClassLoader loader = new GroovyClassLoader(parent, configuration); + GroovyClassLoader loader = + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(parent, configuration); + } + }); if (!forceLookupUnnamedFiles) { // in normal case we don't need to do script lookups loader.setResourceLoader(new GroovyResourceLoader() { http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java index 651f2fa..03fc49e 100644 --- a/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java +++ b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java @@ -79,6 +79,8 @@ import java.io.Writer; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.security.AccessController; +import java.security.PrivilegedAction; /** * JSR-223 Engine implementation. @@ -111,7 +113,12 @@ public class GroovyScriptEngineImpl extends AbstractScriptEngine implements Comp } public GroovyScriptEngineImpl() { - this(new GroovyClassLoader(getParentLoader(), new CompilerConfiguration(CompilerConfiguration.DEFAULT))); + this(AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(getParentLoader(), new CompilerConfiguration(CompilerConfiguration.DEFAULT)); + } + })); } public GroovyScriptEngineImpl(GroovyClassLoader classLoader) { http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateEngine.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateEngine.java b/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateEngine.java index 422ea20..d9118f5 100644 --- a/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateEngine.java +++ b/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateEngine.java @@ -121,8 +121,16 @@ public class MarkupTemplateEngine extends TemplateEngine { * @param templateDirectory directory where to find templates * @param tplConfig template engine configuration */ - public MarkupTemplateEngine(ClassLoader parentLoader, File templateDirectory, TemplateConfiguration tplConfig) { - this(new URLClassLoader(buildURLs(templateDirectory), parentLoader), tplConfig, null); + public MarkupTemplateEngine(final ClassLoader parentLoader, final File templateDirectory, TemplateConfiguration tplConfig) { + this(AccessController.doPrivileged( + new PrivilegedAction<URLClassLoader>() { + @Override + public URLClassLoader run() { + return new URLClassLoader(buildURLs(templateDirectory), parentLoader); + } + }), + tplConfig, + null); } private static URL[] buildURLs(final File templateDirectory) { http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-test/src/main/java/groovy/util/AllTestSuite.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-test/src/main/java/groovy/util/AllTestSuite.java b/subprojects/groovy-test/src/main/java/groovy/util/AllTestSuite.java index be52736..216fe8a 100644 --- a/subprojects/groovy-test/src/main/java/groovy/util/AllTestSuite.java +++ b/subprojects/groovy-test/src/main/java/groovy/util/AllTestSuite.java @@ -28,6 +28,8 @@ import org.codehaus.groovy.runtime.ScriptTestAdapter; import java.io.File; import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.List; import java.util.logging.Logger; @@ -78,7 +80,15 @@ public class AllTestSuite extends TestSuite { private static final Logger LOG = Logger.getLogger(AllTestSuite.class.getName()); private static final ClassLoader JAVA_LOADER = AllTestSuite.class.getClassLoader(); - private static final GroovyClassLoader GROOVY_LOADER = new GroovyClassLoader(JAVA_LOADER); + private static final GroovyClassLoader GROOVY_LOADER = + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(JAVA_LOADER); + } + } + ); private static final String[] EMPTY_ARGS = new String[]{}; private static IFileNameFinder finder = null; http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/groovy-test/src/main/java/groovy/util/GroovyTestSuite.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-test/src/main/java/groovy/util/GroovyTestSuite.java b/subprojects/groovy-test/src/main/java/groovy/util/GroovyTestSuite.java index 7655f03..4c7dce7 100644 --- a/subprojects/groovy-test/src/main/java/groovy/util/GroovyTestSuite.java +++ b/subprojects/groovy-test/src/main/java/groovy/util/GroovyTestSuite.java @@ -26,6 +26,8 @@ import junit.textui.TestRunner; import org.codehaus.groovy.runtime.ScriptTestAdapter; import java.io.File; +import java.security.AccessController; +import java.security.PrivilegedAction; /** @@ -50,7 +52,15 @@ public class GroovyTestSuite extends TestSuite { protected static String file = null; - protected final GroovyClassLoader loader = new GroovyClassLoader(GroovyTestSuite.class.getClassLoader()); + protected final GroovyClassLoader loader = + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(GroovyTestSuite.class.getClassLoader()); + } + } + ); public static void main(String[] args) { if (args.length > 0) { http://git-wip-us.apache.org/repos/asf/groovy/blob/341f0ac1/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/AbstractParser.java ---------------------------------------------------------------------- diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/AbstractParser.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/AbstractParser.java index f216797..5575f0f 100644 --- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/AbstractParser.java +++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/AbstractParser.java @@ -25,6 +25,8 @@ import org.codehaus.groovy.control.ErrorCollector; import org.codehaus.groovy.control.SourceUnit; import java.io.File; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.logging.Level; import java.util.logging.Logger; @@ -44,7 +46,19 @@ public abstract class AbstractParser { } CompilerConfiguration configuration = this.getCompilerConfiguration(); - SourceUnit sourceUnit = new SourceUnit(file, configuration, new GroovyClassLoader(), new ErrorCollector(configuration)); + SourceUnit sourceUnit = + new SourceUnit( + file, + configuration, + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(); + } + }), + new ErrorCollector(configuration) + ); return this.parse(sourceUnit); } @@ -59,7 +73,20 @@ public abstract class AbstractParser { } CompilerConfiguration configuration = this.getCompilerConfiguration(); - SourceUnit sourceUnit = new SourceUnit(name, text, configuration, new GroovyClassLoader(), new ErrorCollector(configuration)); + SourceUnit sourceUnit = + new SourceUnit( + name, + text, + configuration, + AccessController.doPrivileged( + new PrivilegedAction<GroovyClassLoader>() { + @Override + public GroovyClassLoader run() { + return new GroovyClassLoader(); + } + }), + new ErrorCollector(configuration) + ); return this.parse(sourceUnit); }
