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