コード例 #1
0
        private bool defaultImplies(ProtectionDomain domain, Permission permission)
        {
            if (domain == null && permission == null)
            {
                throw new java.lang.NullPointerException();
            }
            bool implies = false;

            if (domain != null)
            {
                PermissionCollection total    = getPermissions(domain);
                PermissionCollection inherent = domain.getPermissions();
                if (inherent != null)
                {
                    java.util.Enumeration <Permission> en = inherent.elements();
                    while (en.hasMoreElements())
                    {
                        total.add(en.nextElement());
                    }
                }
                try {
                    implies = total.implies(permission);
                } catch (java.lang.NullPointerException) {
                    // return false instead of throwing the NullPointerException
                    implies = false;
                }
            }
            return(implies);
        }
コード例 #2
0
        /**
         * Returns a {@code PermissionCollection} describing what permissions are
         * allowed for the specified {@code ProtectionDomain} (more specifically,
         * its {@code CodeSource}) based on the current security policy.
         * <p />
         * Note that this method is not called for classes which are in the
         * system domain (i.e. system classes). System classes are always
         * given full permissions (i.e. AllPermission). This can not be changed by
         * installing a new policy.
         *
         * @param domain
         *            the {@code ProtectionDomain} to compute the permissions for.
         * @return the permissions that are granted to the specified {@code
         *         CodeSource}.
         */
        public PermissionCollection getPermissions(ProtectionDomain domain)
        {
            Permissions permissions = new Permissions();

            if (domain != null)
            {
                try {
                    PermissionCollection cds = getPermissions(domain
                                                              .getCodeSource());
                    if (cds != Policy.UNSUPPORTED_EMPTY_COLLECTION)
                    {
                        java.util.Enumeration <Permission> elements = cds.elements();
                        while (elements.hasMoreElements())
                        {
                            permissions.add(elements.nextElement());
                        }
                    }
                } catch (java.lang.NullPointerException) {
                    // ignore the exception, just add nothing to the result set
                }

                PermissionCollection pds = domain.getPermissions();
                if (pds != null)
                {
                    java.util.Enumeration <Permission> pdElements = pds.elements();
                    while (pdElements.hasMoreElements())
                    {
                        permissions.add(pdElements.nextElement());
                    }
                }
            }
            return(permissions);
        }
コード例 #3
0
        public override bool implies(Permission permission)
        {
            if (permission == null)
            {
                // RI compatible
                throw new java.lang.NullPointerException("Null permission"); //$NON-NLS-1$
            }
            if (allEnabled)
            {
                return(true);
            }
            java.lang.Class      klass      = permission.getClass();
            PermissionCollection klassMates = null;

            UnresolvedPermissionCollection billets = (UnresolvedPermissionCollection)klasses
                                                     .get(typeof(UnresolvedPermission).getClass());

            if (billets != null && billets.hasUnresolved(permission))
            {
                // try to fill up klassMates with freshly resolved permissions
                lock (klasses) {
                    klassMates = (PermissionCollection)klasses.get(klass);
                    try {
                        klassMates = billets.resolveCollection(permission,
                                                               klassMates);
                    } catch (java.lang.Exception ignore) {
                        //TODO log warning
                        ignore.printStackTrace();
                    }

                    if (klassMates != null)
                    {
                        //maybe klassMates were just created
                        // so put them into common map
                        klasses.put(klass, klassMates);
                        // very uncommon case, but not improbable one
                        if (klass == typeof(AllPermission).getClass())
                        {
                            allEnabled = true;
                        }
                    }
                }
            }
            else
            {
                klassMates = (PermissionCollection)klasses.get(klass);
            }

            if (klassMates != null)
            {
                return(klassMates.implies(permission));
            }
            return(false);
        }
