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 Alexander V. Astapchuk
020: * @version $Revision$
021: */package org.apache.harmony.auth;
022:
023: import java.security.Principal;
024: import java.io.Serializable;
025:
026: import org.apache.harmony.auth.internal.nls.Messages;
027:
028: /**
029: * A Principal class which serves as a base class for many SID-based
030: * principals.
031: */
032: public class NTSid implements Serializable, Principal {
033:
034: private static final long serialVersionUID = 5589132933506948177L;
035:
036: // SID
037: private String sid;
038:
039: // Name of the object whose SID is kept here
040: private String name;
041:
042: // Domain of the object
043: private String domain;
044:
045: /**
046: * A constructor which takes object's SID as its only argument.
047: * @param sid SID
048: */
049: public NTSid(String sid) {
050: if (sid == null) {
051: throw new NullPointerException(Messages
052: .getString("auth.01")); //$NON-NLS-1$
053: }
054: if (sid.length() == 0) {
055: throw new IllegalArgumentException(Messages
056: .getString("auth.02")); //$NON-NLS-1$
057: }
058: this .sid = sid;
059: }
060:
061: /**
062: * A constructor which takes an extended set of information - object's SID,
063: * its name and name of its domain
064: * @param sid SID
065: * @param objName name of the object whose SID is stored
066: * @param objDomain name of the domain the object belongs to
067: */
068: public NTSid(String sid, String objName, String objDomain) {
069: this (sid);
070: name = objName;
071: domain = objDomain;
072: }
073:
074: /**
075: * Returns object's SID. Same as {@link #getSid()}.
076: */
077: public String getName() {
078: return sid;
079: }
080:
081: /**
082: * Returns object's SID.
083: */
084: public String getSid() {
085: return sid;
086: }
087:
088: /**
089: * Returns name of the object whose SID is stored in this NTSid.
090: */
091: public String getObjectName() {
092: return name;
093: }
094:
095: /**
096: * Returns name of the object's domain.
097: */
098: public String getObjectDomain() {
099: return domain;
100: }
101:
102: /**
103: * Returns String representation of this object.
104: */
105: @Override
106: public String toString() {
107: String str = getClass().getName();
108: int dot = str.lastIndexOf('.');
109: str = str.substring(dot + 1) + ": "; //$NON-NLS-1$
110: str += name + '@' + domain;
111: str += "; SID=" + sid; //$NON-NLS-1$
112: return str;
113: }
114:
115: /**
116: * Tests two objects for equality.<br>
117: * Two objects are considered equal if they both represent
118: * NTSid and they both have the same sid value.
119: */
120: @Override
121: public boolean equals(Object obj) {
122: if (obj == this ) {
123: return true;
124: }
125: if (obj instanceof NTSid) {
126: return sid.equals(((NTSid) obj).sid);
127: }
128: return false;
129: }
130:
131: /**
132: * Returns hashCode for this object.
133: */
134: @Override
135: public int hashCode() {
136: return sid.hashCode();
137: }
138: }
|