protected PermissionCollection getPermissions(CodeSource codeSource) { PermissionCollection perms; try { try { perms = super.getPermissions(codeSource); } catch (SecurityException e) { // We lied about our CodeSource and that makes URLClassLoader unhappy. perms = new Permissions(); } ProtectionDomain myDomain = AccessController.doPrivileged( new PrivilegedAction<ProtectionDomain>() { public ProtectionDomain run() { return getClass().getProtectionDomain(); } }); PermissionCollection myPerms = myDomain.getPermissions(); if (myPerms != null) { for (Enumeration<Permission> elements = myPerms.elements(); elements.hasMoreElements(); ) { perms.add(elements.nextElement()); } } } catch (Throwable e) { // We lied about our CodeSource and that makes URLClassLoader unhappy. perms = new Permissions(); } perms.setReadOnly(); return perms; }
/** * @deprecated Prefer using methods taking a Reader rather than an InputStream to avoid wrong * encoding issues. */ public Class parseClass(final InputStream in, final String fileName) throws CompilationFailedException { // For generic input streams, provide a catch-all codebase of GroovyScript // Security for these classes can be administered via policy grants with // a codebase of file:groovy.script GroovyCodeSource gcs = AccessController.doPrivileged( new PrivilegedAction<GroovyCodeSource>() { public GroovyCodeSource run() { try { String scriptText = config.getSourceEncoding() != null ? IOGroovyMethods.getText(in, config.getSourceEncoding()) : IOGroovyMethods.getText(in); return new GroovyCodeSource(scriptText, fileName, "/groovy/script"); } catch (IOException e) { throw new RuntimeException( "Impossible to read the content of the input stream for file named: " + fileName, e); } } }); return parseClass(gcs); }
/** * creates a ClassCollector for a new compilation. * * @param unit the compilationUnit * @param su the SourceUnit * @return the ClassCollector */ protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su) { InnerLoader loader = AccessController.doPrivileged( new PrivilegedAction<InnerLoader>() { public InnerLoader run() { return new InnerLoader(GroovyClassLoader.this); } }); return new ClassCollector(loader, unit, su); }
/** * Parses the given text into a Java class capable of being run * * @param text the text of the script/class to parse * @param fileName the file name to use as the name of the class * @return the main class defined in the given script */ public Class parseClass(final String text, final String fileName) throws CompilationFailedException { GroovyCodeSource gcs = AccessController.doPrivileged( new PrivilegedAction<GroovyCodeSource>() { public GroovyCodeSource run() { return new GroovyCodeSource(text, fileName, "/groovy/script"); } }); gcs.setCachable(false); return parseClass(gcs); }
public URL loadGroovySource(final String filename) throws MalformedURLException { return AccessController.doPrivileged( new PrivilegedAction<URL>() { public URL run() { for (String extension : config.getScriptExtensions()) { try { URL ret = getSourceFile(filename, extension); if (ret != null) return ret; } catch (Throwable t) { // } } return null; } }); }
/** * adds a classpath to this classloader. * * @param path is a jar file or a directory. * @see #addURL(URL) */ public void addClasspath(final String path) { AccessController.doPrivileged( new PrivilegedAction<Void>() { public Void run() { try { // As the java.net.URL Javadoc says, the recommended way to get a URL is via URI. // http://docs.oracle.com/javase/7/docs/api/java/net/URL.html // "Note, the URI class does perform escaping of its component fields in certain // circumstances. // The recommended way to manage the encoding and decoding of URLs is to use URI, and // to convert // between these two classes using toURI() and URI.toURL()." // A possibly better approach here is to construct a URI and then resolve it against // a URI for the current working directory. // But we use this string match for now so everyone can see it doesn't hurt file-only // classpaths. URI newURI; if (!URI_PATTERN.matcher(path).matches()) { newURI = new File(path).toURI(); } else { newURI = new URI(path); } URL[] urls = getURLs(); for (URL url : urls) { // Do not use URL.equals. It uses the network to resolve names and compares ip // addresses! // That is a violation of RFC and just plain evil. // http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html // http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#equals(java.lang.Object) // "Since hosts comparison requires name resolution, this operation is a blocking // operation. // Note: The defined behavior for equals is known to be inconsistent with virtual // hosting in HTTP." if (newURI.equals(url.toURI())) return null; } addURL(newURI.toURL()); } catch (MalformedURLException e) { // TODO: fail through ? } catch (URISyntaxException e) { // Just doing the same thing... } return null; } }); }