Search in sources :

Example 36 with ProtectionDomain

use of java.security.ProtectionDomain in project pinpoint by naver.

the class AccessorInjectionTest method addTraceValue.

@Test
public void addTraceValue() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String javassistClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject";
    loader.addTransformer(javassistClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", loader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(loader, javassistClassName, classfileBuffer);
                aClass.addField(ObjectTraceValue.class.getName());
                aClass.addField(IntTraceValue.class.getName());
                aClass.addField(IntArrayTraceValue.class.getName());
                aClass.addField(IntegerArrayTraceValue.class.getName());
                aClass.addField(DatabaseInfoTraceValue.class.getName());
                aClass.addField(BindValueTraceValue.class.getName());
                String methodName = "callA";
                aClass.getDeclaredMethod(methodName).addInterceptor("com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor");
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(javassistClassName);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    callA.invoke(testObject);
    Class<?> objectTraceValue = loader.loadClass(ObjectTraceValue.class.getName());
    Assert.assertTrue("ObjectTraceValue implements fail", objectTraceValue.isInstance(testObject));
    objectTraceValue.getMethod("_$PINPOINT$_setTraceObject", Object.class).invoke(testObject, "a");
    Object get = objectTraceValue.getMethod("_$PINPOINT$_getTraceObject").invoke(testObject);
    Assert.assertEquals("a", get);
    Class<?> intTraceValue = loader.loadClass(IntTraceValue.class.getName());
    Assert.assertTrue("IntTraceValue implements fail", intTraceValue.isInstance(testObject));
    intTraceValue.getMethod("_$PINPOINT$_setTraceInt", int.class).invoke(testObject, 1);
    int a = (Integer) intTraceValue.getMethod("_$PINPOINT$_getTraceInt").invoke(testObject);
    Assert.assertEquals(1, a);
    Class<?> intArrayTraceValue = loader.loadClass(IntArrayTraceValue.class.getName());
    Assert.assertTrue("IntArrayTraceValue implements fail", intArrayTraceValue.isInstance(testObject));
    int[] expectedInts = { 1, 2, 3 };
    intArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntArray", int[].class).invoke(testObject, expectedInts);
    int[] ints = (int[]) intArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntArray").invoke(testObject);
    Assert.assertEquals(expectedInts, ints);
    Class<?> integerArrayTraceValue = loader.loadClass(IntegerArrayTraceValue.class.getName());
    Assert.assertTrue("IntegerArrayTraceValue implements fail", integerArrayTraceValue.isInstance(testObject));
    Integer[] expectedIntegers = { 1, 2 };
    // wrap due to vararg expansion
    Object[] wrappedExpectedIntegers = new Object[] { expectedIntegers };
    integerArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntegerArray", Integer[].class).invoke(testObject, wrappedExpectedIntegers);
    Integer[] integers = (Integer[]) integerArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntegerArray").invoke(testObject);
    Assert.assertArrayEquals(expectedIntegers, integers);
    Class<?> databaseTraceValue = loader.loadClass(DatabaseInfoTraceValue.class.getName());
    Assert.assertTrue("DatabaseInfoTraceValue implements fail", databaseTraceValue.isInstance(testObject));
    databaseTraceValue.getMethod("_$PINPOINT$_setTraceDatabaseInfo", DatabaseInfo.class).invoke(testObject, UnKnownDatabaseInfo.INSTANCE);
    Object databaseInfo = databaseTraceValue.getMethod("_$PINPOINT$_getTraceDatabaseInfo").invoke(testObject);
    Assert.assertSame(UnKnownDatabaseInfo.INSTANCE, databaseInfo);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test) JavassistClassTest(com.navercorp.pinpoint.test.javasssit.JavassistClassTest)

Example 37 with ProtectionDomain

use of java.security.ProtectionDomain in project OpenAM by OpenRock.

the class ISPermission method equals.

