2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / PropertyPermissionCollection.java
blob8f9c71d3e590c8265d63feb42e01f8c7fed9166d
1 /* PropertyPermissionCollection.java -- a collection of PropertyPermissions
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package java.util;
41 import java.security.Permission;
42 import java.security.PermissionCollection;
44 /**
45 * This class provides the implementation for
46 * <code>PropertyPermission.newPermissionCollection()</code>. It only accepts
47 * PropertyPermissions, and correctly implements <code>implies</code>. It
48 * is synchronized, as specified in the superclass.
50 * @author Eric Blake <ebb9@email.byu.edu>
51 * @status an undocumented class, but this matches Sun's serialization
53 class PropertyPermissionCollection extends PermissionCollection
55 /**
56 * Compatible with JDK 1.4.
58 private static final long serialVersionUID = 7015263904581634791L;
60 /**
61 * The permissions.
63 * @serial the table of permissions in the collection
65 private final Hashtable permissions = new Hashtable();
67 /**
68 * A flag to detect if "*" is in the collection.
70 * @serial true if "*" is in the collection
72 private boolean all_allowed;
74 /**
75 * Adds a PropertyPermission to this collection.
77 * @param permission the permission to add
78 * @throws IllegalArgumentException if permission is not a PropertyPermission
79 * @throws SecurityException if collection is read-only
81 public void add(Permission permission)
83 if (isReadOnly())
84 throw new SecurityException("readonly");
85 if (! (permission instanceof PropertyPermission))
86 throw new IllegalArgumentException();
87 PropertyPermission pp = (PropertyPermission) permission;
88 String name = pp.getName();
89 if (name.equals("*"))
90 all_allowed = true;
91 PropertyPermission old = (PropertyPermission) permissions.get(name);
92 if (old != null)
94 if ((pp.actions | old.actions) == old.actions)
95 pp = old; // Old implies pp.
96 else if ((pp.actions | old.actions) != pp.actions)
97 // Here pp doesn't imply old; the only case left is both actions.
98 pp = new PropertyPermission(name, "read,write");
100 permissions.put(name, pp);
104 * Returns true if this collection implies the given permission. This even
105 * returns true for this case:
106 * <p>
107 <pre>collection.add(new PropertyPermission("a.*", "read"));
108 collection.add(new PropertyPermission("a.b.*", "write"));
109 collection.implies(new PropertyPermission("a.b.c", "read,write"));</pre>
111 * @param permission the permission to check
112 * @return true if it is implied by this
114 public boolean implies(Permission permission)
116 if (! (permission instanceof PropertyPermission))
117 return false;
118 PropertyPermission toImply = (PropertyPermission) permission;
119 int actions = toImply.actions;
121 if (all_allowed)
123 int all_actions = ((PropertyPermission) permissions.get("*")).actions;
124 actions &= ~all_actions;
125 if (actions == 0)
126 return true;
129 String name = toImply.getName();
130 if (name.equals("*"))
131 return false;
133 int prefixLength = name.length();
134 if (name.endsWith("*"))
135 prefixLength -= 2;
137 while (true)
139 PropertyPermission forName =
140 (PropertyPermission) permissions.get(name);
141 if (forName != null)
143 actions &= ~forName.actions;
144 if (actions == 0)
145 return true;
148 prefixLength = name.lastIndexOf('.', prefixLength);
149 if (prefixLength < 0)
150 return false;
151 name = name.substring(0, prefixLength + 1) + '*';
156 * Enumerate over the collection.
158 * @return an enumeration of the collection contents
160 public Enumeration elements()
162 return permissions.elements();