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

  1. /*
  2.  * @(#)Provider.java    1.28 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 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.*;
  18. import java.util.*;
  19.  
  20. /**
  21.  * This class represents a "provider" for the
  22.  * Java Security API.  A provider implements some or all parts of
  23.  * Java Security, including:<ul>
  24.  *
  25.  * <li>Algorithms (such as DSA, RSA, MD5 or SHA-1).
  26.  *
  27.  * <li>Key generation and management facilities (such as for
  28.  * algorithm-specific keys).
  29.  *
  30.  * </ul>
  31.  *
  32.  * <p>Each provider has a name and a version number, and is configured
  33.  * in each runtime it is installed in. 
  34.  * 
  35.  * <p>There is a default provider that comes standard with the JDK. It is
  36.  * called the SUN Provider.
  37.  * 
  38.  * See <a href =
  39.  * "../guide/security/CryptoSpec.html#Provider">The Provider Class</a> 
  40.  * in the "Java Cryptography Architecture API Specification & Reference"
  41.  * for information about how providers work and how to install them.
  42.  * 
  43.  * @version 1.28 98/03/18
  44.  * @author Benjamin Renaud */
  45.  
  46. public abstract class Provider extends Properties {
  47.  
  48.     private String name;
  49.     private String info;
  50.     private double version;
  51.  
  52.     /**
  53.      * Constructs a provider with the specified name, version number,
  54.      * and information.
  55.      *
  56.      * @param name the provider name.
  57.      *
  58.      * @param version the provider version number.
  59.      * 
  60.      * @param info a description of the provider and its services.
  61.      */
  62.     protected Provider(String name, double version, String info) {
  63.     this.name = name;
  64.     this.version = version;
  65.     this.info = info;
  66.     }
  67.  
  68.     /**
  69.      * Constructs a provider with the specified name. Assigns it
  70.      * version 1.0.
  71.      *
  72.      * @param name the provider name.  
  73.      */
  74.     Provider(String name) {
  75.     this(name, 1.0, "no information available");
  76.     }
  77.  
  78.     /**
  79.      * Returns the name of this provider.     
  80.      * 
  81.      * @return the name of this provider.
  82.      */
  83.     public String getName() {
  84.     return name;
  85.     }
  86.  
  87.     /**
  88.      * Returns the version number for this provider.     
  89.      * 
  90.      * @return the version number for this provider.
  91.      */
  92.     public double getVersion() {
  93.     return version;
  94.     }
  95.  
  96.     /**
  97.      * Returns a human-readable description of the provider and its
  98.      * services.  This may return an HTML page, with relevant links.
  99.      *
  100.      * @return a description of the provider and its services.  
  101.      */
  102.     public String getInfo() {
  103.     return info;
  104.     }
  105.  
  106.  
  107.     static Provider loadProvider(String name) {
  108.     
  109.     try {
  110.         Class cl = Class.forName(name);
  111.         Object instance = cl.newInstance();
  112.  
  113.         if (instance instanceof Provider) {
  114.         return (Provider)instance;
  115.         }
  116.  
  117.     } catch (Exception e) {
  118.         debug("error loading provider " + name, e);
  119.     }
  120.     return null;
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Returns a string with the name and the version number
  126.      * of this provider.     
  127.      * 
  128.      * @return the string with the name and the version number
  129.      * for this provider.
  130.      */
  131.     public String toString() {
  132.     return name + " version " + version;
  133.     }
  134.  
  135.     /*
  136.      * override 3 methods from Hashtable to ensure that provider
  137.      * information can only be changed if the caller has the appropriate
  138.      * permissions.
  139.      */
  140.  
  141.     /**
  142.      * Clears this provider so that it no longer contains the properties
  143.      * used to look up facilities implemented by the provider.
  144.      *
  145.      * @since JDK1.2
  146.      */
  147.     public synchronized void clear() {
  148.     check("Provider.clear."+name);
  149.     super.clear();
  150.     }
  151.  
  152.     /**
  153.      * Sets the <code>key</code> property to have the specified 
  154.      * <code>value</code>.
  155.      * 
  156.      * @param key the property key.
  157.      * 
  158.      * @param value the property value.
  159.      * 
  160.      * @return the previous value of the specified property
  161.      * (<code>key</code>), or null if it did not have one.
  162.      * 
  163.      * @since JDK1.2
  164.      */
  165.     public synchronized Object put(Object key, Object value) {
  166.     check("Provider.put."+name);
  167.     return super.put(key, value);
  168.     }
  169.  
  170.     /**
  171.      * Removes the <code>key</code> property (and its corresponding 
  172.      * <code>value</code>).
  173.      * 
  174.      * @param key the key for the property to be removed.
  175.      * 
  176.      * @return the value to which the key had been mapped,
  177.      * or null if the key did not have a mapping.
  178.      * 
  179.      * @since JDK1.2
  180.      */
  181.     public synchronized Object remove(Object key) {
  182.     check("Provider.remove."+name);
  183.     return super.remove(key);
  184.     }
  185.  
  186.     private static void check(String directive) {
  187.          SecurityManager security = System.getSecurityManager();
  188.         if (security != null) {
  189.             security.checkSecurityAccess(directive);
  190.         }
  191.     }
  192.  
  193.     private static void debug(String msg) {
  194.     Security.debug(msg);
  195.     }
  196.  
  197.     private static void debug(String msg, Throwable t) {
  198.     Security.debug(msg, t);
  199.     }
  200.  
  201.     // Declare serialVersionUID to be compatible with JDK1.1
  202.     static final long serialVersionUID = -4298000515446427739L;    
  203. }
  204.  
  205.