/**
     * Returns true if two <code>ISPermission</code> objects for equality.
     *
     * @param obj <code>ISPermission</code> object.
     * @return true if subject, <code>codesource</code>, service name, resource
     *         name actions and environment parameters of both objects are 
     *         equal.
     */
public boolean equals(Object obj) {
    boolean result = true;
    debug.message("ISPermission:: equals(Object) called ");
    if (obj == this) {
        if (debug.messageEnabled()) {
            debug.message("ISPermission::equals::this " + result);
        }
        return true;
    }
    if (obj instanceof ISPermission) {
        ISPermission perm = (ISPermission) obj;
        Subject subject = perm.getSubject();
        if (subject != null) {
            result = subject.equals(this.subject);
        } else {
            if (this.subject != null) {
                // subject is null, while this.subject is 
                result = false;
            // not null.
            }
        }
        if (debug.messageEnabled()) {
            debug.message("ISPermission::subject equals:" + result);
        }
        if (result) {
            CodeSource codesource = perm.getCodeSource();
            if (codesource != null) {
                result = codesource.equals(this.codesource);
                if (debug.messageEnabled()) {
                    debug.message("ISPermission::codesource equals:" + codesource.equals(this.codesource));
                }
            } else {
                if (this.codesource != null) {
                    result = false;
                }
            }
        }
        if (result) {
            ProtectionDomain protectionDomain = perm.getProtectionDomain();
            if (protectionDomain != null) {
                result = protectionDomain.equals(this.protectionDomain);
                if (debug.messageEnabled()) {
                    debug.message("ISPermission::protectionDomain equals:" + protectionDomain.equals(this.protectionDomain));
                }
            } else {
                if (this.protectionDomain != null) {
                    result = false;
                }
            }
        }
        if (result) {
            String serviceName = perm.getServiceName();
            if (serviceName != null) {
                result = serviceName.equals(this.serviceName);
                if (debug.messageEnabled()) {
                    debug.message("ISPermission::servicename equals:" + serviceName.equals(this.serviceName));
                }
            } else {
                if (this.serviceName != null) {
                    result = false;
                }
            }
        }
        if (result) {
            String resourceName = perm.getResourceName();
            if (resourceName != null) {
                result = resourceName.equals(this.resourceName);
                if (debug.messageEnabled()) {
                    debug.message("ISPermission::resourceName equals:" + resourceName.equals(this.resourceName));
                }
            } else {
                if (this.resourceName != null) {
                    result = false;
                }
            }
        }
        if (result) {
            String actions = perm.getActions();
            if (actions != null) {
                result = actionEquals(actions, this.actions);
                if (debug.messageEnabled()) {
                    debug.message("ISPermission::Actions equals:" + actionEquals(actions, this.actions));
                }
            } else {
                if (this.actions != null) {
                    result = false;
                }
            }
        }
        if (result) {
            Map envParams = perm.getEnvParams();
            if (envParams != null && !envParams.isEmpty()) {
                result = envParams.equals(this.envParams);
                if (debug.messageEnabled()) {
                    debug.message("ISPermission::equals::envMap" + envParams.equals(this.envParams));
                }
            } else {
                if (this.envParams != null && !this.envParams.isEmpty()) {
                    result = false;
                }
            }
        }
    }
    if (debug.messageEnabled()) {
        debug.message("ISPermission::equals::returning " + result);
    }
    return result;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource) Map(java.util.Map) Subject(javax.security.auth.Subject)

Example 38 with ProtectionDomain

use of java.security.ProtectionDomain in project yyl_example by Relucent.

the class ClassPath method getClassLocationURL.

/**
	 * 获取类的class文件位置的URL。这个方法是本类最基础的方法,供其它方法调用。
	 */
