001    /* AllPermission.java -- Permission to do anything
002       Copyright (C) 1998, 2001, 2002, 2004, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.security;
040    
041    import gnu.java.util.EmptyEnumeration;
042    
043    import java.util.Collections;
044    import java.util.Enumeration;
045    
046    /**
047     * This class is a permission that implies all other permissions.  Granting
048     * this permission effectively grants all others.  Extreme caution should
049     * be exercised in granting this permission.
050     *
051     * @author Aaron M. Renn (arenn@urbanophile.com)
052     * @author Eric Blake (ebb9@email.byu.edu)
053     * @see AccessController
054     * @see Permissions
055     * @see SecurityManager
056     * @since 1.1
057     * @status updated to 1.4
058     */
059    public final class AllPermission extends Permission
060    {
061      /**
062       * Compatible with JDK 1.1+.
063       */
064      private static final long serialVersionUID = -2916474571451318075L;
065    
066      /**
067       * Create a new AllPermission object.
068       */
069      public AllPermission()
070      {
071        super("*");
072      }
073    
074      /**
075       * Create a new AllPermission object. The parameters are ignored, as all
076       * permission implies ALL PERMISSION.
077       *
078       * @param name ignored
079       * @param actions ignored
080       */
081      public AllPermission(String name, String actions)
082      {
083        super("*");
084      }
085    
086      /**
087       * This method always returns <code>true</code> to indicate that this
088       * permission always implies that any other permission is also granted.
089       *
090       * @param perm ignored
091       * @return true, the permission is implied
092       */
093      public boolean implies(Permission perm)
094      {
095        return true;
096      }
097    
098      /**
099       * Checks an object for equality. All AllPermissions are equal.
100       *
101       * @param obj the <code>Object</code> to test for equality
102       */
103      public boolean equals(Object obj)
104      {
105        return obj instanceof AllPermission;
106      }
107    
108      /**
109       * This method returns a hash code for this object. This returns 1.
110       *
111       * @return a hash value for this object
112       */
113      public int hashCode()
114      {
115        return 1;
116      }
117    
118      /**
119       * This method returns the list of actions associated with this object.
120       * This will always be the empty string ("") for this class.
121       *
122       * @return the action list
123       */
124      public String getActions()
125      {
126        return "";
127      }
128    
129      /**
130       * Returns a PermissionCollection which can hold AllPermission.
131       *
132       * @return a permission collection
133       */
134      public PermissionCollection newPermissionCollection()
135      {
136        return new AllPermissionCollection();
137      }
138    
139      /**
140       * Implements AllPermission.newPermissionCollection, and obeys serialization
141       * of JDK.
142       *
143       * @author Eric Blake (ebb9@email.byu.edu)
144       */
145      private static final class AllPermissionCollection extends PermissionCollection
146      {
147        /**
148         * Compatible with JDK 1.1+.
149         */
150        private static final long serialVersionUID = -4023755556366636806L;
151    
152        /**
153         * Whether an AllPermission has been added to the collection.
154         *
155         * @serial if all permission is in the collection yet
156         */
157        private boolean all_allowed;
158    
159        /**
160         * Add an AllPermission.
161         *
162         * @param perm the permission to add
163         * @throws IllegalArgumentException if perm is not an AllPermission
164         * @throws SecurityException if the collection is read-only
165         */
166        public void add(Permission perm)
167        {
168          if (isReadOnly())
169            throw new SecurityException();
170          if (! (perm instanceof AllPermission))
171            throw new IllegalArgumentException();
172          all_allowed = true;
173        }
174    
175        /**
176         * Returns true if this collection implies a permission.
177         *
178         * @param perm the permission to check
179         * @return true if this collection contains an AllPermission
180         */
181        public boolean implies(Permission perm)
182        {
183          return all_allowed;
184        }
185    
186        /**
187         * Returns an enumeration of the elements in the collection.
188         *
189         * @return the elements in the collection
190         */
191        public Enumeration elements()
192        {
193          return all_allowed
194            ? Collections.enumeration(Collections.singleton(new AllPermission()))
195            : EmptyEnumeration.getInstance();
196        }
197      } // class AllPermissionCollection
198    } // class AllPermission