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)
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
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
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. */
41 import java
.security
.Permission
;
42 import java
.security
.PermissionCollection
;
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
56 * Compatible with JDK 1.4.
58 private static final long serialVersionUID
= 7015263904581634791L;
63 * @serial the table of permissions in the collection
65 private final Hashtable permissions
= new Hashtable();
68 * A flag to detect if "*" is in the collection.
70 * @serial true if "*" is in the collection
72 private boolean all_allowed
;
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
)
84 throw new SecurityException("readonly");
85 if (! (permission
instanceof PropertyPermission
))
86 throw new IllegalArgumentException();
87 PropertyPermission pp
= (PropertyPermission
) permission
;
88 String name
= pp
.getName();
91 PropertyPermission old
= (PropertyPermission
) permissions
.get(name
);
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:
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
))
118 PropertyPermission toImply
= (PropertyPermission
) permission
;
119 int actions
= toImply
.actions
;
123 int all_actions
= ((PropertyPermission
) permissions
.get("*")).actions
;
124 actions
&= ~all_actions
;
129 String name
= toImply
.getName();
130 if (name
.equals("*"))
133 int prefixLength
= name
.length();
134 if (name
.endsWith("*"))
139 PropertyPermission forName
=
140 (PropertyPermission
) permissions
.get(name
);
143 actions
&= ~forName
.actions
;
148 prefixLength
= name
.lastIndexOf('.', prefixLength
);
149 if (prefixLength
< 0)
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();