private static URL getClassLocationURL(final Class<?> cls) {
    if (cls == null)
        throw new IllegalArgumentException("null input: cls");
    URL result = null;
    final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
    final ProtectionDomain pd = cls.getProtectionDomain();
    if (pd != null) {
        final CodeSource cs = pd.getCodeSource();
        if (cs != null)
            result = cs.getLocation();
        if (result != null) {
            if ("file".equals(result.getProtocol())) {
                try {
                    if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip"))
                        result = new URL("jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
                    else if (new File(result.getFile()).isDirectory())
                        result = new URL(result, clsAsResource);
                } catch (MalformedURLException ignore) {
                }
            }
        }
    }
    if (result == null) {
        final ClassLoader clsLoader = cls.getClassLoader();
        result = clsLoader != null ? clsLoader.getResource(clsAsResource) : ClassLoader.getSystemResource(clsAsResource);
    }
    return result;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) MalformedURLException(java.net.MalformedURLException) CodeSource(java.security.CodeSource) File(java.io.File) URL(java.net.URL)

Example 39 with ProtectionDomain

use of java.security.ProtectionDomain in project joda-time by JodaOrg.

the class TestDateTimeZone method testZoneInfoProviderResourceLoading.

public void testZoneInfoProviderResourceLoading() {
    final Set<String> ids = new HashSet<String>(DateTimeZone.getAvailableIDs());
    ids.remove(DateTimeZone.getDefault().getID());
    final String id = ids.toArray(new String[ids.size()])[new Random().nextInt(ids.size())];
    try {
        Policy.setPolicy(new Policy() {

            @Override
            public PermissionCollection getPermissions(CodeSource codesource) {
                Permissions p = new Permissions();
                // enable everything
                p.add(new AllPermission());
                return p;
            }

            @Override
            public void refresh() {
            }

            @Override
            public boolean implies(ProtectionDomain domain, Permission permission) {
                return !(permission instanceof FilePermission) && !permission.getName().contains(id);
            }
        });
        System.setSecurityManager(new SecurityManager());
        // will throw IllegalArgumentException if the resource can
        // not be loaded
        final DateTimeZone zone = DateTimeZone.forID(id);
        assertNotNull(zone);
    } finally {
        System.setSecurityManager(null);
        Policy.setPolicy(ALLOW);
    }
}
Also used : Policy(java.security.Policy) PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource) FilePermission(java.io.FilePermission) Random(java.util.Random) Permissions(java.security.Permissions) AllPermission(java.security.AllPermission) FilePermission(java.io.FilePermission) Permission(java.security.Permission) AllPermission(java.security.AllPermission) HashSet(java.util.HashSet)

Example 40 with ProtectionDomain

use of java.security.ProtectionDomain in project jdk8u_jdk by JetBrains.

the class ContextInsulation method main.

public static void main(String[] args) throws Exception {
    /*
         * If we delay setting the security manager until after the service
         * configuration file has been installed, then this test still
         * functions properly, but the -Djava.security.debug output is
         * lacking, so to ease debugging, we'll set it early-- at the cost
         * of having to specify the policy even when running standalone.
         */
    TestLibrary.suggestSecurityManager(null);
    ServiceConfiguration.installServiceConfigurationFile();
    /*
         * Execute use of RMIClassLoader within an AccessControlContext
         * that has a protection domain with no permissions, to make sure
         * that RMIClassLoader can still properly initialize itself.
         */
    CodeSource codesource = new CodeSource(null, (Certificate[]) null);
    Permissions perms = null;
    ProtectionDomain pd = new ProtectionDomain(codesource, perms);
    AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { pd });
    java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {

        public Object run() throws Exception {
            TestProvider.exerciseTestProvider(TestProvider2.loadClassReturn, TestProvider2.loadProxyClassReturn, TestProvider2.getClassLoaderReturn, TestProvider2.getClassAnnotationReturn, TestProvider2.invocations);
            return null;
        }
    }, acc);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) Permissions(java.security.Permissions) CodeSource(java.security.CodeSource) Certificate(java.security.cert.Certificate)

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