Fix a bug that broke -freorder-functions
[official-gcc.git] / libjava / classpath / java / util / PropertyPermissionCollection.java
blob768b112256a5c91e9dafa40c7a5fec0a46d165ce
1 /* PropertyPermissionCollection.java -- a collection of PropertyPermissions
2 Copyright (C) 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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:
107 * <pre>
108 * collection.add(new PropertyPermission("a.*", "read"));
109 * collection.add(new PropertyPermission("a.b.*", "write"));
110 * collection.implies(new PropertyPermission("a.b.c", "read,write"));
111 * </pre>
113 * @param permission the permission to check
114 * @return true if it is implied by this
116 public boolean implies(Permission permission)
118 if (! (permission instanceof PropertyPermission))
119 return false;
120 PropertyPermission toImply = (PropertyPermission) permission;
121 int actions = toImply.actions;
123 if (all_allowed)
125 int all_actions = ((PropertyPermission) permissions.get("*")).actions;
126 actions &= ~all_actions;
127 if (actions == 0)
128 return true;
131 String name = toImply.getName();
132 if (name.equals("*"))
133 return false;
135 int prefixLength = name.length();
136 if (name.endsWith("*"))
137 prefixLength -= 2;
139 while (true)
141 PropertyPermission forName =
142 (PropertyPermission) permissions.get(name);
143 if (forName != null)
145 actions &= ~forName.actions;
146 if (actions == 0)
147 return true;
150 prefixLength = name.lastIndexOf('.', prefixLength - 1);
151 if (prefixLength < 0)
152 return false;
153 name = name.substring(0, prefixLength + 1) + '*';
158 * Enumerate over the collection.
160 * @return an enumeration of the collection contents
162 public Enumeration elements()
164 return permissions.elements();