home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / IdentityScope.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.9 KB  |  234 lines

  1. /*
  2.  * @(#)IdentityScope.java    1.38 98/03/18
  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.38 98/03/18
  47.  * @author Benjamin Renaud
  48.  */
  49.  
  50. public abstract 
  51. class IdentityScope extends Identity {
  52.  
  53.     /* The system's scope */
  54.     private static IdentityScope scope;
  55.  
  56.     // initialize the system scope
  57.     private static void initializeSystemScope() {
  58.  
  59.     String classname = null;
  60.  
  61.     try {
  62.         AccessController.beginPrivileged();
  63.         classname = Security.getProperty("system.scope");
  64.     } finally {
  65.         AccessController.endPrivileged();
  66.     }
  67.  
  68.     if (classname == null) {
  69.         return;
  70.  
  71.         } else {
  72.  
  73.         try {
  74.         Class.forName(classname);
  75.         } catch (ClassNotFoundException e) {
  76.         Security.error("unable to establish a system scope from " +
  77.                    classname);
  78.         e.printStackTrace();
  79.         }
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * This constructor is used for serialization only and should not
  85.      * be used by subclasses.
  86.      */
  87.     protected IdentityScope() {
  88.     this("restoring...");
  89.     }
  90.  
  91.     /**
  92.      * Constructs a new identity scope with the specified name.
  93.      *
  94.      * @param name the scope name.
  95.      */
  96.     public IdentityScope(String name) {
  97.     super(name);
  98.     }
  99.  
  100.     /**
  101.      * Constructs a new identity scope with the specified name and scope.
  102.      * 
  103.      * @param name the scope name.
  104.      * @param scope the scope for the new identity scope.
  105.      * 
  106.      * @exception KeyManagementException if there is already an identity 
  107.      * with the same name in the scope.
  108.      */
  109.     public IdentityScope(String name, IdentityScope scope) 
  110.     throws KeyManagementException {
  111.     super(name, scope);
  112.     }
  113.  
  114.     /**
  115.      * Returns the system's identity scope. See the
  116.      * "System Identity Scope" section in the <a href=
  117.      * "../guide/security/CryptoSpec.html#SysIdScope">
  118.      * Java Cryptography Architecture API Specification & Reference </a>.
  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. See the
  132.      * "System Identity Scope" section in the <a href=
  133.      * "../guide/security/CryptoSpec.html#SysIdScope">
  134.      * Java Cryptography Architecture API Specification & Reference </a>.
  135.      *
  136.      * @param scope the scope to set.
  137.      */
  138.     protected static void setSystemScope(IdentityScope scope) {
  139.     check("IdentityScope.setSystemScope");
  140.     IdentityScope.scope = scope;
  141.     }
  142.  
  143.     /**
  144.      * Returns the number of identities within this identity scope.
  145.      * 
  146.      * @return the number of identities within this identity scope.
  147.      */
  148.     public abstract int size();
  149.  
  150.     /**
  151.      * Returns the identity in this scope with the specified name (if any).
  152.      * 
  153.      * @param name the name of the identity to be retrieved.
  154.      * 
  155.      * @return the identity named <code>name</code>, or null if there are
  156.      * no identities named <code>name</code> in this scope.
  157.      */
  158.     public abstract Identity getIdentity(String name);
  159.  
  160.     /**
  161.      * Retrieves the identity whose name is the same as that of the 
  162.      * specified principal. (Note: Identity implements Principal.)
  163.      *
  164.      * @param principal the principal corresponding to the identity
  165.      * to be retrieved.
  166.      * 
  167.      * @return the identity whose name is the same as that of the 
  168.      * principal, or null if there are no identities of the same name 
  169.      * in this scope.
  170.      */
  171.     public Identity getIdentity(Principal principal) {
  172.     return getIdentity(principal.getName());
  173.     }
  174.  
  175.     /**
  176.      * Retrieves the identity with the specified public key.
  177.      *
  178.      * @param key the public key for the identity to be returned.
  179.      *
  180.      * @return the identity with the given key, or null if there are
  181.      * no identities in this scope with that key.
  182.      */
  183.     public abstract Identity getIdentity(PublicKey key);
  184.  
  185.     /**
  186.      * Adds an identity to this identity scope.
  187.      *
  188.      * @param identity the identity to be added.
  189.      *
  190.      * @exception KeyManagementException if the identity is not
  191.      * valid, a name conflict occurs, another identity has the same
  192.      * public key as the identity being added, or another exception
  193.      * occurs. */
  194.     public abstract void addIdentity(Identity identity) 
  195.     throws KeyManagementException;
  196.  
  197.     /**
  198.      * Removes an identity from this identity scope.
  199.      *
  200.      * @param identity the identity to be removed.
  201.      *
  202.      * @exception KeyManagementException if the identity is missing,
  203.      * or another exception occurs.
  204.      */
  205.     public abstract void removeIdentity(Identity identity) 
  206.     throws KeyManagementException;
  207.  
  208.     /**
  209.      * Returns an enumeration of all identities in this identity scope.
  210.      * 
  211.      * @return an enumeration of all identities in this identity scope.
  212.      */
  213.     public abstract Enumeration identities();
  214.  
  215.     /**
  216.      * Returns a string representation of this identity scope, including
  217.      * its name, its scope name, and the number of identities in this
  218.      * identity scope.
  219.      *
  220.      * @return a string representation of this identity scope.
  221.      */
  222.     public String toString() {
  223.     return super.toString() + "[" + size() + "]";
  224.     }
  225.  
  226.     private static void check(String directive) {
  227.     SecurityManager security = System.getSecurityManager();
  228.     if (security != null) {
  229.         security.checkSecurityAccess(directive);
  230.     }
  231.     }
  232.  
  233. }
  234.