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:net.sf.gilead.proxy.JavassistProxyGenerator.java

@Override
public Class<?> generateProxyFor(Class<?> superClass, AdditionalCode additionalCode) {
    try {//from  ww  w  . j  av a2 s.co  m
        // Compute proxy class name
        //
        String sourceClassName = superClass.getName();
        String proxyClassName = sourceClassName + additionalCode.getSuffix();
        _log.info("Generating server proxy " + proxyClassName + " for class " + sourceClassName);

        // Create proxy class
        //
        ClassPool pool = ClassPool.getDefault();

        // TOMCAT and JBOSS classloader handling
        pool.insertClassPath(new ClassClassPath(superClass));

        CtClass proxyClass = pool.makeClass(proxyClassName);

        // Add proxy inheritance
        //
        proxyClass.setSuperclass(pool.get(sourceClassName));

        // Add ILightEntity inheritance
        //
        if (additionalCode.getImplementedInterface() != null) {
            proxyClass.addInterface(pool.get(additionalCode.getImplementedInterface()));
        }

        // generate Proxy
        //
        generateProxy(proxyClass, additionalCode);

        // Generate class
        //
        return proxyClass.toClass(superClass.getClassLoader(), superClass.getProtectionDomain());
    } catch (Exception ex) {
        throw new ProxyException("Proxy generation failure for " + superClass.getName(), ex);
    }
}

From source file:org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution.java

private FilePath setupMavenSpy() throws IOException, InterruptedException {
    if (tempBinDir == null) {
        throw new IllegalStateException("tempBinDir not defined");
    }//from  w w  w  .j  av a 2s.co  m

    // Mostly for testing / debugging in the IDE
    final String MAVEN_SPY_JAR_URL = "org.jenkinsci.plugins.pipeline.maven.mavenSpyJarUrl";
    String mavenSpyJarUrl = System.getProperty(MAVEN_SPY_JAR_URL);
    InputStream in;
    if (mavenSpyJarUrl == null) {
        String embeddedMavenSpyJarPath = "META-INF/lib/pipeline-maven-spy.jar";
        LOGGER.log(Level.FINE, "Load embedded maven spy jar '" + embeddedMavenSpyJarPath + "'");
        // Don't use Thread.currentThread().getContextClassLoader() as it doesn't show the resources of the plugin
        Class<WithMavenStepExecution> clazz = WithMavenStepExecution.class;
        ClassLoader classLoader = clazz.getClassLoader();
        LOGGER.log(Level.FINE, "Load " + embeddedMavenSpyJarPath + " using classloader "
                + classLoader.getClass() + ": " + classLoader);
        in = classLoader.getResourceAsStream(embeddedMavenSpyJarPath);
        if (in == null) {
            CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
            String msg = "Embedded maven spy jar not found at " + embeddedMavenSpyJarPath
                    + " in the pipeline-maven-plugin classpath. "
                    + "Maven Spy Jar URL can be defined with the system property: '" + MAVEN_SPY_JAR_URL + "'"
                    + "Classloader " + classLoader.getClass() + ": " + classLoader + ". " + "Class "
                    + clazz.getName() + " loaded from "
                    + (codeSource == null ? "#unknown#" : codeSource.getLocation());
            throw new IllegalStateException(msg);
        }
    } else {
        LOGGER.log(Level.FINE, "Load maven spy jar provided by system property '" + MAVEN_SPY_JAR_URL + "': "
                + mavenSpyJarUrl);
        in = new URL(mavenSpyJarUrl).openStream();
    }

    FilePath mavenSpyJarFilePath = tempBinDir.child("pipeline-maven-spy.jar");
    mavenSpyJarFilePath.copyFrom(in);
    return mavenSpyJarFilePath;
}

From source file:net.sourceforge.seqware.pipeline.plugins.ITUtility.java

/**
 * Method returns code source of given class. This is URL of classpath
 * folder, zip or jar file. If code source is unknown, returns null (for
 * example, for classes java.io.*).//w  w w. ja  v  a2  s.  c o m
 *
 * Edited from
 * http://asolntsev.blogspot.ca/2008/03/how-to-find-which-jar-file-contains.html
 * This is extremely close to what we need to identify the full.jar, however
 * right now it returns the pipeline jar in the maven repo rather than the
 * full jar, which I suspect is an issue with how I've defined dependencies
 * in Maven rather than a issueJava.
 *
 * @param clazz For example, java.sql.SQLException.class
 * @return for example, "file:/C:/jdev10/jdev/mywork/classes/" or
 * "file:/C:/works/projects/classes12.zip"
 */
public static File getCodeSource(Class clazz) {
    if (clazz == null || clazz.getProtectionDomain() == null
            || clazz.getProtectionDomain().getCodeSource() == null
            || clazz.getProtectionDomain().getCodeSource().getLocation() == null) // This typically happens for system classloader
    {
        Log.error("Could not access protection domainon " + clazz.getName());
        return null;
    }

    URI uri = null;
    try {
        uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI();
    } catch (URISyntaxException ex) {
        Log.error("Could not determine location of " + clazz.getName());
        return null;
    }
    if (uri != null) {
        File file = new File(uri);
        return file;
    }
    Log.error("Could not translate URI location of " + clazz.getName());
    return null;
}

