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: /**
019: * @author Alexey V. Varlamov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.io.IOException;
024: import java.io.Serializable;
025:
026: import org.apache.harmony.security.internal.nls.Messages;
027:
028: /**
029: * Superclass of permissions which have names but no action lists.
030: *
031: */
032:
033: public abstract class BasicPermission extends Permission implements
034: Serializable {
035:
036: private static final long serialVersionUID = 6279438298436773498L;
037:
038: /**
039: * Creates an instance of this class with the given name and action list.
040: *
041: * @param name
042: * String the name of the new permission.
043: */
044: public BasicPermission(String name) {
045: super (name);
046: checkName(name);
047: }
048:
049: /**
050: * Creates an instance of this class with the given name and action list.
051: * The action list is ignored.
052: *
053: * @param name
054: * String the name of the new permission.
055: * @param action
056: * String ignored.
057: */
058: public BasicPermission(String name, String action) {
059: super (name);
060: checkName(name);
061: }
062:
063: /**
064: * Checks name parameter
065: */
066: private final void checkName(String name) {
067: if (name == null) {
068: throw new NullPointerException(Messages
069: .getString("security.28")); //$NON-NLS-1$
070: }
071: if (name.length() == 0) {
072: throw new IllegalArgumentException(Messages
073: .getString("security.29")); //$NON-NLS-1$
074: }
075: }
076:
077: /**
078: * Compares the argument to the receiver, and answers true if they represent
079: * the <em>same</em> object using a class specific comparison. In this
080: * case, the receiver and the object must have the same class and name.
081: *
082: * @param obj
083: * the object to compare with this object
084: * @return <code>true</code> if the object is the same as this object
085: * <code>false</code> if it is different from this object
086: * @see #hashCode
087: */
088: public boolean equals(Object obj) {
089: if (obj == this ) {
090: return true;
091: }
092:
093: if (obj != null && obj.getClass() == this .getClass()) {
094: return this .getName().equals(((Permission) obj).getName());
095: }
096: return false;
097: }
098:
099: /**
100: * Answers an integer hash code for the receiver. Any two objects which
101: * answer <code>true</code> when passed to <code>equals</code> must
102: * answer the same value for this method.
103: *
104: * @return int the receiver's hash
105: *
106: * @see #equals
107: */
108: public int hashCode() {
109: return getName().hashCode();
110: }
111:
112: /**
113: * Answers the actions associated with the receiver. BasicPermission objects
114: * have no actions, so answer the empty string.
115: *
116: * @return String the actions associated with the receiver.
117: */
118: public String getActions() {
119: return ""; //$NON-NLS-1$
120: }
121:
122: /**
123: * Indicates whether the argument permission is implied by the receiver.
124: *
125: * @return boolean <code>true</code> if the argument permission is implied
126: * by the receiver, and <code>false</code> if it is not.
127: * @param permission
128: * java.security.Permission the permission to check
129: */
130: public boolean implies(Permission permission) {
131: if (permission != null
132: && permission.getClass() == this .getClass()) {
133: return nameImplies(getName(), permission.getName());
134: }
135: return false;
136: }
137:
138: /**
139: * Checks if <code>thisName</code> implies <code>thatName</code>,
140: * accordingly to hierarchical property naming convention.
141: * It is assumed that names cannot be null or empty.
142: */
143: static boolean nameImplies(String this Name, String thatName) {
144: if (this Name == thatName) {
145: return true;
146: }
147: int end = this Name.length();
148: if (end > thatName.length()) {
149: return false;
150: }
151: if (this Name.charAt(--end) == '*'
152: && (end == 0 || this Name.charAt(end - 1) == '.')) {
153: //wildcard found
154: end--;
155: } else if (end != (thatName.length() - 1)) {
156: //names are not equal
157: return false;
158: }
159: for (int i = end; i >= 0; i--) {
160: if (this Name.charAt(i) != thatName.charAt(i)) {
161: return false;
162: }
163: }
164: return true;
165: }
166:
167: /**
168: * Answers a new PermissionCollection for holding permissions of this class.
169: * Answer null if any permission collection can be used.
170: * <p>
171: * Note: For BasicPermission (and subclasses which do not override this
172: * method), the collection which is returned does <em>not</em> invoke the
173: * .implies method of the permissions which are stored in it when checking
174: * if the collection implies a permission. Instead, it assumes that if the
175: * type of the permission is correct, and the name of the permission is
176: * correct, there is a match.
177: *
178: * @return a new PermissionCollection or null
179: *
180: * @see java.security.BasicPermissionCollection
181: */
182: public PermissionCollection newPermissionCollection() {
183: return new BasicPermissionCollection();
184: }
185:
186: /**
187: * Checks name after default deserialization.
188: */
189: private void readObject(java.io.ObjectInputStream in)
190: throws IOException, ClassNotFoundException {
191: in.defaultReadObject();
192: checkName(this.getName());
193: }
194: }
|