1. /*
  2. * @(#)IdentityScope.java 1.54 04/05/18
  3. *
  4. * Copyright 2004 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.54 04/05/18
  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. @Deprecated
  45. public abstract
  46. class IdentityScope extends Identity {
  47. private static final long serialVersionUID = -2337346281189773310L;
  48. /* The system's scope */
  49. private static IdentityScope scope;
  50. // initialize the system scope
  51. private static void initializeSystemScope() {
  52. String classname = (String) AccessController.doPrivileged(
  53. new PrivilegedAction() {
  54. public Object run() {
  55. return Security.getProperty("system.scope");
  56. }
  57. });
  58. if (classname == null) {
  59. return;
  60. } else {
  61. try {
  62. Class.forName(classname);
  63. } catch (ClassNotFoundException e) {
  64. //Security.error("unable to establish a system scope from " +
  65. // classname);
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. /**
  71. * This constructor is used for serialization only and should not
  72. * be used by subclasses.
  73. */
  74. protected IdentityScope() {
  75. this("restoring...");
  76. }
  77. /**
  78. * Constructs a new identity scope with the specified name.
  79. *
  80. * @param name the scope name.
  81. */
  82. public IdentityScope(String name) {
  83. super(name);
  84. }
  85. /**
  86. * Constructs a new identity scope with the specified name and scope.
  87. *
  88. * @param name the scope name.
  89. * @param scope the scope for the new identity scope.
  90. *
  91. * @exception KeyManagementException if there is already an identity
  92. * with the same name in the scope.
  93. */
  94. public IdentityScope(String name, IdentityScope scope)
  95. throws KeyManagementException {
  96. super(name, scope);
  97. }
  98. /**
  99. * Returns the system's identity scope.
  100. *
  101. * @return the system's identity scope.
  102. *
  103. * @see #setSystemScope
  104. */
  105. public static IdentityScope getSystemScope() {
  106. if (scope == null) {
  107. initializeSystemScope();
  108. }
  109. return scope;
  110. }
  111. /**
  112. * Sets the system's identity scope.
  113. *
  114. * <p>First, if there is a security manager, its
  115. * <code>checkSecurityAccess</code>
  116. * method is called with <code>"setSystemScope"</code>
  117. * as its argument to see if it's ok to set the identity scope.
  118. *
  119. * @param scope the scope to set.
  120. *
  121. * @exception SecurityException if a security manager exists and its
  122. * <code>checkSecurityAccess</code> method doesn't allow
  123. * setting the identity scope.
  124. *
  125. * @see #getSystemScope
  126. * @see SecurityManager#checkSecurityAccess
  127. */
  128. protected static void setSystemScope(IdentityScope scope) {
  129. check("setSystemScope");
  130. IdentityScope.scope = scope;
  131. }
  132. /**
  133. * Returns the number of identities within this identity scope.
  134. *
  135. * @return the number of identities within this identity scope.
  136. */
  137. public abstract int size();
  138. /**
  139. * Returns the identity in this scope with the specified name (if any).
  140. *
  141. * @param name the name of the identity to be retrieved.
  142. *
  143. * @return the identity named <code>name</code>, or null if there are
  144. * no identities named <code>name</code> in this scope.
  145. */
  146. public abstract Identity getIdentity(String name);
  147. /**
  148. * Retrieves the identity whose name is the same as that of the
  149. * specified principal. (Note: Identity implements Principal.)
  150. *
  151. * @param principal the principal corresponding to the identity
  152. * to be retrieved.
  153. *
  154. * @return the identity whose name is the same as that of the
  155. * principal, or null if there are no identities of the same name
  156. * in this scope.
  157. */
  158. public Identity getIdentity(Principal principal) {
  159. return getIdentity(principal.getName());
  160. }
  161. /**
  162. * Retrieves the identity with the specified public key.
  163. *
  164. * @param key the public key for the identity to be returned.
  165. *
  166. * @return the identity with the given key, or null if there are
  167. * no identities in this scope with that key.
  168. */
  169. public abstract Identity getIdentity(PublicKey key);
  170. /**
  171. * Adds an identity to this identity scope.
  172. *
  173. * @param identity the identity to be added.
  174. *
  175. * @exception KeyManagementException if the identity is not
  176. * valid, a name conflict occurs, another identity has the same
  177. * public key as the identity being added, or another exception
  178. * occurs. */
  179. public abstract void addIdentity(Identity identity)
  180. throws KeyManagementException;
  181. /**
  182. * Removes an identity from this identity scope.
  183. *
  184. * @param identity the identity to be removed.
  185. *
  186. * @exception KeyManagementException if the identity is missing,
  187. * or another exception occurs.
  188. */
  189. public abstract void removeIdentity(Identity identity)
  190. throws KeyManagementException;
  191. /**
  192. * Returns an enumeration of all identities in this identity scope.
  193. *
  194. * @return an enumeration of all identities in this identity scope.
  195. */
  196. public abstract Enumeration<Identity> identities();
  197. /**
  198. * Returns a string representation of this identity scope, including
  199. * its name, its scope name, and the number of identities in this
  200. * identity scope.
  201. *
  202. * @return a string representation of this identity scope.
  203. */
  204. public String toString() {
  205. return super.toString() + "[" + size() + "]";
  206. }
  207. private static void check(String directive) {
  208. SecurityManager security = System.getSecurityManager();
  209. if (security != null) {
  210. security.checkSecurityAccess(directive);
  211. }
  212. }
  213. }