1 /* PropertyPermission.java -- permission to get and set System properties
2 Copyright (C) 1999, 2000, 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.BasicPermission;
43 import java.security.PermissionCollection;
44 import java.io.ObjectStreamField;
45 import java.io.ObjectInputStream;
46 import java.io.ObjectOutputStream;
47 import java.io.IOException;
50 * This class represents the permission to access and modify a property.<br>
52 * The name is the name of the property, e.g. xxx. You can also
53 * use an asterisk "*" as described in BasicPermission.<br>
55 * The action string is a comma-separated list of keywords. There are
56 * two possible actions:
59 * <dd>Allows to read the property via <code>System.getProperty</code>.</dd>
61 * <dd>Allows to write the property via <code>System.setProperty</code>.</dd>
64 * The action string is case insensitive (it is converted to lower case).
67 * @see BasicPermission
68 * @see SecurityManager
69 * @author Jochen Hoenicke
71 * @status updated to 1.4
73 public final class PropertyPermission extends BasicPermission
76 * PropertyPermission uses a more efficient representation than the
77 * serialized form; this documents the difference.
79 * @serialField action String the action string
81 private static final ObjectStreamField[] serialPersistentFields =
83 new ObjectStreamField("action", String.class)
87 * Compatible with JDK 1.2+.
89 private static final long serialVersionUID = 885438825399942851L;
91 /** Permission to read. */
92 private static final int READ = 1;
93 /** Permission to write. */
94 private static final int WRITE = 2;
96 /** The set of actions permitted. */
97 // Package visible for use by PropertyPermissionCollection.
98 transient int actions;
101 * The String forms of the actions permitted.
103 private static final String actionStrings[] =
105 "", "read", "write", "read,write"
109 * Constructs a PropertyPermission with the specified property. Possible
110 * actions are read and write, comma-separated and case-insensitive.
112 * @param name the name of the property
113 * @param actions the action string
114 * @throws NullPointerException if name is null
115 * @throws IllegalArgumentException if name string contains an
116 * illegal wildcard or actions string contains an illegal action
117 * (this includes a null actions string)
119 public PropertyPermission(String name, String actions)
123 throw new IllegalArgumentException();
128 * Parse the action string and convert actions from external to internal
129 * form. This will set the internal actions field.
131 * @param str the action string
132 * @throws IllegalArgumentException if actions string contains an
135 private void setActions(String str)
137 // Initialising the class java.util.Locale ...
138 // tries to initialise the Locale.defaultLocale static
139 // which calls System.getProperty,
140 // which calls SecurityManager.checkPropertiesAccess,
141 // which creates a PropertyPermission with action "read,write",
142 // which calls setActions("read,write").
143 // If we now were to call toLowerCase on 'str',
144 // this would call Locale.getDefault() which returns null
145 // because Locale.defaultLocale hasn't been set yet
146 // then toLowerCase will fail with a null pointer exception.
148 // The solution is to take a punt on 'str' being lower case, and
149 // test accordingly. If that fails, we convert 'str' to lower case
150 // and try the tests again.
151 if ("read".equals(str))
153 else if ("write".equals(str))
155 else if ("read,write".equals(str) || "write,read".equals(str))
156 actions = READ | WRITE;
158 String lstr = str.toLowerCase();
159 if ("read".equals(lstr))
161 else if ("write".equals(lstr))
163 else if ("read,write".equals(lstr) || "write,read".equals(lstr))
164 actions = READ | WRITE;
166 throw new IllegalArgumentException("illegal action " + str);
171 * Reads an object from the stream. This converts the external to the
172 * internal representation.
174 * @param s the stream to read from
175 * @throws IOException if the stream fails
176 * @throws ClassNotFoundException if reserialization fails
178 private void readObject(ObjectInputStream s)
179 throws IOException, ClassNotFoundException
181 ObjectInputStream.GetField fields = s.readFields();
182 setActions((String) fields.get("actions", null));
186 * Writes an object to the stream. This converts the internal to the
187 * external representation.
189 * @param s the stram to write to
190 * @throws IOException if the stream fails
192 private void writeObject(ObjectOutputStream s) throws IOException
194 ObjectOutputStream.PutField fields = s.putFields();
195 fields.put("actions", getActions());
200 * Check if this permission implies p. This returns true iff all of
201 * the following conditions are true:
203 * <li> p is a PropertyPermission </li>
204 * <li> this.getName() implies p.getName(),
205 * e.g. <code>java.*</code> implies <code>java.home</code> </li>
206 * <li> this.getActions is a subset of p.getActions </li>
209 * @param p the permission to check
210 * @return true if this permission implies p
212 public boolean implies(Permission p)
214 // BasicPermission checks for name and type.
215 if (super.implies(p))
217 // We have to check the actions.
218 PropertyPermission pp = (PropertyPermission) p;
219 return (pp.actions & ~actions) == 0;
225 * Check to see whether this object is the same as another
226 * PropertyPermission object; this is true if it has the same name and
229 * @param obj the other object
230 * @return true if the two are equivalent
232 public boolean equals(Object obj)
234 return super.equals(obj) && actions == ((PropertyPermission) obj).actions;
238 * Returns the hash code for this permission. It is equivalent to
239 * <code>getName().hashCode()</code>.
241 * @return the hash code
243 public int hashCode()
245 return super.hashCode();
249 * Returns the action string. Note that this may differ from the string
250 * given at the constructor: The actions are converted to lowercase and
253 * @return one of "read", "write", or "read,write"
255 public String getActions()
257 return actionStrings[actions];
261 * Returns a permission collection suitable to take
262 * PropertyPermission objects.
264 * @return a new empty PermissionCollection
266 public PermissionCollection newPermissionCollection()
268 return new PropertyPermissionCollection();