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.apache.jsp.happyaxis_jsp.java

/**
 * get the location of a class//from   ww w.  j  av  a  2s.  c om
 * @param out
 * @param clazz
 * @return the jar file or path where a class was found
 */

String getLocation(JspWriter out, Class clazz) {
    try {
        java.net.URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
        String location = url.toString();
        if (location.startsWith("jar")) {
            url = ((java.net.JarURLConnection) url.openConnection()).getJarFileURL();
            location = url.toString();
        }

        if (location.startsWith("file")) {
            java.io.File file = new java.io.File(url.getFile());
            return file.getAbsolutePath();
        } else {
            return url.toString();
        }
    } catch (Throwable t) {
    }
    return getMessage("classFoundError");
}

From source file:org.apache.marmotta.platform.core.services.modules.ModuleServiceImpl.java

/**
 * Provide the current module configuration for the given class, i.e. the configuration of the
 * module containing the class.//from   w  ww .j  a va 2 s.c  o m
 */
@Override
public ModuleConfiguration getModuleConfiguration(Class<?> cls) {
    URL jarUrl = cls.getProtectionDomain().getCodeSource().getLocation();
    Configuration cfg = jarURLs.get(jarUrl.toString());
    if (cfg != null) {
        return new ModuleConfiguration(cfg);
    } else {
        return new ModuleConfiguration(new MapConfiguration(new HashMap<String, Object>()));
    }
}

From source file:org.apache.metron.maas.service.Client.java

public static String getJar(Class klass) throws URISyntaxException {
    return klass.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
}

From source file:org.apache.ranger.plugin.classloader.RangerPluginClassLoaderUtil.java

private String getPluginImplLibPath(String serviceType, Class<?> pluginClass) throws Exception {

    String ret = null;// w  w  w.j  av a  2 s  .  c o m

    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerPluginClassLoaderUtil.getPluginImplLibPath for Class (" + pluginClass.getName()
                + ")");
    }

    URI uri = pluginClass.getProtectionDomain().getCodeSource().getLocation().toURI();

    Path path = Paths.get(URI.create(uri.toString()));

    ret = path.getParent().toString() + File.separatorChar + rangerPluginLibDir.replaceAll("%", serviceType);

    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerPluginClassLoaderUtil.getPluginImplLibPath for Class (" + pluginClass.getName()
                + " PATH :" + ret + ")");
    }

    return ret;
}

From source file:org.apache.struts2.JSPLoader.java

protected String getJarUrl(Class clazz) {
    ProtectionDomain protectionDomain = clazz.getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URL loc = codeSource.getLocation();
    File file = FileUtils.toFile(loc);
    return file.getAbsolutePath();
}

From source file:org.apache.struts2.osgi.BaseOsgiHost.java

protected String getJarUrl(Class clazz) {
    ProtectionDomain protectionDomain = clazz.getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URL loc = codeSource.getLocation();
    return loc.toString();
}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Obtain a list of all classes in a jar file corresponding to a referenced
 * class.//from   www.ja v  a2  s . co m
 * 
 * @param classInJarFile
 * @return list of all the commons-math3 classes in the referenced
 *         commons-math3 jar file
 * @throws IOException
 *             if an IOException occurs
 * @throws ClassNotFoundException
 *             if a ClassNotFoundException occurs
 */
private static List<String> getAllClassesInJar(Class<?> classInJarFile)
        throws IOException, ClassNotFoundException {
    List<String> classPathNames = new ArrayList<>();
    String jarLocation = classInJarFile.getProtectionDomain().getCodeSource().getLocation().getPath();
    File f = new File(jarLocation);
    try (FileInputStream fis = new FileInputStream(f);
            JarArchiveInputStream jais = new JarArchiveInputStream(fis)) {
        while (true) {
            JarArchiveEntry jae = jais.getNextJarEntry();
            if (jae == null) {
                break;
            }
            String name = jae.getName();
            if (name.endsWith(".class")) {
                classPathNames.add(name);
            }
        }
    }

    String jarName = jarLocation.substring(jarLocation.lastIndexOf("/") + 1);
    addClassPathNamesToJarsAndClasses(jarName, classPathNames);

    return classPathNames;
}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Obtain the file system path to the location of a class.
 * //  ww w . ja  v  a 2  s . c  om
 * @param clazz
 *            the class
 * @return the file system path to the location of a class
 */
private static String getPathToClass(Class<?> clazz) {
    try {
        return clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
    } catch (java.lang.NullPointerException e) {
        return null;
    }
}

From source file:org.atomserver.utils.io.JarUtils.java

/**
 * Find out where a class on the classpath will be loaded from.
 * This will be either a Jar URL (e.g. )
 * or a File URL (e.g. file:/foo/bar/target/classes)
 * @param className name of fully qualified class to find, using dots, but no dot class.
 *                  e.g. org.atomserver.utils.io.JarUtils
 * @return The URL associated with this class
 * @throws ClassNotFoundException/*from  w  w  w  .  j a  v  a  2s  .c  o  m*/
 */
static public URL getURLForClass(String className) throws ClassNotFoundException {
    Class qc = Class.forName(className);
    CodeSource source = qc.getProtectionDomain().getCodeSource();
    URL location = null;
    if (source != null) {
        location = source.getLocation();
        log.debug(className + " : " + location);
    }
    return location;
}

From source file:org.ballerinalang.repository.fs.ClasspathPackageRepository.java

private static Path generatePath(Class<? extends Object> providerClassRef, String orgName) {
    try {/*from  w ww  . ja  v a  2  s. c o  m*/
        URI classURI = providerClassRef.getProtectionDomain().getCodeSource().getLocation().toURI();
        String classPath = classURI.getPath();
        // TODO Fix this properly for other platforms too
        if (SystemUtils.IS_OS_WINDOWS) {
            classPath = classPath.replace(" ", "%20");
        }
        URI pathUri;
        String basePath = JAR_SOURCE_LOCATION + orgName;
        if (classPath.endsWith(".jar")) {
            pathUri = URI.create("jar:file:" + classPath + "!" + basePath);
        } else {
            if (classPath.endsWith(File.separator)) {
                classPath = classPath.substring(0, classPath.length() - 1);
            }
            pathUri = URI.create("file:" + classPath + basePath);
        }
        initFS(pathUri);
        return Paths.get(pathUri);
    } catch (URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}