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 org.apache.harmony.security;
022:
023: import java.security.Principal;
024:
025: import org.apache.harmony.security.internal.nls.Messages;
026:
027: /**
028: * Descriptive implementation of Principal, which holds a name and a classname
029: * of unresolved principal. It is used to define an arbitrary Principal which
030: * may be not yet instantiated and authenticated.
031: * <br>
032: * This concept is somewhat similar to UnresolvedPermission. A principal-based
033: * policy may grant permissions depending on what Principals own the current
034: * execution thread. So the policy refers to this model definition of
035: * acceptable principal and compares it with the actual principal.
036: *
037: * @see org.apache.harmony.security.PolicyEntry
038: * @see org.apache.harmony.security.DefaultPolicy
039: */
040: public final class UnresolvedPrincipal implements Principal {
041:
042: /**
043: * Wildcard value denotes any class and/or any name.
044: */
045: public static final String WILDCARD = DefaultPolicyScanner.PrincipalEntry.WILDCARD;
046:
047: // Class name
048: private final String klass;
049:
050: // Principal name
051: private final String name;
052:
053: /**
054: * Constructs a a new definition of a Principal with specified
055: * parameters.
056: * @param klass fully qualified class name, may be wildcard
057: * @param name name of principal, may be wildcard
058: * @throws IllegalArgumentException if <code>klass</code> value
059: * is <code>null </code> or is empty string
060: */
061: public UnresolvedPrincipal(String klass, String name) {
062: if (klass == null || klass.length() == 0) {
063: throw new IllegalArgumentException(Messages
064: .getString("security.91")); //$NON-NLS-1$
065: }
066:
067: this .klass = klass;
068: this .name = name;
069: }
070:
071: /**
072: * Returns name of a modeled Principal, or wildcard
073: * if any name is acceptable.
074: */
075: public String getName() {
076: return name;
077: }
078:
079: /**
080: * Returns fully qualified class name of a modeled Principal,
081: * or wildcard if any class is acceptable.
082: */
083: public String getClassName() {
084: return klass;
085: }
086:
087: /**
088: * Returns <code>true</code> if compared object is a Principal
089: * matching this definition, or if it is an UnresolvedPrincipal,
090: * which defines the same Principal; <code>false</code> otherwise.
091: */
092: public boolean equals(Object that) {
093: if (that instanceof UnresolvedPrincipal) {
094: UnresolvedPrincipal up = (UnresolvedPrincipal) that;
095: return klass.equals(up.klass)
096: && (name == null ? up.name == null : name
097: .equals(up.name));
098: }
099: if (that instanceof Principal) {
100: return implies((Principal) that);
101: }
102: return false;
103: }
104:
105: /**
106: * Returns <code>true</code> if compared object is a Principal
107: * exactly matching this definition. Namely, if the fully qualified name
108: * of class of passed Principal is equal to the class name value
109: * of this definition and the name of passed Principal is equal to
110: * the name value of this definition, or if this definition allows
111: * any class or name, respectively.
112: * Otherwise returns <code>false</code> .
113: */
114: public boolean implies(Principal another) {
115: return (another != null)
116: && (WILDCARD.equals(klass) || klass.equals(another
117: .getClass().getName())
118: && (WILDCARD.equals(name) || (name == null ? another
119: .getName() == null
120: : name.equals(another.getName()))));
121: }
122:
123: /**
124: * Returns the hash code value for this object.
125: */
126: public int hashCode() {
127: int hash = 0;
128: if (name != null) {
129: hash ^= name.hashCode();
130: }
131: if (klass != null) {
132: hash ^= klass.hashCode();
133: }
134: return hash;
135: }
136:
137: /**
138: * Returns a string describing this model of Principal.
139: * The format is 'Principal classname "name"'.
140: */
141: public String toString() {
142: return "Principal " + klass + " \"" + name + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
143: }
144: }
|