Search in sources :

Example 66 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 67 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 68 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 69 with CodeSource

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

the class SystemPropertyConfiguredIsReloadableTypePlugin method shouldBeMadeReloadable.

// TODO need try/catch protection when calling plugins, in case of bad ones
public ReloadDecision shouldBeMadeReloadable(TypeRegistry typeRegistry, String typename, ProtectionDomain protectionDomain, byte[] bytes) {
    if (debug) {
        System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: entered, for typename " + typename);
    }
    if (protectionDomain == null) {
        return ReloadDecision.PASS;
    }
    String reloadableDirs = System.getProperty("springloaded.directoriesContainingReloadableCode");
    if (debug) {
        System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: reloadableDirs=" + reloadableDirs);
    }
    if (reloadableDirs == null) {
        return ReloadDecision.PASS;
    } else {
        if (mostRecentReloadableDirs != reloadableDirs) {
            synchronized (includes) {
                if (mostRecentReloadableDirs != reloadableDirs) {
                    includes.clear();
                    excludes.clear();
                    // update our cached information
                    StringTokenizer st = new StringTokenizer(reloadableDirs, ",");
                    while (st.hasMoreTokens()) {
                        String nextDir = st.nextToken();
                        boolean isNot = nextDir.charAt(0) == '!';
                        if (isNot) {
                            excludes.add(nextDir.substring(1));
                        } else {
                            includes.add(nextDir);
                        }
                    }
                    mostRecentReloadableDirs = reloadableDirs;
                }
            }
        }
    }
    // Typical example:
    // typename = com/vmware/rabbit/HomeController
    // codeSource.getLocation() = file:/Users/aclement/springsource/tc-server-developer-2.1.1.RELEASE/spring-insight-instance/wtpwebapps/hello-rabbit-client/WEB-INF/classes/com/vmware/rabbit/HomeController.class
    CodeSource codeSource = protectionDomain.getCodeSource();
    if (codeSource == null || codeSource.getLocation() == null) {
        // nothing to watch...
        return ReloadDecision.NO;
    //			if (debug) {
    //				System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename + " does not have a codeSource");
    //			}
    } else {
        // May have to do something special for CGLIB types
        // These will have a type name of something like: grails/plugin/springsecurity/SpringSecurityService$$EnhancerByCGLIB$$8f956be2
        // But a codesource location of file:/Users/aclement/.m2/repository/org/springframework/spring-core/3.2.5.RELEASE/spring-core-3.2.5.RELEASE.jar
        int cglibIndex = typename.indexOf("$$EnhancerBy");
        if (cglibIndex == -1) {
            cglibIndex = typename.indexOf("$$FastClassBy");
        }
        if (cglibIndex != -1) {
            // assuming first $$ is good enough
            String originalType = typename.substring(0, typename.indexOf("$$"));
            while (typeRegistry != null) {
                ReloadableType originalReloadable = typeRegistry.getReloadableType(originalType);
                if (originalReloadable != null) {
                    return ReloadDecision.YES;
                }
                typeRegistry = typeRegistry.getParentRegistry();
            }
        }
        if (debug) {
            System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename + " codeSource.getLocation() is " + codeSource.getLocation());
        }
    }
    try {
        URI uri = codeSource.getLocation().toURI();
        File file = new File(uri);
        String path = file.toString();
        synchronized (includes) {
            for (String exclude : excludes) {
                if (path.contains(exclude)) {
                    if (debug) {
                        System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename + " is not being made reloadable");
                    }
                    return ReloadDecision.NO;
                }
            }
            for (String include : includes) {
                if (path.contains(include)) {
                    if (debug) {
                        System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename + " is being made reloadable");
                    }
                    return ReloadDecision.YES;
                }
            }
        }
    //			StringTokenizer st = new StringTokenizer(reloadableDirs, ",");
    //			while (st.hasMoreTokens()) {
    //				String nextDir = st.nextToken();
    //				boolean isNot = nextDir.charAt(0) == '!';
    //				if (isNot)
    //					nextDir = nextDir.substring(1);
    //				if (path.contains(nextDir)) {
    //					if (isNot) {
    //						if (debug) {
    //							System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename
    //									+ " is not being made reloadable");
    //						}
    //						return ReloadDecision.NO;
    //					} else {
    //						if (debug) {
    //							System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename
    //									+ " is being made reloadable");
    //						}
    //						return ReloadDecision.YES;
    //					}
    //				}
    //			}
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException iae) {
        // IAE: URI is not hierarchical
        if (debug) {
            try {
                System.out.println("IllegalArgumentException: URI is not hierarchical, uri is " + codeSource.getLocation().toURI());
            } catch (URISyntaxException use) {
                System.out.println("IllegalArgumentException: URI is not hierarchical, uri is " + codeSource.getLocation());
            }
        }
    }
    if (debug) {
        System.out.println("SystemPropertyConfiguredIsReloadableTypePlugin: " + typename + " is being PASSed on");
    }
    return ReloadDecision.PASS;
}
Also used : StringTokenizer(java.util.StringTokenizer) URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URI(java.net.URI) File(java.io.File)

