changeset 29:9aac6ac433fe

7900968: Problems with adding permissions to objects in the case of AgentClassLoader2 Summary: this is a suggested fix from Olga Strizhenko
author bkurotsu
date Thu, 10 Jul 2014 17:20:07 -0600
parents a7105e00482d
children 2ba815306d26
files src/com/sun/javatest/agent/AgentClassLoader2.java
diffstat 1 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/sun/javatest/agent/AgentClassLoader2.java	Thu Jul 10 14:40:32 2014 -0600
+++ b/src/com/sun/javatest/agent/AgentClassLoader2.java	Thu Jul 10 17:20:07 2014 -0600
@@ -32,17 +32,45 @@
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
+import java.security.CodeSource;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.ProtectionDomain;
+import java.util.HashMap;
 import java.util.MissingResourceException;
 
 class AgentClassLoader2 extends ClassLoader {
 
+    private CodeSource cs = null;
+    private final HashMap<CodeSource, ProtectionDomain> pdcache = new HashMap<CodeSource, ProtectionDomain>(11);
     private static volatile AgentClassLoader2 instance = null;
 
     public AgentClassLoader2(Agent.Task parent) {
         super(parent.getClass().getClassLoader());
         this.parent = parent;
+
+        SecurityManager security = System.getSecurityManager();
+        if (security != null) {
+            security.checkCreateClassLoader();
+        }
+        cs = this.getClass().getProtectionDomain().getCodeSource();
     }
 
+
+    private ProtectionDomain getProtectionDomain(CodeSource cs) {
+        ProtectionDomain pd = null;
+        synchronized (pdcache) {
+            pd = pdcache.get(cs);
+            if (pd == null) {
+                PermissionCollection perms = new Permissions();
+                pd = new ProtectionDomain(cs, perms, this, null);
+                pdcache.put(cs, pd);
+            }
+        }
+        return pd;
+    }
+
+
     /*
      * Returns shared instance of classloader for tests where it is required.
      */
@@ -66,11 +94,11 @@
             if (i>0) {
                 String pkgName = className.substring(0,i);
                 if (getPackage(pkgName) == null) {
-                    definePackage(pkgName,null,null,null,null,null,null,null);
+                    definePackage(pkgName, null, null, null, null, null, null, null);
                 }
             }
             byte[] data = parent.getClassData(className);
-            return defineClass(className, data, 0, data.length);
+            return defineClass(className, data, 0, data.length, getProtectionDomain(cs));
         }
         throw new ClassNotFoundException();
     }