Search in sources :

Example 61 with CodeSource

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

the class LogbackLoggingSystem method getLocation.

private Object getLocation(ILoggerFactory factory) {
    try {
        ProtectionDomain protectionDomain = factory.getClass().getProtectionDomain();
        CodeSource codeSource = protectionDomain.getCodeSource();
        if (codeSource != null) {
            return codeSource.getLocation();
        }
    } catch (SecurityException ex) {
    // Unable to determine location
    }
    return "unknown location";
}
Also used : ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource)

Example 62 with CodeSource

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

the class Launcher method createArchive.

protected final Archive createArchive() throws Exception {
    ProtectionDomain protectionDomain = getClass().getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
    String path = (location == null ? null : location.getSchemeSpecificPart());
    if (path == null) {
        throw new IllegalStateException("Unable to determine code source archive");
    }
    File root = new File(path);
    if (!root.exists()) {
        throw new IllegalStateException("Unable to determine code source archive from " + root);
    }
    return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
}
Also used : ExplodedArchive(org.springframework.boot.loader.archive.ExplodedArchive) ProtectionDomain(java.security.ProtectionDomain) JarFileArchive(org.springframework.boot.loader.archive.JarFileArchive) CodeSource(java.security.CodeSource) URI(java.net.URI) JarFile(org.springframework.boot.loader.jar.JarFile) File(java.io.File)

Example 63 with CodeSource

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

the class DocumentRoot method getCodeSourceArchive.

private File getCodeSourceArchive() {
    try {
        CodeSource codeSource = getClass().getProtectionDomain().getCodeSource();
        URL location = (codeSource == null ? null : codeSource.getLocation());
        if (location == null) {
            return null;
        }
        String path = location.getPath();
        URLConnection connection = location.openConnection();
        if (connection instanceof JarURLConnection) {
            path = ((JarURLConnection) connection).getJarFile().getName();
        }
        if (path.indexOf("!/") != -1) {
            path = path.substring(0, path.indexOf("!/"));
        }
        return new File(path);
    } catch (IOException ex) {
        return null;
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) CodeSource(java.security.CodeSource) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 64 with CodeSource

use of java.security.CodeSource in project jetbrick-template-1x by subchen.

the class Version method getVersion.

public static String getVersion(Class<?> cls, String defaultVersion) {
    try {
        // look up version from MANIFEST.MF
        String version = cls.getPackage().getImplementationVersion();
        if (version == null || version.length() == 0) {
            version = cls.getPackage().getSpecificationVersion();
        }
        if (version == null || version.length() == 0) {
            // look up version from jar file name
            // sample: jetbrick-template-1.0.2.jar
            CodeSource codeSource = cls.getProtectionDomain().getCodeSource();
            if (codeSource != null) {
                String file = codeSource.getLocation().getFile();
                if (file != null) {
                    Matcher matcher = Pattern.compile("[\\-\\._][vV]?(\\d+([\\-\\._]\\d+)*)\\.jar$").matcher(file);
                    if (matcher.find()) {
                        version = matcher.group(1);
                        version = version.replaceAll("[\\-_]", ".");
                    }
                }
            }
        }
        if (version == null || version.length() == 0) {
            version = defaultVersion;
        }
        return version;
    } catch (Throwable e) {
        return defaultVersion;
    }
}
Also used : Matcher(java.util.regex.Matcher) CodeSource(java.security.CodeSource)

Example 65 with CodeSource

use of java.security.CodeSource in project lwjgl by LWJGL.

the class AppletLoader method updateClassPath.

/**
	 * Edits the ClassPath at runtime to include the jars
	 * that have just been downloaded and then adds the
	 * lwjgl natives folder property.
	 *
	 * @param path location where applet is stored
	 * @throws Exception if it fails to add classpath
	 */
protected void updateClassPath(final String path) throws Exception {
    setState(STATE_UPDATING_CLASSPATH);
    percentage = 95;
    URL[] urls = new URL[urlList.length];
    for (int i = 0; i < urlList.length; i++) {
        String file = new File(path, getJarName(urlList[i])).toURI().toString();
        // fix JVM bug where ! is not escaped
        file = file.replace("!", "%21");
        urls[i] = new URL(file);
    }
    // get AppletLoader certificates
    final Certificate[] certs = getCurrentCertificates();
    // detect if we are running on a mac and save result as boolean
    String osName = System.getProperty("os.name");
    final boolean isMacOS = (osName.startsWith("Mac") || osName.startsWith("Darwin"));
    // add downloaded jars to the classpath with required permissions
    classLoader = new URLClassLoader(urls) {

        protected PermissionCollection getPermissions(CodeSource codesource) {
            PermissionCollection perms = null;
            try {
                // no permissions
                perms = new Permissions();
                // if certificates match the AppletLoader certificates then we should be all set
                if (certificatesMatch(certs, codesource.getCertificates())) {
                    perms.add(new AllPermission());
                    return perms;
                }
                String host = getCodeBase().getHost();
                if (host != null && (host.length() > 0)) {
                    // add permission for downloaded jars to access host they were from
                    perms.add(new SocketPermission(host, "connect,accept"));
                } else if ("file".equals(codesource.getLocation().getProtocol())) {
                    // if running locally add file permission
                    String path = codesource.getLocation().getFile().replace('/', File.separatorChar);
                    perms.add(new FilePermission(path, "read"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return perms;
        }

        // allow non lwjgl native to be found from cache directory
        protected String findLibrary(String libname) {
            String libPath = path + "natives" + File.separator + LWJGLUtil.mapLibraryName(libname);
            if (new File(libPath).exists()) {
                return libPath;
            }
            return super.findLibrary(libname);
        }
    };
    debug_sleep(2000);
    // unload natives loaded by a previous instance of this lwjgl applet
    unloadNatives(path);
    // add natives files path to native class path
    System.setProperty("org.lwjgl.librarypath", path + "natives");
    // Make sure jinput knows about the new path too
    System.setProperty("net.java.games.input.librarypath", path + "natives");
    // set the library path, useful for non lwjgl natives
    System.setProperty("java.library.path", path + "natives");
    // mark natives as loaded
    natives_loaded = true;
}
Also used : PermissionCollection(java.security.PermissionCollection) SocketPermission(java.net.SocketPermission) CodeSource(java.security.CodeSource) FilePermission(java.io.FilePermission) URL(java.net.URL) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) URLClassLoader(java.net.URLClassLoader) Permissions(java.security.Permissions) AllPermission(java.security.AllPermission) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) Certificate(java.security.cert.Certificate)

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