OSDN Git Service

Add license clarification.
[pf3gnuchains/gcc-fork.git] / libjava / java / security / BasicPermission.java
1 /* BasicPermission.java -- Implements a simple named permission.
2    Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package java.security;
39
40 import java.io.Serializable;
41 import java.util.Hashtable;
42 import java.util.Enumeration;
43
44 /**
45  * This class implements a simple model for named permissions without an
46  * associated action list.  That is, either the named permission is granted
47  * or it is not.  
48  * <p>
49  * It also supports trailing wildcards to allow the
50  * easy granting of permissions in a hierarchical fashion.  (For example,
51  * the name "org.gnu.*" might grant all permissions under the "org.gnu"
52  * permissions hierarchy).  The only valid wildcard character is a '*'
53  * which matches anything.  It must be the rightmost element in the
54  * permission name and must follow a '.' or else the Permission name must
55  * consist of only a '*'.  Any other occurrence of a '*' is not valid.
56  * <p>
57  * This class ignores the action list.  Subclasses can choose to implement
58  * actions on top of this class if desired.
59  *
60  * @version 0.1
61  *
62  * @author Aaron M. Renn (arenn@urbanophile.com)
63  */
64 public abstract class BasicPermission extends Permission implements
65   Serializable
66 {
67   /**
68    * This method initializes a new instance of <code>BasicPermission</code>
69    * with the specified name.  If the name contains an illegal wildcard
70    * character, an exception is thrown.
71    *
72    * @param name The name of this permission.
73    *
74    * @exception IllegalArgumentException If the name contains an invalid wildcard character
75    * @exception NullPointerException If the name is null
76    */
77   public BasicPermission(String name) 
78     throws IllegalArgumentException, NullPointerException
79   {
80     super(name);
81
82     if (name.indexOf("*") != -1)
83       {
84         if (!name.endsWith(".*") && !name.equals("*"))
85           throw new IllegalArgumentException("Bad wildcard: " + name);
86
87         if (name.indexOf("*") != name.lastIndexOf("*"))
88           throw new IllegalArgumentException("Bad wildcard: " + name);
89       }
90   }
91
92   /**
93    * This method initializes a new instance of <code>BasicPermission</code>
94    * with the specified name.  If the name contains an illegal wildcard
95    * character, an exception is thrown.  The action list passed to this
96    * form of the constructor is ignored.
97    *
98    * @param name The name of this permission.
99    * @param actions The list of actions for this permission - ignored in this class.
100    *
101    * @exception IllegalArgumentException If the name contains an invalid wildcard character
102    * @exception NullPointerException If the name is null
103    */
104   public BasicPermission(String name, String actions) 
105     throws IllegalArgumentException, NullPointerException
106   {
107     // ignore actions
108     this(name);
109   }
110
111   /**
112    * This method tests to see if the specified permission is implied by 
113    * this permission.  This will be true if the following conditions are met:
114    * <p>
115    * <ul>
116    * <li>The specified object is an instance of <code>BasicPermission</code>, 
117    * or a subclass.
118    * <li>The name of the specified permission is identical to this permission's
119    * name or the name of the specified permission satisfies a wildcard match 
120    * on this permission.
121    * </ul>
122    *
123    * @param perm The <code>Permission</code> object to test against.
124    *
125    * @return <code>true</code> if the specified permission is implied by this one or <code>false</code> otherwise.
126    */
127   public boolean implies(Permission perm)
128   {
129     if (!(perm instanceof BasicPermission))
130       return false;
131
132     String otherName = perm.getName();
133     String name = getName();
134
135     if (name.equals(otherName))
136       return true;
137
138     int last = name.length() - 1;
139     if (name.charAt(last) == '*'
140         && otherName.startsWith(name.substring(0, last)))
141       return true;
142
143     return false;
144   }
145
146   /**
147    * This method tests to see if this object is equal to the specified
148    * <code>Object</code>.  This will be true if and only if the specified
149    * object meets the following conditions:
150    * <p>
151    * <ul>
152    * <li>It is an instance of <code>BasicPermission</code>, or a subclass.
153    * <li>It has the same name as this permission.
154    * </ul>
155    *
156    * @param obj The <code>Object</code> to test for equality against this object
157    *
158    * @return <code>true</code> if the specified <code>Object</code> is equal to this object or <code>false</code> otherwise.
159    */
160   public boolean equals(Object obj)
161   {
162     if (!(obj instanceof BasicPermission))
163       return (false);
164
165     if (!getName().equals(((BasicPermission) obj).getName()))
166       return (false);
167
168     return (true);
169   }
170
171   /**
172    * This method returns a hash code for this permission object.  The hash
173    * code returned is the value returned by calling the <code>hashCode</code>
174    * method on the <code>String</code> that is the name of this permission.
175    *
176    * @return A hash value for this object
177    */
178   public int hashCode()
179   {
180     return (getName().hashCode());
181   }
182
183   /**
184    * This method returns a list of the actions associated with this 
185    * permission.  This method always returns the empty string ("") since
186    * this class ignores actions.
187    *
188    * @return The action list.
189    */
190   public String getActions()
191   {
192     return ("");
193   }
194
195   /**
196    * This method returns an instance of <code>PermissionCollection</code>
197    * suitable for storing <code>BasicPermission</code> objects.  This returns
198    * be a sub class of <code>PermissionCollection</code>
199    * that allows for an efficient and consistent implementation of
200    * the <code>implies</code> method.  The collection doesn't handle subclasses
201    * of BasicPermission correctly; they must override this method. 
202    *
203    * @return A new empty <code>PermissionCollection</code> object.
204    */
205   public PermissionCollection newPermissionCollection()
206   {
207     return new PermissionCollection()
208     {
209       Hashtable permissions = new Hashtable();
210       boolean allAllowed = false;
211
212       public void add(Permission permission)
213       {
214         if (isReadOnly())
215           throw new IllegalStateException("readonly");
216
217         BasicPermission bp = (BasicPermission) permission;
218         String name = bp.getName();
219         if (name.equals("*"))
220           allAllowed = true;
221         permissions.put(name, bp);
222       }
223
224       public boolean implies(Permission permission)
225       {
226         if (!(permission instanceof BasicPermission))
227           return false;
228
229         if (allAllowed)
230           return true;
231
232         BasicPermission toImply = (BasicPermission) permission;
233         String name = toImply.getName();
234         if (name.equals("*"))
235           return false;
236
237         int prefixLength = name.length();
238         if (name.endsWith("*"))
239           prefixLength -= 2;
240
241         while (true)
242           {
243             if (permissions.get(name) != null)
244               return true;
245
246             prefixLength = name.lastIndexOf('.', prefixLength);
247             if (prefixLength < 0)
248               return false;
249             name = name.substring(0, prefixLength + 1) + '*';
250           }
251       }
252
253       public Enumeration elements()
254       {
255         return permissions.elements();
256       }
257     };
258   }
259 }