Пример #1
0
        /// <summary>
        /// Gets the PermissionCollection in this Permissions object for
        /// permissions whose type is the same as that of <i>p</i>.
        /// For example, if <i>p</i> is a FilePermission,
        /// the FilePermissionCollection
        /// stored in this Permissions object will be returned.
        ///
        /// If createEmpty is true,
        /// this method creates a new PermissionCollection object for the specified
        /// type of permission objects if one does not yet exist.
        /// To do so, it first calls the {@code newPermissionCollection} method
        /// on <i>p</i>.  Subclasses of class Permission
        /// override that method if they need to store their permissions in a
        /// particular PermissionCollection object in order to provide the
        /// correct semantics when the {@code PermissionCollection.implies}
        /// method is called.
        /// If the call returns a PermissionCollection, that collection is stored
        /// in this Permissions object. If the call returns null and createEmpty
        /// is true, then
        /// this method instantiates and stores a default PermissionCollection
        /// that uses a hashtable to store its permission objects.
        ///
        /// createEmpty is ignored when creating empty PermissionCollection
        /// for unresolved permissions because of the overhead of determining the
        /// PermissionCollection to use.
        ///
        /// createEmpty should be set to false when this method is invoked from
        /// implies() because it incurs the additional overhead of creating and
        /// adding an empty PermissionCollection that will just return false.
        /// It should be set to true when invoked from add().
        /// </summary>
        private PermissionCollection GetPermissionCollection(Permission p, bool createEmpty)
        {
            Class c = p.GetType();

            PermissionCollection pc = PermsMap[c];

            if (!HasUnresolved && !createEmpty)
            {
                return(pc);
            }
            else if (pc == null)
            {
                // Check for unresolved permissions
                pc = (HasUnresolved ? GetUnresolvedPermissions(p) : null);

                // if still null, create a new collection
                if (pc == null && createEmpty)
                {
                    pc = p.NewPermissionCollection();

                    // still no PermissionCollection?
                    // We'll give them a PermissionsHash.
                    if (pc == null)
                    {
                        pc = new PermissionsHash();
                    }
                }

                if (pc != null)
                {
                    PermsMap[c] = pc;
                }
            }
            return(pc);
        }
Пример #2
0
        /// <summary>
        /// Resolves any unresolved permissions of type p.
        /// </summary>
        /// <param name="p"> the type of unresolved permission to resolve
        /// </param>
        /// <returns> PermissionCollection containing the unresolved permissions,
        ///  or null if there were no unresolved permissions of type p.
        ///  </returns>
        private PermissionCollection GetUnresolvedPermissions(Permission p)
        {
            // Called from within synchronized method so permsMap doesn't need lock

            UnresolvedPermissionCollection uc = (UnresolvedPermissionCollection)PermsMap[typeof(UnresolvedPermission)];

            // we have no unresolved permissions if uc is null
            if (uc == null)
            {
                return(null);
            }

            IList <UnresolvedPermission> unresolvedPerms = uc.GetUnresolvedPermissions(p);

            // we have no unresolved permissions of this type if unresolvedPerms is null
            if (unresolvedPerms == null)
            {
                return(null);
            }

            java.security.cert.Certificate[] certs = null;

            Object[] signers = p.GetType().Signers;

            int n = 0;

            if (signers != null)
            {
                for (int j = 0; j < signers.Length; j++)
                {
                    if (signers[j] is java.security.cert.Certificate)
                    {
                        n++;
                    }
                }
                certs = new java.security.cert.Certificate[n];
                n     = 0;
                for (int j = 0; j < signers.Length; j++)
                {
                    if (signers[j] is java.security.cert.Certificate)
                    {
                        certs[n++] = (java.security.cert.Certificate)signers[j];
                    }
                }
            }

            PermissionCollection pc = null;

            lock (unresolvedPerms)
            {
                int len = unresolvedPerms.Count;
                for (int i = 0; i < len; i++)
                {
                    UnresolvedPermission up   = unresolvedPerms[i];
                    Permission           perm = up.Resolve(p, certs);
                    if (perm != null)
                    {
                        if (pc == null)
                        {
                            pc = p.NewPermissionCollection();
                            if (pc == null)
                            {
                                pc = new PermissionsHash();
                            }
                        }
                        pc.Add(perm);
                    }
                }
            }
            return(pc);
        }