Search in sources :

Example 16 with CodeSource

use of java.security.CodeSource in project ratpack by ratpack.

the class ScriptEngine method createClassLoader.

private GroovyClassLoader createClassLoader(final Path scriptPath) {
    final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    if (!scriptBaseClass.equals(Script.class)) {
        compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
    }
    compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (staticCompile) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
            classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
            if (scriptPath != null) {
                AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
                scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
                classNode.addAnnotation(scriptPathAnnotation);
            }
        }
    });
    return new GroovyClassLoader(parentLoader, compilerConfiguration) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
            return new CompilationUnit(config, source, this) {

                {
                    verifier = new Verifier() {

                        @Override
                        public void visitClass(ClassNode node) {
                            if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
                                AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
                                lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
                                node.addAnnotation(lineNumberAnnotation);
                            }
                            super.visitClass(node);
                        }
                    };
                }
            };
        }
    };
}
Also used : Script(groovy.lang.Script) ClassNode(org.codehaus.groovy.ast.ClassNode) InheritConstructors(groovy.transform.InheritConstructors) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) CodeSource(java.security.CodeSource) Verifier(org.codehaus.groovy.classgen.Verifier) GroovyClassLoader(groovy.lang.GroovyClassLoader) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Example 17 with CodeSource

use of java.security.CodeSource in project robovm by robovm.

the class SecureClassLoaderTest method testGetPermissions.

public void testGetPermissions() throws Exception {
    URL url = new URL("http://localhost");
    CodeSource cs = new CodeSource(url, (Certificate[]) null);
    MyClassLoader ldr = new MyClassLoader();
    ldr.getPerms(null);
    ldr.getPerms(cs);
}
Also used : CodeSource(java.security.CodeSource) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Example 18 with CodeSource

use of java.security.CodeSource in project spring-boot by spring-projects.

the class ApplicationHome method findSource.

private File findSource(Class<?> sourceClass) {
    try {
        ProtectionDomain domain = (sourceClass == null ? null : sourceClass.getProtectionDomain());
        CodeSource codeSource = (domain == null ? null : domain.getCodeSource());
        URL location = (codeSource == null ? null : codeSource.getLocation());
        File source = (location == null ? null : findSource(location));
        if (source != null && source.exists() && !isUnitTest()) {
            return source.getAbsoluteFile();
        }
        return null;
    } catch (Exception ex) {
        return null;
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException)

Example 19 with CodeSource

use of java.security.CodeSource in project gradle by gradle.

the class ClasspathUtil method getClasspathForClass.

public static File getClasspathForClass(Class<?> targetClass) {
    URI location;
    try {
        CodeSource codeSource = targetClass.getProtectionDomain().getCodeSource();
        if (codeSource != null && codeSource.getLocation() != null) {
            location = toURI(codeSource.getLocation());
            if (location.getScheme().equals("file")) {
                return new File(location);
            }
        }
        if (targetClass.getClassLoader() != null) {
            String resourceName = targetClass.getName().replace('.', '/') + ".class";
            URL resource = targetClass.getClassLoader().getResource(resourceName);
            if (resource != null) {
                return getClasspathForResource(resource, resourceName);
            }
        }
        throw new GradleException(String.format("Cannot determine classpath for class %s.", targetClass.getName()));
    } catch (URISyntaxException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : GradleException(org.gradle.api.GradleException) URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URI(java.net.URI) File(java.io.File) URL(java.net.URL)

Example 20 with CodeSource

use of java.security.CodeSource in project gradle by gradle.

the class TransformingClassLoader method findClass.

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    if (!shouldTransform(name)) {
        return super.findClass(name);
    }
    String resourceName = name.replace('.', '/') + ".class";
    URL resource = findResource(resourceName);
    byte[] bytes;
    CodeSource codeSource;
    try {
        if (resource != null) {
            bytes = loadBytecode(resource);
            bytes = transform(name, bytes);
            URL codeBase = ClasspathUtil.getClasspathForResource(resource, resourceName).toURI().toURL();
            codeSource = new CodeSource(codeBase, (Certificate[]) null);
        } else {
            bytes = generateMissingClass(name);
            codeSource = null;
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not load class '%s' from %s.", name, resource), e);
    }
    if (bytes == null) {
        throw new ClassNotFoundException(name);
    }
    String packageName = StringUtils.substringBeforeLast(name, ".");
    Package p = getPackage(packageName);
    if (p == null) {
        definePackage(packageName, null, null, null, null, null, null, null);
    }
    return defineClass(name, bytes, 0, bytes.length, codeSource);
}
Also used : GradleException(org.gradle.api.GradleException) CodeSource(java.security.CodeSource) URL(java.net.URL) GradleException(org.gradle.api.GradleException) IOException(java.io.IOException)

Aggregations

CodeSource (java.security.CodeSource)85 URL (java.net.URL)43 ProtectionDomain (java.security.ProtectionDomain)29 File (java.io.File)24 IOException (java.io.IOException)16 Certificate (java.security.cert.Certificate)14 URISyntaxException (java.net.URISyntaxException)10 Permissions (java.security.Permissions)10 Policy (java.security.Policy)10 JarFile (java.util.jar.JarFile)10 PermissionCollection (java.security.PermissionCollection)9 URI (java.net.URI)8 FilePermission (java.io.FilePermission)7 MalformedURLException (java.net.MalformedURLException)5 AccessControlContext (java.security.AccessControlContext)5 URLClassLoader (java.net.URLClassLoader)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 JarURLConnection (java.net.JarURLConnection)3 SocketPermission (java.net.SocketPermission)3 Path (java.nio.file.Path)3