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.tests.support; 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 * Provides full functionality. Currently used for testing only. 34 * 35 * @see java.security.IdentityScope 36 */ 37 38public class SystemScope extends IdentityScope { 39 40 /** 41 * @serial 42 */ 43 private static final long serialVersionUID = -4810285697932522607L; 44 45 // Identities hash: key is the identity name 46 private Hashtable names = new Hashtable(); 47 48 // Identities hash: key is the public key 49 private Hashtable keys = new Hashtable(); 50 51 /** 52 * @see java.security.IdentityScope#IdentityScope() 53 */ 54 public SystemScope() { 55 } 56 57 /** 58 * @see java.security.IdentityScope#IdentityScope(String) 59 */ 60 public SystemScope(String name) { 61 super(name); 62 } 63 64 /** 65 * @see java.security.IdentityScope#IdentityScope(String, IdentityScope) 66 */ 67 public SystemScope(String name, IdentityScope scope) 68 throws KeyManagementException { 69 super(name, scope); 70 } 71 72 /** 73 * @see java.security.IdentityScope#size() 74 */ 75 public int size() { 76 return names.size(); 77 } 78 79 /** 80 * @see java.security.IdentityScope#getIdentity(java.lang.String) 81 */ 82 public synchronized Identity getIdentity(String name) { 83 if (name == null) { 84 throw new NullPointerException("name == null"); 85 } 86 return (Identity) names.get(name); 87 } 88 89 /** 90 * @see java.security.IdentityScope#getIdentity(java.security.PublicKey) 91 */ 92 public synchronized Identity getIdentity(PublicKey key) { 93 if (key == null) { 94 return null; 95 } 96 return (Identity) keys.get(key); 97 } 98 99 /** 100 * @see java.security.IdentityScope#addIdentity(java.security.Identity) 101 */ 102 public synchronized void addIdentity(Identity identity) throws KeyManagementException { 103 if (identity == null) { 104 throw new NullPointerException("identity == null"); 105 } 106 107 String name = identity.getName(); 108 if (names.containsKey(name)) { 109 throw new KeyManagementException("name '" + name + "' is already used"); 110 } 111 112 PublicKey key = identity.getPublicKey(); 113 if (key != null && keys.containsKey(key)) { 114 throw new KeyManagementException("key '" + key + "' is already used"); 115 } 116 117 names.put(name, identity); 118 if (key != null) { 119 keys.put(key, identity); 120 } 121 } 122 123 /** 124 * @see java.security.IdentityScope#removeIdentity(java.security.Identity) 125 */ 126 public synchronized void removeIdentity(Identity identity) 127 throws KeyManagementException { 128 129 //Exception caught = null; 130 if (identity == null) { 131 throw new NullPointerException("identity == null"); 132 } 133 134 String name = identity.getName(); 135 if (name == null) { 136 throw new NullPointerException("name == null"); 137 } 138 139 boolean contains = names.containsKey(name); 140 names.remove(name); 141 142 PublicKey key = identity.getPublicKey(); 143 144 if (key != null) { 145 contains = contains || keys.containsKey(key); 146 keys.remove(key); 147 } 148 149 if (!contains) { 150 throw new KeyManagementException("identity not found"); 151 } 152 } 153 154 /** 155 * @see java.security.IdentityScope#identities() 156 */ 157 public Enumeration identities() { 158 return names.elements(); 159 } 160} 161