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

  1. /*
  2.  * @(#)Policy.java    1.49 98/03/18
  3.  *
  4.  * Copyright 1997, 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.  
  16. package java.security;
  17.  
  18. import java.io.*;
  19. import java.lang.RuntimePermission;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.util.Enumeration;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. import java.util.StringTokenizer;
  26. import java.util.PropertyPermission;
  27.  
  28. import java.lang.reflect.*;
  29.  
  30. /**
  31.  * The policy for a Java runtime (specifying 
  32.  * which permissions are available for code from various principals)
  33.  * is represented by a Policy object.
  34.  * 
  35.  * <p>There is only one Policy object in effect at any given time.
  36.  * It is consulted by
  37.  * a ProtectionDomain when the protection domain initializes its set of
  38.  * permissions. <p>
  39.  *
  40.  * <p>The source location for the policy information utilized by the
  41.  * Policy object is up to the Policy implementation.
  42.  * The policy configuration may be stored, for example, as a
  43.  * flat ASCII file, as a serialized binary file of
  44.  * the Policy class, or as a database. <p>
  45.  *
  46.  * <p>The currently-installed Policy object can be obtained by calling
  47.  * <code>getPolicy</code>, and it can be changed by a call to
  48.  * the <code>setPolicy</code> method.
  49.  * 
  50.  * <p>The <code>refresh</code> method causes the policy
  51.  * object to refresh/reload its current configuration. This is
  52.  * implementation-dependent. For example, if the policy object stores
  53.  * its policy in configuration files, calling <code>refresh</code> will
  54.  * cause it to re-read the configuration policy files.
  55.  * 
  56.  * <p>The Policy object is agnostic in that
  57.  * it is not involved in making policy decisions.  It is merely the
  58.  * Java runtime representation of the persistent policy configuration.
  59.  * 
  60.  * <p>When a protection domain needs to initialize its set of
  61.  * permissions, it executes code such as the following
  62.  * to ask the currently installed Policy object to populate a
  63.  * Permissions object with the appropriate permissions:
  64.  * <pre>
  65.  *   policy = Policy.getPolicy();
  66.  *   Permissions perms = policy.evaluate(MyCodeSource)
  67.  * </pre>
  68.  * 
  69.  * <p>The protection domain passes in a CodeSource
  70.  * object, which encapsulates its codebase (URL) and public key attributes.
  71.  * The Policy object evaluates the global policy in light of who the
  72.  * principal is and returns an appropriate Permissions object.
  73.  *
  74.  * <p>The default Policy implementation can be changed 
  75.  * by setting the value of the "policy.provider" security property (in the
  76.  * Java security properties file) to the fully qualified
  77.  * name of the desired Policy implementation class.
  78.  * The Java security properties file is located in the file named
  79.  * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  80.  * refers to the directory where the JDK was installed.
  81.  * 
  82.  * @author Roland Schemers
  83.  * @version 1.49, 03/18/98
  84.  * @see java.security.CodeSource
  85.  * @see java.security.Permissions
  86.  */
  87.  
  88. public abstract class Policy {
  89.  
  90.     /** the system-wide policy. */
  91.     private static Policy policy;
  92.  
  93.     /**
  94.      * Returns the installed Policy object. This value should not be cached,
  95.      * as it may be changed by a call to setPolicy. This method calls 
  96.      * <code>SecurityManager.checkPermission</code> with the
  97.      * <code>SecurityPermission("Policy.getPolicy")</code>
  98.      * permission.
  99.      *
  100.      * @return the installed Policy.
  101.      *
  102.      * @exception java.lang.SecurityException if the current thread does not 
  103.      *            have permission to get the Policy object.
  104.      *
  105.      */
  106.     public static Policy getPolicy() 
  107.     {
  108.         SecurityManager sm = System.getSecurityManager();
  109.         if (sm != null) sm.checkPermission(new SecurityPermission
  110.                         ("Policy.getPolicy"));
  111.     return getPolicyNoCheck();
  112.     }
  113.  
  114.     /**
  115.      * Returns the installed Policy object, skipping the security check.
  116.      * Used by ProtectionDomain and getPolicy.
  117.      *
  118.      * @return the installed Policy.
  119.      *
  120.      */
  121.     static Policy getPolicyNoCheck() 
  122.     {
  123.     if (policy == null) {
  124.  
  125.         String policy_class = null;
  126.  
  127.         try {
  128.         AccessController.beginPrivileged();
  129.         policy_class = Security.getProperty("policy.provider");
  130.         } finally {
  131.         AccessController.endPrivileged();
  132.         }
  133.  
  134.         if (policy_class == null) {
  135.         policy_class = "java.security.PolicyFile";
  136.         }
  137.  
  138.         try {
  139.         policy = (Policy) Class.forName(policy_class).newInstance();
  140.         } catch (Exception e) {
  141.         policy = new java.security.PolicyFile();
  142.         }
  143.     }
  144.  
  145.     return policy;
  146.     }
  147.  
  148.     /**
  149.      * Sets the system-wide Policy object. This method calls 
  150.      * <code>SecurityManager.checkPermission</code> with the
  151.      * <code>SecurityPermission("Policy.setPolicy")</code>
  152.      * permission.
  153.      *
  154.      * @param policy the new system Policy object.
  155.      *
  156.      * @exception java.lang.SecurityException if the current thread does not 
  157.      *            have permission to set the Policy.
  158.      *
  159.      */
  160.     public static void setPolicy(Policy policy)
  161.     {
  162.     SecurityManager sm = System.getSecurityManager();
  163.     if (sm != null) sm.checkPermission(
  164.                  new SecurityPermission("Policy.setPolicy"));
  165.     Policy.policy = policy;
  166.     }
  167.  
  168.     /**
  169.      * Evaluates the policy object with the specified CodeSource, and
  170.      * creates a Permissions object with
  171.      * the set of permissions for that principal's protection domain.
  172.      *
  173.      * @param CodeSource the codesource associated with the caller.
  174.      * This encapsulates the original location of the code (where the code
  175.      * came from) and the public key(s) of its signer.
  176.      *
  177.      * @return the set of permissions according to the policy.  
  178.      *
  179.      * @exception java.lang.SecurityException if the current thread does not 
  180.      *            have permission to call <code>evaluate</code> on the policy object.
  181.  
  182.      */
  183.     public abstract Permissions evaluate(CodeSource codesource);
  184.  
  185.     /**
  186.      * Refreshes the given policy object. The behavior of this method
  187.      * depends on the implementation. For example, calling refresh on a 
  188.      * file-based policy will cause the file to be re-read.
  189.      *
  190.      * @exception java.lang.SecurityException if the current thread does not 
  191.      *            have permission to refresh this Policy object.
  192.      */
  193.     public abstract void refresh();
  194. }
  195.