home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / IdentityScope.java < prev    next >
Text File  |  1997-05-20  |  7KB  |  225 lines

  1. /*
  2.  * @(#)IdentityScope.java    1.31 97/01/27
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.security;
  24.  
  25. import java.io.Serializable;
  26. import java.util.Enumeration;
  27. import java.util.Properties;
  28.  
  29. /** 
  30.  * <p>This class represents a scope for identities. It is an Identity 
  31.  * itself, and therefore has a name and can have a scope. It can also 
  32.  * optionally have a public key and associated certificates.
  33.  *
  34.  * <p>An IdentityScope can contain Identity objects of all kinds, including
  35.  * Signers. All types of Identity objects can be retrieved, added, and 
  36.  * removed using the same methods. Note that it is possible, and in fact
  37.  * expected, that different types of identity scopes will
  38.  * apply different policies for their various operations on the
  39.  * various types of Identities.
  40.  *
  41.  * <p>There is a one-to-one mapping between keys and identities, and 
  42.  * there can only be one copy of one key per scope. For example, suppose
  43.  * <b>Acme Software, Inc</b> is a software publisher known to a user.
  44.  * Suppose it is an Identity, that is, it has a public key, and a set of
  45.  * associated certificates. It is named in the scope using the name 
  46.  * "Acme Software". No other named Identity in the scope has the same 
  47.  * public  key. Of course, none has the same name as well.
  48.  *
  49.  * @see Identity
  50.  * @see Signer
  51.  * @see Principal
  52.  * @see Key
  53.  *
  54.  * @version     1.31, 01/27/97
  55.  * @author Benjamin Renaud */
  56. public abstract 
  57. class IdentityScope extends Identity {
  58.  
  59.     /* The system's scope */
  60.     private static IdentityScope scope;
  61.  
  62.     // initialize the system scope
  63.     private static void initializeSystemScope() {
  64.  
  65.     String classname = Security.getProperty("system.scope");
  66.  
  67.     if (classname == null) {
  68.         return;
  69.  
  70.         } else {
  71.  
  72.         try {
  73.         Class.forName(classname);
  74.         } catch (ClassNotFoundException e) {
  75.         Security.error("unable to establish a system scope from " +
  76.                    classname);
  77.         e.printStackTrace();
  78.         }
  79.     }
  80.     }
  81.  
  82.     /**
  83.      * This constructor is used for serialization only and should not
  84.      * be used by subclasses.
  85.      */
  86.     protected IdentityScope() {
  87.     this("restoring...");
  88.     }
  89.  
  90.     /**
  91.      * Constructs a new identity scope with the specified name.
  92.      *
  93.      * @param name the scope name.
  94.      */
  95.     public IdentityScope(String name) {
  96.     super(name);
  97.     }
  98.  
  99.     /**
  100.      * Constructs a new identity scope with the specified name and scope.
  101.      * 
  102.      * @param name the scope name.
  103.      * @param scope the scope for the new identity scope.
  104.      * 
  105.      * @exception KeyManagementException if there is already an identity 
  106.      * with the same name in the scope.
  107.      */
  108.     public IdentityScope(String name, IdentityScope scope) 
  109.     throws KeyManagementException {
  110.     super(name, scope);
  111.     }
  112.  
  113.     /**
  114.      * Returns the system's identity scope. See the
  115.      * "System Identity Scope" section in the <a href=
  116.      * "../guide/security/CryptoSpec.html#SysIdScope">
  117.      * Java Cryptography Architecture API Specification & Reference </a>.
  118.      * 
  119.      * @return the system's identity scope.
  120.      */
  121.     public static IdentityScope getSystemScope() {
  122.     if (scope == null) {
  123.         initializeSystemScope();
  124.     }
  125.     return scope;
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Sets the system's identity scope. See the
  131.      * "System Identity Scope" section in the <a href=
  132.      * "../guide/security/CryptoSpec.html#SysIdScope">
  133.      * Java Cryptography Architecture API Specification & Reference </a>.
  134.      *
  135.      * @param scope the scope to set.
  136.      */
  137.     protected static void setSystemScope(IdentityScope scope) {
  138.     staticCheck("set.system.scope");
  139.     IdentityScope.scope = scope;
  140.     }
  141.  
  142.     /**
  143.      * Returns the number of identities within this identity scope.
  144.      * 
  145.      * @return the number of identities within this identity scope.
  146.      */
  147.     public abstract int size();
  148.  
  149.     /**
  150.      * Returns the identity in this scope with the specified name (if any).
  151.      * 
  152.      * @param name the name of the identity to be retrieved.
  153.      * 
  154.      * @return the identity named <code>name</code>, or null if there are
  155.      * no identities named <code>name</code> in this scope.
  156.      */
  157.     public abstract Identity getIdentity(String name);
  158.  
  159.     /**
  160.      * Retrieves the identity whose name is the same as that of the 
  161.      * specified principal. (Note: Identity implements Principal.)
  162.      *
  163.      * @param principal the principal corresponding to the identity
  164.      * to be retrieved.
  165.      * 
  166.      * @return the identity whose name is the same as that of the 
  167.      * principal, or null if there are no identities of the same name 
  168.      * in this scope.
  169.      */
  170.     public Identity getIdentity(Principal principal) {
  171.     return getIdentity(principal.getName());
  172.     }
  173.  
  174.     /**
  175.      * Retrieves the identity with the specified public key.
  176.      *
  177.      * @param key the public key for the identity to be returned.
  178.      *
  179.      * @return the identity with the given key, or null if there are
  180.      * no identities in this scope with that key.
  181.      */
  182.     public abstract Identity getIdentity(PublicKey key);
  183.  
  184.     /**
  185.      * Adds an identity to this identity scope.
  186.      *
  187.      * @param identity the identity to be added.
  188.      *
  189.      * @exception KeyManagementException if the identity is not
  190.      * valid, a name conflict occurs, another identity has the same
  191.      * public key as the identity being added, or another exception
  192.      * occurs. */
  193.     public abstract void addIdentity(Identity identity) 
  194.     throws KeyManagementException;
  195.  
  196.     /**
  197.      * Removes an identity from this identity scope.
  198.      *
  199.      * @param identity the identity to be removed.
  200.      *
  201.      * @exception KeyManagementException if the identity is missing,
  202.      * or another exception occurs.
  203.      */
  204.     public abstract void removeIdentity(Identity identity) 
  205.     throws KeyManagementException;
  206.  
  207.     /**
  208.      * Returns an enumeration of all identities in this identity scope.
  209.      * 
  210.      * @return an enumeration of all identities in this identity scope.
  211.      */
  212.     public abstract Enumeration identities();
  213.  
  214.     /**
  215.      * Returns a string representation of this identity scope, including
  216.      * its name, its scope name, and the number of identities in this
  217.      * identity scope.
  218.      *
  219.      * @return a string representation of this identity scope.
  220.      */
  221.     public String toString() {
  222.     return super.toString() + "[" + size() + "]";
  223.     }
  224. }
  225.