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 Aleksei Y. Semenov
020: * @version $Revision$
021: */package org.apache.harmony.security;
022:
023: import java.security.Identity;
024: import java.security.IdentityScope;
025: import java.security.KeyManagementException;
026: import java.security.PublicKey;
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029:
030: import org.apache.harmony.security.internal.nls.Messages;
031:
032: /**
033: * @see java.security.IdentityScope
034: */
035:
036: public class SystemScope extends IdentityScope {
037:
038: /**
039: * @serial
040: */
041: private static final long serialVersionUID = -4810285697932522607L;
042:
043: // Identities hash: key is the identity name
044: private Hashtable names = new Hashtable();
045:
046: // Identities hash: key is the public key
047: private Hashtable keys = new Hashtable();
048:
049: /**
050: * @see java.security.IdentityScope#IdentityScope()
051: */
052: public SystemScope() {
053: super ();
054: }
055:
056: /**
057: * @see java.security.IdentityScope#IdentityScope(String)
058: */
059: public SystemScope(String name) {
060: super (name);
061: }
062:
063: /**
064: * @see java.security.IdentityScope#IdentityScope(String, IdentityScope)
065: */
066: public SystemScope(String name, IdentityScope scope)
067: throws KeyManagementException {
068: super (name, scope);
069: }
070:
071: /**
072: * @see java.security.IdentityScope#size()
073: */
074: public int size() {
075: return names.size();
076: }
077:
078: /**
079: * @see java.security.IdentityScope#getIdentity(java.lang.String)
080: */
081: public synchronized Identity getIdentity(String name) {
082: if (name == null) {
083: throw new NullPointerException();
084: }
085: return (Identity) names.get(name);
086: }
087:
088: /**
089: * @see java.security.IdentityScope#getIdentity(java.security.PublicKey)
090: */
091: public synchronized Identity getIdentity(PublicKey key) {
092: if (key == null) {
093: return null;
094: }
095: return (Identity) keys.get(key);
096: }
097:
098: /**
099: * @see java.security.IdentityScope#addIdentity(java.security.Identity)
100: */
101: public synchronized void addIdentity(Identity identity)
102: throws KeyManagementException {
103: if (identity == null) {
104: throw new NullPointerException(Messages
105: .getString("security.92")); //$NON-NLS-1$
106: }
107:
108: String name = identity.getName();
109: if (names.containsKey(name)) {
110: throw new KeyManagementException(Messages.getString(
111: "security.93", name)); //$NON-NLS-1$
112: }
113:
114: PublicKey key = identity.getPublicKey();
115: if (key != null && keys.containsKey(key)) {
116: throw new KeyManagementException(Messages.getString(
117: "security.94", key)); //$NON-NLS-1$
118: }
119:
120: names.put(name, identity);
121: if (key != null) {
122: keys.put(key, identity);
123: }
124: }
125:
126: /**
127: * @see java.security.IdentityScope#removeIdentity(java.security.Identity)
128: */
129: public synchronized void removeIdentity(Identity identity)
130: throws KeyManagementException {
131:
132: //Exception caught = null;
133: if (identity == null) {
134: throw new NullPointerException(Messages
135: .getString("security.92")); //$NON-NLS-1$
136: }
137:
138: String name = identity.getName();
139: if (name == null) {
140: throw new NullPointerException(Messages
141: .getString("security.95")); //$NON-NLS-1$
142: }
143:
144: boolean contains = names.containsKey(name);
145: names.remove(name);
146:
147: PublicKey key = identity.getPublicKey();
148:
149: if (key != null) {
150: contains = contains || keys.containsKey(key);
151: keys.remove(key);
152: }
153:
154: if (!contains) {
155: throw new KeyManagementException(Messages
156: .getString("security.96")); //$NON-NLS-1$
157: }
158: }
159:
160: /**
161: * @see java.security.IdentityScope#identities()
162: */
163: public Enumeration identities() {
164: return names.elements();
165: }
166: }
|