コード例 #4
0
        private bool allEnabled; // = false;

        /**
         * Adds the given {@code Permission} to this heterogeneous {@code
         * PermissionCollection}. The {@code permission} is stored in its
         * appropriate {@code PermissionCollection}.
         *
         * @param permission
         *            the {@code Permission} to be added.
         * @throws SecurityException
         *             if this collection's {@link #isReadOnly()} method returns
         *             {@code true}.
         * @throws NullPointerException
         *             if {@code permission} is {@code null}.
         */
        public override void add(Permission permission)
        {
            if (isReadOnly())
            {
                throw new java.lang.SecurityException("collection is read-only"); //$NON-NLS-1$
            }

            if (permission == null)
            {
                throw new java.lang.NullPointerException("invalid null permission"); //$NON-NLS-1$
            }

            java.lang.Class      klass      = permission.getClass();
            PermissionCollection klassMates = (PermissionCollection)klasses
                                              .get(klass);

            if (klassMates == null)
            {
                lock (klasses) {
                    klassMates = (PermissionCollection)klasses.get(klass);
                    if (klassMates == null)
                    {
                        klassMates = permission.newPermissionCollection();
                        if (klassMates == null)
                        {
                            klassMates = new PermissionsHash();
                        }
                        klasses.put(klass, klassMates);
                    }
                }
            }
            klassMates.add(permission);

            if (klass == typeof(AllPermission).getClass())
            {
                allEnabled = true;
            }
        }
コード例 #5
0
        /*
         * Resolves all permissions of the same class as the specified target
         * permission and adds them to the specified collection. If passed
         * collection is {@code null} and some unresolved permissions were resolved,
         * an appropriate new collection is instantiated and used. All resolved
         * permissions are removed from this unresolved collection, and collection
         * with resolved ones is returned.
         *
         * @param target
         *            a kind of permissions to be resolved.
         * @param holder
         *            an existing collection for storing resolved permissions.
         * @return a collection containing resolved permissions (if any found)
         */
        internal PermissionCollection resolveCollection(Permission target,
                                                        PermissionCollection holder)
        {
            String klass = target.getClass().getName();

            if (klasses.containsKey(klass))
            {
                lock (klasses)
                {
                    java.util.Collection <Object> klassMates = (java.util.Collection <Object>)klasses.get(klass);
                    for (java.util.Iterator <Object> iter = klassMates.iterator(); iter.hasNext();)
                    {
                        UnresolvedPermission element = (UnresolvedPermission)iter
                                                       .next();
                        Permission resolved = element.resolve(target.getClass());
                        if (resolved != null)
                        {
                            if (holder == null)
                            {
                                holder = target.newPermissionCollection();
                                if (holder == null)
                                {
                                    holder = new PermissionsHash();
                                }
                            }
                            holder.add(resolved);
                            iter.remove();
                        }
                    }
                    if (klassMates.size() == 0)
                    {
                        klasses.remove(klass);
                    }
                }
            }
            return(holder);
        }
コード例 #6
0
 /**
  * Resolves all permissions of the same class as the specified target
  * permission and adds them to the specified collection. If passed
  * collection is {@code null} and some unresolved permissions were resolved,
  * an appropriate new collection is instantiated and used. All resolved
  * permissions are removed from this unresolved collection, and collection
  * with resolved ones is returned.
  *
  * @param target
  *            a kind of permissions to be resolved.
  * @param holder
  *            an existing collection for storing resolved permissions.
  * @return a collection containing resolved permissions (if any found)
  */
 internal PermissionCollection resolveCollection(Permission target,
                                        PermissionCollection holder)
 {
     String klass = target.getClass().getName();
     if (klasses.containsKey(klass))
     {
         lock (klasses)
         {
             java.util.Collection<Object> klassMates = (java.util.Collection<Object>)klasses.get(klass);
             for (java.util.Iterator<Object> iter = klassMates.iterator(); iter.hasNext(); )
             {
                 UnresolvedPermission element = (UnresolvedPermission)iter
                     .next();
                 Permission resolved = element.resolve(target.getClass());
                 if (resolved != null)
                 {
                     if (holder == null)
                     {
                         holder = target.newPermissionCollection();
                         if (holder == null)
                         {
                             holder = new PermissionsHash();
                         }
                     }
                     holder.add(resolved);
                     iter.remove();
                 }
             }
             if (klassMates.size() == 0)
             {
                 klasses.remove(klass);
             }
         }
     }
     return holder;
 }