Example 70 with CodeSource

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

the class SpringLoadedPreProcessor method getWatchPathFromProtectionDomain.

/**
	 * Determine where to watch for changes based on the protectionDomain. Relying on the protectionDomain may prove
	 * fragile though, as it is up to the classloader in question to create it. Some classloaders will create one
	 * protectionDomain per 'directory' containing class files (and so the slashedClassName must be appended to the
	 * codesource). Some classloaders have a protectiondomain per class.
	 *
	 * @param protectionDomain the protection domain passed in to the defineclass call
	 * @param slashedClassName the slashed class name currently being defined
	 * @return the path to watch for changes to this class
	 */
private String getWatchPathFromProtectionDomain(ProtectionDomain protectionDomain, String slashedClassName) {
    String watchPath = null;
    //				+ protectionDomain + " codesource=" + (protectionDomain == null ? "null" : protectionDomain.getCodeSource()));
    if (protectionDomain == null) {
        if (GlobalConfiguration.isRuntimeLogging && log.isLoggable(Level.WARNING)) {
            log.warning("Changes to type cannot be tracked: " + slashedClassName + " - no protection domain");
        }
    } else {
        try {
            CodeSource codeSource = protectionDomain.getCodeSource();
            if (codeSource == null || codeSource.getLocation() == null) {
                if (GlobalConfiguration.isRuntimeLogging && log.isLoggable(Level.WARNING)) {
                    log.warning("null codesource for " + slashedClassName);
                }
            } else {
                if (GlobalConfiguration.isRuntimeLogging && log.isLoggable(Level.FINEST)) {
                    log.finest("Codesource.getLocation()=" + codeSource.getLocation());
                }
                // A 'URI is not hierarchical' message can come out when the File ctor is called. Cases seen
                // so far:
                // GRAILS-10384: relative URL file:../foo/bar - should have built it with new File().toURI.toURL() and not just new URL()
                File file = null;
                URI uri = null;
                try {
                    file = new File(codeSource.getLocation().getFile());
                    uri = file.toURI();
                } catch (IllegalArgumentException iae) {
                    boolean recovered = false;
                    if (iae.toString().indexOf("URI is not hierarchical") != -1) {
                        // try another approach...
                        String uristring = uri.toString();
                        if (uristring.startsWith("file:../")) {
                            file = new File(uristring.substring(8)).getAbsoluteFile();
                        } else if (uristring.startsWith("file:./")) {
                            file = new File(uristring.substring(7)).getAbsoluteFile();
                        }
                        if (file != null && file.exists()) {
                            recovered = true;
                        }
                    }
                    if (!recovered) {
                        System.out.println("Unable to watch file: classname = " + slashedClassName + " codesource location = " + codeSource.getLocation() + " ex = " + iae.toString());
                        return null;
                    }
                }
                if (file.isDirectory()) {
                    file = new File(file, slashedClassName + ".class");
                } else if (file.getName().endsWith(".class")) {
                // great! nothing to do
                } else if (file.getName().endsWith(".jar")) {
                    boolean found = false;
                    if (GlobalConfiguration.jarsToWatch != null) {
                        // Check if it is one to watch
                        String candidate = file.getName();
                        for (String jarToWatch : GlobalConfiguration.jarsToWatch) {
                            if (candidate.equals(jarToWatch)) {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found && GlobalConfiguration.isRuntimeLogging && log.isLoggable(Level.WARNING)) {
                        log.warning("unable to watch this jar file entry: " + slashedClassName.replace('/', '.') + ". Computed location=" + file.toString());
                    }
                    if (!found)
                        return null;
                } else if (file.toString().equals("/groovy/script") || file.toString().equals("\\groovy\\script")) {
                    // reloaded we will have to be told via an alternate route
                    return null;
                } else if (!file.toString().endsWith(".class")) {
                    // something other than a class, no point in watching it
                    return null;
                } else {
                    throw new UnsupportedOperationException("unable to watch " + slashedClassName.replace('/', '.') + ". Computed location=" + file.toString());
                }
                watchPath = file.toString();
                if (GlobalConfiguration.isRuntimeLogging && log.isLoggable(Level.INFO)) {
                    log.info("Watched location for changes to " + slashedClassName + " is " + watchPath);
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException("Unexpected problem processing URI ", e);
        }
    }
    return watchPath;
}
Also used : CodeSource(java.security.CodeSource) File(java.io.File) URI(java.net.URI)

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