1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Aleksei Y. Semenov 20* @version $Revision$ 21*/ 22 23package org.apache.harmony.security; 24 25import java.security.Identity; 26import java.security.IdentityScope; 27import java.security.KeyManagementException; 28import java.security.PublicKey; 29import java.util.Enumeration; 30import java.util.Hashtable; 31 32/** 33 * @see java.security.IdentityScope 34 */ 35 36public class SystemScope extends IdentityScope { 37 38 /** 39 * @serial 40 */ 41 private static final long serialVersionUID = -4810285697932522607L; 42 43 // Identities hash: key is the identity name 44 private Hashtable names = new Hashtable(); 45 46 // Identities hash: key is the public key 47 private Hashtable keys = new Hashtable(); 48 49 /** 50 * @see java.security.IdentityScope#IdentityScope() 51 */ 52 public SystemScope() { 53 } 54 55 /** 56 * @see java.security.IdentityScope#IdentityScope(String) 57 */ 58 public SystemScope(String name) { 59 super(name); 60 } 61 62 /** 63 * @see java.security.IdentityScope#IdentityScope(String, IdentityScope) 64 */ 65 public SystemScope(String name, IdentityScope scope) 66 throws KeyManagementException { 67 super(name, scope); 68 } 69 70 /** 71 * @see java.security.IdentityScope#size() 72 */ 73 public int size() { 74 return names.size(); 75 } 76 77 /** 78 * @see java.security.IdentityScope#getIdentity(java.lang.String) 79 */ 80 public synchronized Identity getIdentity(String name) { 81 if (name == null) { 82 throw new NullPointerException("name == null"); 83 } 84 return (Identity) names.get(name); 85 } 86 87 /** 88 * @see java.security.IdentityScope#getIdentity(java.security.PublicKey) 89 */ 90 public synchronized Identity getIdentity(PublicKey key) { 91 if (key == null) { 92 return null; 93 } 94 return (Identity) keys.get(key); 95 } 96 97 /** 98 * @see java.security.IdentityScope#addIdentity(java.security.Identity) 99 */ 100 public synchronized void addIdentity(Identity identity) throws KeyManagementException { 101 if (identity == null) { 102 throw new NullPointerException("identity == null"); 103 } 104 105 String name = identity.getName(); 106 if (names.containsKey(name)) { 107 throw new KeyManagementException("name '" + name + "' is already used"); 108 } 109 110 PublicKey key = identity.getPublicKey(); 111 if (key != null && keys.containsKey(key)) { 112 throw new KeyManagementException("key '" + key + "' is already used"); 113 } 114 115 names.put(name, identity); 116 if (key != null) { 117 keys.put(key, identity); 118 } 119 } 120 121 /** 122 * @see java.security.IdentityScope#removeIdentity(java.security.Identity) 123 */ 124 public synchronized void removeIdentity(Identity identity) 125 throws KeyManagementException { 126 127 //Exception caught = null; 128 if (identity == null) { 129 throw new NullPointerException("identity == null"); 130 } 131 132 String name = identity.getName(); 133 if (name == null) { 134 throw new NullPointerException("name == null"); 135 } 136 137 boolean contains = names.containsKey(name); 138 names.remove(name); 139 140 PublicKey key = identity.getPublicKey(); 141 142 if (key != null) { 143 contains = contains || keys.containsKey(key); 144 keys.remove(key); 145 } 146 147 if (!contains) { 148 throw new KeyManagementException("identity not found"); 149 } 150 } 151 152 /** 153 * @see java.security.IdentityScope#identities() 154 */ 155 public Enumeration identities() { 156 return names.elements(); 157 } 158} 159