OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [java/] [util/] [PropertyPermissionCollection.java] - Blame information for rev 771

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 771 jeremybenn
/* PropertyPermissionCollection.java -- a collection of PropertyPermissions
2
   Copyright (C) 2002, 2004, 2005  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., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 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
 
39
package java.util;
40
 
41
import java.security.Permission;
42
import java.security.PermissionCollection;
43
 
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.
49
 *
50
 * @author Eric Blake (ebb9@email.byu.edu)
51
 * @status an undocumented class, but this matches Sun's serialization
52
 */
53
class PropertyPermissionCollection extends PermissionCollection
54
{
55
  /**
56
   * Compatible with JDK 1.4.
57
   */
58
  private static final long serialVersionUID = 7015263904581634791L;
59
 
60
  /**
61
   * The permissions.
62
   *
63
   * @serial the table of permissions in the collection
64
   */
65
  private final Hashtable permissions = new Hashtable();
66
 
67
  /**
68
   * A flag to detect if "*" is in the collection.
69
   *
70
   * @serial true if "*" is in the collection
71
   */
72
  private boolean all_allowed;
73
 
74
  /**
75
   * Adds a PropertyPermission to this collection.
76
   *
77
   * @param permission the permission to add
78
   * @throws IllegalArgumentException if permission is not a PropertyPermission
79
   * @throws SecurityException if collection is read-only
80
   */
81
  public void add(Permission permission)
82
  {
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)
93
      {
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");
99
      }
100
    permissions.put(name, pp);
101
  }
102
 
103
  /**
104
   * Returns true if this collection implies the given permission. This even
105
   * returns true for this case:
106
   *
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>
112
   *
113
   * @param permission the permission to check
114
   * @return true if it is implied by this
115
   */
116
  public boolean implies(Permission permission)
117
  {
118
    if (! (permission instanceof PropertyPermission))
119
      return false;
120
    PropertyPermission toImply = (PropertyPermission) permission;
121
    int actions = toImply.actions;
122
 
123
    if (all_allowed)
124
      {
125
        int all_actions = ((PropertyPermission) permissions.get("*")).actions;
126
        actions &= ~all_actions;
127
        if (actions == 0)
128
          return true;
129
      }
130
 
131
    String name = toImply.getName();
132
    if (name.equals("*"))
133
      return false;
134
 
135
    int prefixLength = name.length();
136
    if (name.endsWith("*"))
137
      prefixLength -= 2;
138
 
139
    while (true)
140
      {
141
        PropertyPermission forName =
142
          (PropertyPermission) permissions.get(name);
143
        if (forName != null)
144
          {
145
            actions &= ~forName.actions;
146
            if (actions == 0)
147
              return true;
148
          }
149
 
150
        prefixLength = name.lastIndexOf('.', prefixLength - 1);
151
        if (prefixLength < 0)
152
          return false;
153
        name = name.substring(0, prefixLength + 1) + '*';
154
      }
155
  }
156
 
157
  /**
158
   * Enumerate over the collection.
159
   *
160
   * @return an enumeration of the collection contents
161
   */
162
  public Enumeration elements()
163
  {
164
    return permissions.elements();
165
  }
166
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.