1. /*
  2. * @(#)IdentityScope.java 1.45 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.io.Serializable;
  9. import java.util.Enumeration;
  10. import java.util.Properties;
  11. /**
  12. * <p>This class represents a scope for identities. It is an Identity
  13. * itself, and therefore has a name and can have a scope. It can also
  14. * optionally have a public key and associated certificates.
  15. *
  16. * <p>An IdentityScope can contain Identity objects of all kinds, including
  17. * Signers. All types of Identity objects can be retrieved, added, and
  18. * removed using the same methods. Note that it is possible, and in fact
  19. * expected, that different types of identity scopes will
  20. * apply different policies for their various operations on the
  21. * various types of Identities.
  22. *
  23. * <p>There is a one-to-one mapping between keys and identities, and
  24. * there can only be one copy of one key per scope. For example, suppose
  25. * <b>Acme Software, Inc</b> is a software publisher known to a user.
  26. * Suppose it is an Identity, that is, it has a public key, and a set of
  27. * associated certificates. It is named in the scope using the name
  28. * "Acme Software". No other named Identity in the scope has the same
  29. * public key. Of course, none has the same name as well.
  30. *
  31. * @see Identity
  32. * @see Signer
  33. * @see Principal
  34. * @see Key
  35. *
  36. * @version 1.45 01/11/29
  37. * @author Benjamin Renaud
  38. *
  39. * @deprecated This class is no longer used. Its functionality has been
  40. * replaced by <code>java.security.KeyStore</code>, the
  41. * <code>java.security.cert</code> package, and
  42. * <code>java.security.Principal</code>.
  43. */
  44. public abstract
  45. class IdentityScope extends Identity {
  46. /* The system's scope */
  47. private static IdentityScope scope;
  48. // initialize the system scope
  49. private static void initializeSystemScope() {
  50. String classname = (String) AccessController.doPrivileged(
  51. new PrivilegedAction() {
  52. public Object run() {
  53. return Security.getProperty("system.scope");
  54. }
  55. });
  56. if (classname == null) {
  57. return;
  58. } else {
  59. try {
  60. Class.forName(classname);
  61. } catch (ClassNotFoundException e) {
  62. Security.error("unable to establish a system scope from " +
  63. classname);
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. /**
  69. * This constructor is used for serialization only and should not
  70. * be used by subclasses.
  71. */
  72. protected IdentityScope() {
  73. this("restoring...");
  74. }
  75. /**
  76. * Constructs a new identity scope with the specified name.
  77. *
  78. * @param name the scope name.
  79. */
  80. public IdentityScope(String name) {
  81. super(name);
  82. }
  83. /**
  84. * Constructs a new identity scope with the specified name and scope.
  85. *
  86. * @param name the scope name.
  87. * @param scope the scope for the new identity scope.
  88. *
  89. * @exception KeyManagementException if there is already an identity
  90. * with the same name in the scope.
  91. */
  92. public IdentityScope(String name, IdentityScope scope)
  93. throws KeyManagementException {
  94. super(name, scope);
  95. }
  96. /**
  97. * Returns the system's identity scope.
  98. *
  99. * @return the system's identity scope.
  100. */
  101. public static IdentityScope getSystemScope() {
  102. if (scope == null) {
  103. initializeSystemScope();
  104. }
  105. return scope;
  106. }
  107. /**
  108. * Sets the system's identity scope.
  109. *
  110. * <p>First, if there is a security manager, its
  111. * <code>checkSecurityAccess</code>
  112. * method is called with <code>"setSystemScope"</code>
  113. * as its argument to see if it's ok to set the identity scope.
  114. *
  115. * @param scope the scope to set.
  116. *
  117. * @exception SecurityException if a security manager exists and its
  118. * <code>checkSecurityAccess</code> method doesn't allow
  119. * setting the identity scope.
  120. *
  121. * @see SecurityManager#checkSecurityAccess
  122. */
  123. protected static void setSystemScope(IdentityScope scope) {
  124. check("setSystemScope");
  125. IdentityScope.scope = scope;
  126. }
  127. /**
  128. * Returns the number of identities within this identity scope.
  129. *
  130. * @return the number of identities within this identity scope.
  131. */
  132. public abstract int size();
  133. /**
  134. * Returns the identity in this scope with the specified name (if any).
  135. *
  136. * @param name the name of the identity to be retrieved.
  137. *
  138. * @return the identity named <code>name</code>, or null if there are
  139. * no identities named <code>name</code> in this scope.
  140. */
  141. public abstract Identity getIdentity(String name);
  142. /**
  143. * Retrieves the identity whose name is the same as that of the
  144. * specified principal. (Note: Identity implements Principal.)
  145. *
  146. * @param principal the principal corresponding to the identity
  147. * to be retrieved.
  148. *
  149. * @return the identity whose name is the same as that of the
  150. * principal, or null if there are no identities of the same name
  151. * in this scope.
  152. */
  153. public Identity getIdentity(Principal principal) {
  154. return getIdentity(principal.getName());
  155. }
  156. /**
  157. * Retrieves the identity with the specified public key.
  158. *
  159. * @param key the public key for the identity to be returned.
  160. *
  161. * @return the identity with the given key, or null if there are
  162. * no identities in this scope with that key.
  163. */
  164. public abstract Identity getIdentity(PublicKey key);
  165. /**
  166. * Adds an identity to this identity scope.
  167. *
  168. * @param identity the identity to be added.
  169. *
  170. * @exception KeyManagementException if the identity is not
  171. * valid, a name conflict occurs, another identity has the same
  172. * public key as the identity being added, or another exception
  173. * occurs. */
  174. public abstract void addIdentity(Identity identity)
  175. throws KeyManagementException;
  176. /**
  177. * Removes an identity from this identity scope.
  178. *
  179. * @param identity the identity to be removed.
  180. *
  181. * @exception KeyManagementException if the identity is missing,
  182. * or another exception occurs.
  183. */
  184. public abstract void removeIdentity(Identity identity)
  185. throws KeyManagementException;
  186. /**
  187. * Returns an enumeration of all identities in this identity scope.
  188. *
  189. * @return an enumeration of all identities in this identity scope.
  190. */
  191. public abstract Enumeration identities();
  192. /**
  193. * Returns a string representation of this identity scope, including
  194. * its name, its scope name, and the number of identities in this
  195. * identity scope.
  196. *
  197. * @return a string representation of this identity scope.
  198. */
  199. public String toString() {
  200. return super.toString() + "[" + size() + "]";
  201. }
  202. private static void check(String directive) {
  203. SecurityManager security = System.getSecurityManager();
  204. if (security != null) {
  205. security.checkSecurityAccess(directive);
  206. }
  207. }
  208. }