001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.ObjectStreamField;
024: import java.security.BasicPermission;
025: import java.security.Permission;
026: import java.security.PermissionCollection;
027:
028: import org.apache.harmony.luni.util.Util;
029:
030: /**
031: * PropertyPermission objects represent permission to access system properties.
032: */
033: public final class PropertyPermission extends BasicPermission {
034: private static final long serialVersionUID = 885438825399942851L;
035:
036: transient private boolean read, write;
037:
038: /**
039: * Constructs a new instance of this class.
040: *
041: * @param name
042: * java.lang.String the (possibly wildcarded) name of the
043: * property.
044: * @param actions
045: * java.lang.String the actions which are applicable to it.
046: */
047: public PropertyPermission(String name, String actions) {
048: super (name);
049: decodeActions(actions);
050: }
051:
052: private void decodeActions(String actions) {
053: StringTokenizer tokenizer = new StringTokenizer(Util
054: .toASCIILowerCase(actions), " \t\n\r,"); //$NON-NLS-1$
055: while (tokenizer.hasMoreTokens()) {
056: String token = tokenizer.nextToken();
057: if (token.equals("read")) { //$NON-NLS-1$
058: read = true;
059: } else if (token.equals("write")) { //$NON-NLS-1$
060: write = true;
061: } else {
062: throw new IllegalArgumentException();
063: }
064: }
065: if (!read && !write) {
066: throw new IllegalArgumentException();
067: }
068: }
069:
070: /**
071: * Compares the argument to the receiver, and answers true if they represent
072: * the <em>same</em> object using a class specific comparison. In this
073: * case, the receiver must be for the same property as the argument, and
074: * must have the same actions.
075: *
076: * @param o
077: * the object to compare with this object
078: * @return <code>true</code> if the object is the same as this object
079: * <code>false</code> if it is different from this object
080: * @see #hashCode
081: */
082: @Override
083: public boolean equals(Object o) {
084: if (super .equals(o)) {
085: PropertyPermission pp = (PropertyPermission) o;
086: return read == pp.read && write == pp.write;
087: }
088: return false;
089: }
090:
091: /**
092: * Answers the actions associated with the receiver. The result will be
093: * either "read", "write", or "read,write".
094: *
095: * @return String the actions associated with the receiver.
096: */
097: @Override
098: public String getActions() {
099: return read ? (write ? "read,write" : "read") : "write"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
100: }
101:
102: /**
103: * Answers an integer hash code for the receiver. Any two objects which
104: * answer <code>true</code> when passed to <code>equals</code> must
105: * answer the same value for this method.
106: *
107: * @return the receiver's hash
108: *
109: * @see #equals
110: */
111: @Override
112: public int hashCode() {
113: return super .hashCode();
114: }
115:
116: /**
117: * Indicates whether the argument permission is implied by the receiver.
118: *
119: * @return boolean <code>true</code> if the argument permission is implied
120: * by the receiver, and <code>false</code> if it is not.
121: * @param permission
122: * java.security.Permission the permission to check
123: */
124: @Override
125: public boolean implies(Permission permission) {
126: if (super .implies(permission)) {
127: PropertyPermission pp = (PropertyPermission) permission;
128: return (read || !pp.read) && (write || !pp.write);
129: }
130: return false;
131: }
132:
133: /**
134: * Answers a new PermissionCollection for holding permissions of this class.
135: * Answer null if any permission collection can be used.
136: *
137: * @return a new PermissionCollection or null
138: *
139: * see java.security.BasicPermissionCollection
140: */
141: @Override
142: public PermissionCollection newPermissionCollection() {
143: return new PropertyPermissionCollection();
144: }
145:
146: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
147: "actions", String.class) }; //$NON-NLS-1$
148:
149: private void writeObject(ObjectOutputStream stream)
150: throws IOException {
151: ObjectOutputStream.PutField fields = stream.putFields();
152: fields.put("actions", getActions()); //$NON-NLS-1$
153: stream.writeFields();
154: }
155:
156: private void readObject(ObjectInputStream stream)
157: throws IOException, ClassNotFoundException {
158: ObjectInputStream.GetField fields = stream.readFields();
159: String actions = (String) fields.get("actions", ""); //$NON-NLS-1$ //$NON-NLS-2$
160: decodeActions(actions);
161: }
162: }
|