home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / IdentityScope.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.2 KB  |  242 lines  |  [TEXT/CWIE]

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