Example usage for java.lang Class getProtectionDomain

List of usage examples for java.lang Class getProtectionDomain

Introduction

In this page you can find the example usage for java.lang Class getProtectionDomain.

Prototype

public java.security.ProtectionDomain getProtectionDomain() 

Source Link

Document

Returns the ProtectionDomain of this class.

Usage

From source file:org.sipfoundry.sipxconfig.test.TestHelper.java

/**
* The directory that is part of the classpath that a class was loaded from
*//*from w  w  w  .j  a va 2s  .com*/
public static String getClasspathDirectory(Class testClass) {
    // create file on classpath
    CodeSource code = testClass.getProtectionDomain().getCodeSource();
    URL classpathUrl = code.getLocation();
    File classpathDir = new File(classpathUrl.getFile());

    return classpathDir.getAbsolutePath();
}

From source file:org.sonatype.sisu.bl.jsw.JSWConfig.java

public JSWConfig addToJavaClassPath(final Class clazz) {
    final URL jar = clazz.getProtectionDomain().getCodeSource().getLocation();
    return addToJavaClassPath(urlToFile(jar).getAbsolutePath());
}

From source file:org.springframework.boot.gradle.testkit.GradleBuild.java

private String pathOfJarContaining(Class<?> type) {
    return type.getProtectionDomain().getCodeSource().getLocation().getPath();
}

From source file:org.springframework.cloud.function.deployer.ApplicationBootstrap.java

private URL[] findClassPath(Class<?> mainClass) {
    ClassLoader base = mainClass.getClassLoader();
    if (!(base instanceof URLClassLoader)) {
        try {//from w  w  w  .jav  a  2 s  . co  m
            // Guess the classpath, based on where we can resolve existing resources
            List<URL> list = Collections.list(mainClass.getClassLoader().getResources("META-INF"));
            List<URL> result = new ArrayList<>();
            result.add(mainClass.getProtectionDomain().getCodeSource().getLocation());
            for (URL url : list) {
                String path = url.toString();
                path = path.substring(0, path.length() - "/META-INF".length()) + "/";
                result.add(new URL(path));
            }
            return result.toArray(new URL[result.size()]);
        } catch (IOException e) {
            throw new IllegalStateException("Cannot find class path", e);
        }
    } else {
        @SuppressWarnings("resource")
        URLClassLoader urlClassLoader = (URLClassLoader) base;
        return urlClassLoader.getURLs();
    }
}

From source file:org.structr.core.Services.java

public Set<String> getResources() {

    final Set<String> resources = new LinkedHashSet<>();

    // scan through structr.conf and try to identify module-specific classes
    for (final Object configurationValue : structrConf.values()) {

        for (final String value : configurationValue.toString().split("[\\s ,;]+")) {

            try {

                // try to load class and find source code origin
                final Class candidate = Class.forName(value);
                if (!candidate.getName().startsWith("org.structr")) {

                    final String codeLocation = candidate.getProtectionDomain().getCodeSource().getLocation()
                            .toString();
                    if (codeLocation.startsWith("file:") && codeLocation.endsWith(".jar")
                            || codeLocation.endsWith(".war")) {

                        final File file = new File(URI.create(codeLocation));
                        if (file.exists()) {

                            resources.add(file.getAbsolutePath());
                        }//from   w w w .ja  v  a  2s . c om
                    }
                }

            } catch (Throwable ignore) {
            }
        }
    }

    logger.log(Level.INFO, "Found {0} possible resources: {1}", new Object[] { resources.size(), resources });

    return resources;
}

From source file:org.stubby.cli.CommandLineIntepreter.java

private static String getCurrentJarLocation(final Class theclass) {
    final URL location = theclass.getProtectionDomain().getCodeSource().getLocation();
    try {/* w  w w  . j  av  a2 s  . c om*/
        final String jar = new File(location.getFile()).getName();
        if (jar.toLowerCase().endsWith(".jar")) {
            return jar;
        }
        return "stubby4j-x.x.x-SNAPSHOT.jar";
    } catch (Exception ignored) {
        return "stubby4j-x.x.x-SNAPSHOT.jar";
    }
}

From source file:org.wso2.carbon.wsdl2code.POMGenerator.java

public static URL getContainingArtifact(Class clazz) {
    if (clazz == null || clazz.getProtectionDomain() == null
            || clazz.getProtectionDomain().getCodeSource() == null
            || clazz.getProtectionDomain().getCodeSource().getLocation() == null)

        // This typically happens for system classloader
        // (java.lang.* etc. classes)
        return null;

    return clazz.getProtectionDomain().getCodeSource().getLocation();
}

From source file:rapture.module.ModuleLoaderTest.java

@Test
@Ignore//from w w w . j a  v a  2 s.  co  m
public void testLoadingExternalJar() throws ClassNotFoundException, IOException {
    ModuleLoader loader = new ModuleLoader();
    loader.addJar("/Users/amkimian/Development/cloud/libs/BloombergData-1.0.0-RC16.jar");
    // Now try and load the net package used by BloombergData
    Class<?> c = Class.forName("org.apache.commons.net.ftp.FTPFileFilter");
    System.out.println("Got it! = " + c.getProtectionDomain().getCodeSource().getLocation().toString());
}

From source file:schemacrawler.test.utility.TestUtility.java

private static Path buildDirectory() throws Exception {
    final StackTraceElement ste = currentMethodStackTraceElement();
    final Class<?> callingClass = Class.forName(ste.getClassName());
    final Path codePath = Paths.get(callingClass.getProtectionDomain().getCodeSource().getLocation().toURI())
            .normalize().toAbsolutePath();
    final boolean isInTarget = codePath.toString().contains("target");
    if (!isInTarget) {
        throw new RuntimeException("Not in build directory, " + codePath);
    }/*from   w  ww  .  j a  va 2  s  .c o  m*/
    final Path directory = codePath.resolve("..");
    return directory.normalize().toAbsolutePath();
}

From source file:wordnice.utils.JavaUtils.java

public static File getClassesLocation(Class<?> cls) throws URISyntaxException {
    return new File(cls.getProtectionDomain().getCodeSource().getLocation().toURI());
}