Search in sources :

Example 36 with CodeSource

use of java.security.CodeSource in project streamsx.topology by IBMStreams.

the class DependencyResolver method addClassDependency.

public void addClassDependency(Class<?> clazz) {
    CodeSource source = clazz.getProtectionDomain().getCodeSource();
    if (source == null)
        return;
    Path absolutePath = null;
    try {
        absolutePath = Paths.get(source.getLocation().toURI()).toAbsolutePath();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    globalDependencies.add(absolutePath);
}
Also used : Path(java.nio.file.Path) URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource)

Example 37 with CodeSource

use of java.security.CodeSource in project jdk8u_jdk by JetBrains.

the class FactoryURLClassLoader method defineClass.

/*
     * Defines a Class using the class bytes obtained from the specified
     * Resource. The resulting Class must be resolved before it can be
     * used.
     */
private Class<?> defineClass(String name, Resource res) throws IOException {
    long t0 = System.nanoTime();
    int i = name.lastIndexOf('.');
    URL url = res.getCodeSourceURL();
    if (i != -1) {
        String pkgname = name.substring(0, i);
        // Check if package already loaded.
        Manifest man = res.getManifest();
        definePackageInternal(pkgname, man, url);
    }
    // Now read the class bytes and define the class
    java.nio.ByteBuffer bb = res.getByteBuffer();
    if (bb != null) {
        // Use (direct) ByteBuffer:
        CodeSigner[] signers = res.getCodeSigners();
        CodeSource cs = new CodeSource(url, signers);
        sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
        return defineClass(name, bb, cs);
    } else {
        byte[] b = res.getBytes();
        // must read certificates AFTER reading bytes.
        CodeSigner[] signers = res.getCodeSigners();
        CodeSource cs = new CodeSource(url, signers);
        sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
        return defineClass(name, b, 0, b.length, cs);
    }
}
Also used : Manifest(java.util.jar.Manifest) CodeSource(java.security.CodeSource) CodeSigner(java.security.CodeSigner)

Example 38 with CodeSource

use of java.security.CodeSource in project jdk8u_jdk by JetBrains.

the class MethodUtil method defineClass.

/*
     * Define the proxy classes
     */
private Class<?> defineClass(String name, URL url) throws IOException {
    byte[] b = getBytes(url);
    CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[]) null);
    if (!name.equals(TRAMPOLINE)) {
        throw new IOException("MethodUtil: bad name " + name);
    }
    return defineClass(name, b, 0, b.length, cs);
}
Also used : IOException(java.io.IOException) CodeSource(java.security.CodeSource)

Example 39 with CodeSource

use of java.security.CodeSource in project jdk8u_jdk by JetBrains.

the class LoaderHandler method getLoaderAccessControlContext.

/**
     * Return the access control context that a loader for the given
     * codebase URL path should execute with.
     */
private static AccessControlContext getLoaderAccessControlContext(URL[] urls) {
    /*
         * The approach used here is taken from the similar method
         * getAccessControlContext() in the sun.applet.AppletPanel class.
         */
    // begin with permissions granted to all code in current policy
    PermissionCollection perms = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<PermissionCollection>() {

        public PermissionCollection run() {
            CodeSource codesource = new CodeSource(null, (java.security.cert.Certificate[]) null);
            Policy p = java.security.Policy.getPolicy();
            if (p != null) {
                return p.getPermissions(codesource);
            } else {
                return new Permissions();
            }
        }
    });
    // createClassLoader permission needed to create loader in context
    perms.add(new RuntimePermission("createClassLoader"));
    // add permissions to read any "java.*" property
    perms.add(new java.util.PropertyPermission("java.*", "read"));
    // add permissions reuiqred to load from codebase URL path
    addPermissionsForURLs(urls, perms, true);
    /*
         * Create an AccessControlContext that consists of a single
         * protection domain with only the permissions calculated above.
         */
    ProtectionDomain pd = new ProtectionDomain(new CodeSource((urls.length > 0 ? urls[0] : null), (java.security.cert.Certificate[]) null), perms);
    return new AccessControlContext(new ProtectionDomain[] { pd });
}
Also used : Policy(java.security.Policy) PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource) AccessControlContext(java.security.AccessControlContext) Permissions(java.security.Permissions)

Example 40 with CodeSource

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

the class FileUtilSupport method getJmriJarFile.

/**
     * Get the JMRI distribution jar file.
     *
     * @return the JAR file containing the JMRI library or null if not running
     *         from a JAR file
     */
public JarFile getJmriJarFile() {
    if (jarPath == null) {
        CodeSource sc = FileUtilSupport.class.getProtectionDomain().getCodeSource();
        if (sc != null) {
            jarPath = sc.getLocation().toString();
            if (jarPath.startsWith("jar:file:")) {
                // 9 = length of jar:file:
                jarPath = jarPath.substring(9, jarPath.lastIndexOf("!"));
            } else {
                log.info("Running from classes not in jar file.");
                // set to empty String to bypass search
                jarPath = "";
                return null;
            }
            log.debug("jmri.jar path is {}", jarPath);
        }
        if (jarPath == null) {
            log.error("Unable to locate jmri.jar");
            // set to empty String to bypass search
            jarPath = "";
            return null;
        }
    }
    if (!jarPath.isEmpty()) {
        try {
            return new JarFile(jarPath);
        } catch (IOException ex) {
            log.error("Unable to open jmri.jar", ex);
            return null;
        }
    }
    return null;
}
Also used : IOException(java.io.IOException) CodeSource(java.security.CodeSource) JarFile(java.util.jar.JarFile)

Aggregations

CodeSource (java.security.CodeSource)104 URL (java.net.URL)49 ProtectionDomain (java.security.ProtectionDomain)39 File (java.io.File)30 IOException (java.io.IOException)20 Certificate (java.security.cert.Certificate)17 JarFile (java.util.jar.JarFile)13 PermissionCollection (java.security.PermissionCollection)12 URI (java.net.URI)11 URISyntaxException (java.net.URISyntaxException)11 Permissions (java.security.Permissions)11 Policy (java.security.Policy)10 FilePermission (java.io.FilePermission)7 InputStream (java.io.InputStream)6 AccessControlContext (java.security.AccessControlContext)6 MalformedURLException (java.net.MalformedURLException)5 Permission (java.security.Permission)4 JarEntry (java.util.jar.JarEntry)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 URLClassLoader (java.net.URLClassLoader)3