Search in sources :

Example 76 with ProtectionDomain

use of java.security.ProtectionDomain in project blade by biezhi.

the class AbstractClassGenerator method generate.

protected Class generate(ClassLoaderData data) {
    Class gen;
    Object save = CURRENT.get();
    CURRENT.set(this);
    try {
        ClassLoader classLoader = data.getClassLoader();
        if (classLoader == null) {
            throw new IllegalStateException("ClassLoader is null while trying to define class " + getClassName() + ". It seems that the loader has been expired from a weak reference somehow. " + "Please file an issue at cglib's issue tracker.");
        }
        synchronized (classLoader) {
            String name = generateClassName(data.getUniqueNamePredicate());
            data.reserveName(name);
            this.setClassName(name);
        }
        if (attemptLoad) {
            try {
                gen = classLoader.loadClass(getClassName());
                return gen;
            } catch (ClassNotFoundException e) {
            // ignore
            }
        }
        byte[] b = strategy.generate(this);
        String className = ClassNameReader.getClassName(new ClassReader(b));
        ProtectionDomain protectionDomain = getProtectionDomain();
        synchronized (classLoader) {
            // just in case
            if (protectionDomain == null) {
                gen = ReflectUtils.defineClass(className, b, classLoader);
            } else {
                gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain);
            }
        }
        return gen;
    } catch (RuntimeException e) {
        throw e;
    } catch (Error e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    } finally {
        CURRENT.set(save);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassReader(org.objectweb.asm.ClassReader)

Example 77 with ProtectionDomain

use of java.security.ProtectionDomain in project jetty.project by eclipse.

the class WebAppClassLoaderTest method testNullClassFileTransformer.

@Test
public void testNullClassFileTransformer() throws Exception {
    _loader.addTransformer(new ClassFileTransformer() {

        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            return null;
        }
    });
    assertCanLoadClass("org.acme.webapp.ClassInJarA");
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 78 with ProtectionDomain

use of java.security.ProtectionDomain in project flyway by flyway.

the class ClassUtils method getLocationOnDisk.

/**
     * Retrieves the physical location on disk of this class.
     *
     * @param aClass The class to get the location for.
     * @return The absolute path of the .class file.
     */
public static String getLocationOnDisk(Class<?> aClass) {
    try {
        ProtectionDomain protectionDomain = aClass.getProtectionDomain();
        if (protectionDomain == null) {
            //Android
            return null;
        }
        CodeSource codeSource = protectionDomain.getCodeSource();
        if (codeSource == null) {
            //Custom classloader with for example classes defined using URLClassLoader#defineClass(String name, byte[] b, int off, int len)
            return null;
        }
        String url = codeSource.getLocation().getPath();
        return URLDecoder.decode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        //Can never happen.
        return null;
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CodeSource(java.security.CodeSource)

Example 79 with ProtectionDomain

use of java.security.ProtectionDomain in project gitblit by gitblit.

the class Launcher method main.

public static void main(String[] args) {
    if (DEBUG) {
        System.out.println("jcp=" + System.getProperty("java.class.path"));
        ProtectionDomain protectionDomain = Launcher.class.getProtectionDomain();
        System.out.println("launcher=" + protectionDomain.getCodeSource().getLocation().toExternalForm());
    }
    // Load the JARs in the lib and ext folder
    String[] folders = new String[] { "lib", "ext" };
    List<File> jars = new ArrayList<File>();
    for (String folder : folders) {
        if (folder == null) {
            continue;
        }
        File libFolder = new File(folder);
        if (!libFolder.exists()) {
            continue;
        }
        List<File> found = findJars(libFolder.getAbsoluteFile());
        jars.addAll(found);
    }
    // sort the jars by name and then reverse the order so the newer version
    // of the library gets loaded in the event that this is an upgrade
    Collections.sort(jars);
    Collections.reverse(jars);
    if (jars.size() == 0) {
        for (String folder : folders) {
            File libFolder = new File(folder);
            // this is a test of adding a comment
            // more really interesting things
            System.err.println("Failed to find any JARs in " + libFolder.getPath());
        }
        System.exit(-1);
    } else {
        for (File jar : jars) {
            try {
                jar.canRead();
                addJarFile(jar);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
    // Start Server
    GitBlitServer.main(args);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ArrayList(java.util.ArrayList) File(java.io.File)

Example 80 with ProtectionDomain

use of java.security.ProtectionDomain in project elasticsearch by elastic.

the class ESPolicyUnitTests method testListen.

public void testListen() {
    assumeTrue("test cannot run with security manager", System.getSecurityManager() == null);
    final PermissionCollection noPermissions = new Permissions();
    final ESPolicy policy = new ESPolicy(noPermissions, Collections.emptyMap(), true);
    assertFalse(policy.implies(new ProtectionDomain(ESPolicyUnitTests.class.getProtectionDomain().getCodeSource(), noPermissions), new SocketPermission("localhost:" + randomFrom(0, randomIntBetween(49152, 65535)), "listen")));
}
Also used : PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) SocketPermission(java.net.SocketPermission) Permissions(java.security.Permissions)

Aggregations

ProtectionDomain (java.security.ProtectionDomain)148 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)44 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)44 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)42 CodeSource (java.security.CodeSource)39 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)28 AccessControlContext (java.security.AccessControlContext)24 Permissions (java.security.Permissions)22 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)20 Permission (java.security.Permission)17 URL (java.net.URL)16 Policy (java.security.Policy)16 Test (org.junit.Test)16 File (java.io.File)12 PermissionCollection (java.security.PermissionCollection)12 IOException (java.io.IOException)11 Method (java.lang.reflect.Method)8 URI (java.net.URI)8 HashSet (java.util.HashSet)8 Principal (java.security.Principal)7