From source file:net.technicpack.autoupdate.Relauncher.java

public String getRunningPath(Class mainClass) throws UnsupportedEncodingException {
    return URLDecoder.decode(mainClass.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
}

From source file:nl.nn.adapterframework.util.ClassUtils.java

/**
 * Determine the last modification date for this
 * class file or its enclosing library//  w  ww  .  j a  v  a  2  s.  c  om
 *
 * @param aClass A class whose last modification date is queried
 * @return The time the given class was last modified
 * @exception IOException IOError
 * @exception IllegalArgumentException The class was not loaded from a file
 * or directory
 */
public static long lastModified(Class aClass) throws IOException, IllegalArgumentException {
    URL url = aClass.getProtectionDomain().getCodeSource().getLocation();

    if (!url.getProtocol().equals("file")) {
        throw new IllegalArgumentException("Class was not loaded from a file url");
    }

    File directory = new File(url.getFile());
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException("Class was not loaded from a directory");
    }

    String className = aClass.getName();
    String basename = className.substring(className.lastIndexOf(".") + 1);

    File file = new File(directory, basename + ".class");

    return file.lastModified();
}

From source file:nl.nn.adapterframework.util.ClassUtils.java

/**
 * Gets the absolute pathname of the class file
 * containing the specified class name, as prescribed
 * by the current classpath./*from  w w  w  .  j a v  a2 s  .  co  m*/
 *
 * @param aClass A class
 */
public static String which(Class aClass) {
    String path = null;
    try {
        path = aClass.getProtectionDomain().getCodeSource().getLocation().toString();
    } catch (Throwable t) {
    }
    return path;
}

From source file:org.apache.accumulo.cluster.standalone.StandaloneClusterControl.java

String getJarFromClass(Class<?> clz) {
    CodeSource source = clz.getProtectionDomain().getCodeSource();
    if (null == source) {
        throw new RuntimeException("Could not get CodeSource for class");
    }//from   w  w  w .  ja  v a2  s . c o  m
    URL jarUrl = source.getLocation();
    String jar = jarUrl.getPath();
    if (!jar.endsWith(".jar")) {
        throw new RuntimeException("Need to have a jar to run mapreduce: " + jar);
    }
    return jar;
}

From source file:org.apache.apex.common.util.JarHelper.java

public String getJar(Class<?> jarClass, boolean makeJarFromFolder) {
    String jar = null;//  w  ww. jav a  2  s.  c  o  m
    final CodeSource codeSource = jarClass.getProtectionDomain().getCodeSource();
    if (codeSource != null) {
        URL location = codeSource.getLocation();
        jar = sourceToJar.get(location);
        if (jar == null) {
            // don't create jar file from folders multiple times
            if ("jar".equals(location.getProtocol())) {
                try {
                    location = ((JarURLConnection) location.openConnection()).getJarFileURL();
                } catch (IOException e) {
                    throw new AssertionError("Cannot resolve jar file for " + jarClass, e);
                }
            }
            if ("file".equals(location.getProtocol())) {
                jar = location.getFile();
                final File dir = new File(jar);
                if (dir.isDirectory()) {
                    if (!makeJarFromFolder) {
                        throw new AssertionError(
                                "Cannot resolve jar file for " + jarClass + ". URL " + location);
                    }
                    try {
                        jar = createJar("apex-", dir, false);
                    } catch (IOException e) {
                        throw new AssertionError(
                                "Cannot resolve jar file for " + jarClass + ". URL " + location, e);
                    }
                }
            } else {
                throw new AssertionError("Cannot resolve jar file for " + jarClass + ". URL " + location);
            }
            sourceToJar.put(location, jar);
            logger.debug("added sourceLocation {} as {}", location, jar);
        }
        if (jar == null) {
            throw new AssertionError("Cannot resolve jar file for " + jarClass);
        }
    }
    return jar;
}

From source file:org.apache.axis.client.HappyClient.java

/**
 * get the location of a class/*  w  ww . ja v a 2  s . c  o m*/
 * @param clazz
 * @return the jar file or path where a class was found
 */

String getLocation(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 Messages.getMessage("happyClientUnknownLocation");
}

From source file:org.apache.axis2.deployment.RepositoryListener.java

/**
 * To get the location of the Axis2.jar from that I can drive the location of class path
 *
 * @return String (location of the axis2 jar)
 *///w  ww  .  j  a v a 2s.com
protected String getLocation() {
    try {
        Class clazz = Loader.loadClass("org.apache.axis2.engine.AxisEngine");
        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")) {
            File file = Utils.toFile(url);
            return file.getAbsolutePath();
        } else {
            return url.toString();
        }
    } catch (Throwable t) {
        return null;
    }
}