Exemple #1
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);
        }
Exemple #2
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);
        }
Exemple #3
0
 /**
  * Indicates whether the specified permission is implied by this permission.
  *
  * @param permission
  *            the permission to check against this permission.
  * @return {@code true} if the specified permission is implied by this
  *         permission, {@code false} otherwise.
  */
 public override bool implies(Permission permission)
 {
     if (permission != null && permission.getClass() == this.getClass())
     {
         String name     = getName();
         String thatName = permission.getName();
         if (this is java.lang.RuntimePermission)
         {
             if (thatName.equals("exitVM"))
             {
                 thatName = "exitVM.*";
             }
             if (name.equals("exitVM"))
             {
                 name = "exitVM.*";
             }
         }
         return(nameImplies(name, thatName));
     }
     return(false);
 }
        /**
         * Adds a permission to the collection. The first added permission must be a
         * subclass of BasicPermission, next permissions must be of the same class
         * as the first one.
         *
         * @see java.security.PermissionCollection#add(java.security.Permission)
         */

        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.IllegalArgumentException("invalid null permission"); //$NON-NLS-1$
            }

            java.lang.Class inClass = permission.getClass();
            if (permClass != null)
            {
                if (permClass != inClass)
                {
                    throw new java.lang.IllegalArgumentException("invalid permission: " + permission);
                }
            }
            else if (!(permission is BasicPermission))
            {
                throw new java.lang.IllegalArgumentException("invalid permission: " + permission);
            }
            else
            {
                // this is the first element provided that another thread did not add
                lock (this) {
                    if (permClass != null && inClass != permClass)
                    {
                        throw new java.lang.IllegalArgumentException("invalid permission: " + permission);
                    }
                    permClass = inClass;
                }
            }

            String name = permission.getName();

            items.put(name, permission);
            allEnabled = allEnabled || (name.length() == 1 && '*' == name.charAt(0));
        }
Exemple #5
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;
            }
        }
Exemple #6
0
 /*
  * Adds an unresolved permission to this {@code
  * UnresolvedPermissionCollection}.
  *
  * @param permission
  *            the permission to be added.
  * @throws SecurityException
  *             if this collection is read only.
  * @throws IllegalArgumentException
  *             if {@code permission} is {@code null} or not an {@code
  *             UnresolvedPermission}.
  */
 public override void add(Permission permission)
 {
     if (isReadOnly())
     {
         throw new java.lang.SecurityException("collection is read-only"); //$NON-NLS-1$
     }
     if (permission == null ||
         permission.getClass() != typeof(UnresolvedPermission).getClass())
     {
         throw new java.lang.IllegalArgumentException("invalid permission: " + permission);
     }
     lock (klasses)
     {
         String klass = permission.getName();
         java.util.Collection <Permission> klassMates = (java.util.Collection <Permission>)klasses.get(klass);
         if (klassMates == null)
         {
             klassMates = new java.util.HashSet <Permission>();
             klasses.put(klass, klassMates);
         }
         klassMates.add(permission);
     }
 }
        /**
         * Indicates whether the argument permission is implied by the receiver.
         *
         * @return boolean {@code true} if the argument permission is implied by the
         *         receiver, and {@code false} if it is not.
         * @param permission
         *            the permission to check.
         * @see Permission
         */
        public override bool implies(Permission permission)
        {
            if (permission == null || permission.getClass() != permClass)
            {
                return(false);
            }
            if (allEnabled)
            {
                return(true);
            }
            String checkName = permission.getName();

            //first check direct coincidence
            if (items.containsKey(checkName))
            {
                return(true);
            }

            //Special treat with RuntimePermission exitVM.*
            if (items.containsKey("exitVM") || items.containsKey("exitVM.*"))
            {
                if (checkName.endsWith("exitVM"))
                {
                    return(true);
                }
                if (checkName.startsWith("exitVM.") && checkName.length() > "exitVM.".length())
                {
                    return(true);
                }
            }


            //now check if there are suitable wildcards
            //suppose we have "a.b.c", let's check "a.b.*" and "a.*"
            char[] name = checkName.toCharArray();
            //I presume that "a.b.*" does not imply "a.b."
            //so the dot at end is ignored
            int pos = name.Length - 2;

            for (; pos >= 0; pos--)
            {
                if (name[pos] == '.')
                {
                    break;
                }
            }
            while (pos >= 0)
            {
                name[pos + 1] = '*';
                if (items.containsKey(new String(name, 0, pos + 2)))
                {
                    return(true);
                }
                for (--pos; pos >= 0; pos--)
                {
                    if (name[pos] == '.')
                    {
                        break;
                    }
                }
            }
            return(false);
        }
Exemple #8
0
 /*
  * Returns true if this collection contains unresolved permissions
  * with the same classname as argument permission.
  */
 internal bool hasUnresolved(Permission permission)
 {
     return(klasses.containsKey(permission.getClass().getName()));
 }