home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 6.4 KB | 195 lines |
- /*
- * @(#)Policy.java 1.49 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
-
- package java.security;
-
- import java.io.*;
- import java.lang.RuntimePermission;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.StringTokenizer;
- import java.util.PropertyPermission;
-
- import java.lang.reflect.*;
-
- /**
- * The policy for a Java runtime (specifying
- * which permissions are available for code from various principals)
- * is represented by a Policy object.
- *
- * <p>There is only one Policy object in effect at any given time.
- * It is consulted by
- * a ProtectionDomain when the protection domain initializes its set of
- * permissions. <p>
- *
- * <p>The source location for the policy information utilized by the
- * Policy object is up to the Policy implementation.
- * The policy configuration may be stored, for example, as a
- * flat ASCII file, as a serialized binary file of
- * the Policy class, or as a database. <p>
- *
- * <p>The currently-installed Policy object can be obtained by calling
- * <code>getPolicy</code>, and it can be changed by a call to
- * the <code>setPolicy</code> method.
- *
- * <p>The <code>refresh</code> method causes the policy
- * object to refresh/reload its current configuration. This is
- * implementation-dependent. For example, if the policy object stores
- * its policy in configuration files, calling <code>refresh</code> will
- * cause it to re-read the configuration policy files.
- *
- * <p>The Policy object is agnostic in that
- * it is not involved in making policy decisions. It is merely the
- * Java runtime representation of the persistent policy configuration.
- *
- * <p>When a protection domain needs to initialize its set of
- * permissions, it executes code such as the following
- * to ask the currently installed Policy object to populate a
- * Permissions object with the appropriate permissions:
- * <pre>
- * policy = Policy.getPolicy();
- * Permissions perms = policy.evaluate(MyCodeSource)
- * </pre>
- *
- * <p>The protection domain passes in a CodeSource
- * object, which encapsulates its codebase (URL) and public key attributes.
- * The Policy object evaluates the global policy in light of who the
- * principal is and returns an appropriate Permissions object.
- *
- * <p>The default Policy implementation can be changed
- * by setting the value of the "policy.provider" security property (in the
- * Java security properties file) to the fully qualified
- * name of the desired Policy implementation class.
- * The Java security properties file is located in the file named
- * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
- * refers to the directory where the JDK was installed.
- *
- * @author Roland Schemers
- * @version 1.49, 03/18/98
- * @see java.security.CodeSource
- * @see java.security.Permissions
- */
-
- public abstract class Policy {
-
- /** the system-wide policy. */
- private static Policy policy;
-
- /**
- * Returns the installed Policy object. This value should not be cached,
- * as it may be changed by a call to setPolicy. This method calls
- * <code>SecurityManager.checkPermission</code> with the
- * <code>SecurityPermission("Policy.getPolicy")</code>
- * permission.
- *
- * @return the installed Policy.
- *
- * @exception java.lang.SecurityException if the current thread does not
- * have permission to get the Policy object.
- *
- */
- public static Policy getPolicy()
- {
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) sm.checkPermission(new SecurityPermission
- ("Policy.getPolicy"));
- return getPolicyNoCheck();
- }
-
- /**
- * Returns the installed Policy object, skipping the security check.
- * Used by ProtectionDomain and getPolicy.
- *
- * @return the installed Policy.
- *
- */
- static Policy getPolicyNoCheck()
- {
- if (policy == null) {
-
- String policy_class = null;
-
- try {
- AccessController.beginPrivileged();
- policy_class = Security.getProperty("policy.provider");
- } finally {
- AccessController.endPrivileged();
- }
-
- if (policy_class == null) {
- policy_class = "java.security.PolicyFile";
- }
-
- try {
- policy = (Policy) Class.forName(policy_class).newInstance();
- } catch (Exception e) {
- policy = new java.security.PolicyFile();
- }
- }
-
- return policy;
- }
-
- /**
- * Sets the system-wide Policy object. This method calls
- * <code>SecurityManager.checkPermission</code> with the
- * <code>SecurityPermission("Policy.setPolicy")</code>
- * permission.
- *
- * @param policy the new system Policy object.
- *
- * @exception java.lang.SecurityException if the current thread does not
- * have permission to set the Policy.
- *
- */
- public static void setPolicy(Policy policy)
- {
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) sm.checkPermission(
- new SecurityPermission("Policy.setPolicy"));
- Policy.policy = policy;
- }
-
- /**
- * Evaluates the policy object with the specified CodeSource, and
- * creates a Permissions object with
- * the set of permissions for that principal's protection domain.
- *
- * @param CodeSource the codesource associated with the caller.
- * This encapsulates the original location of the code (where the code
- * came from) and the public key(s) of its signer.
- *
- * @return the set of permissions according to the policy.
- *
- * @exception java.lang.SecurityException if the current thread does not
- * have permission to call <code>evaluate</code> on the policy object.
-
- */
- public abstract Permissions evaluate(CodeSource codesource);
-
- /**
- * Refreshes the given policy object. The behavior of this method
- * depends on the implementation. For example, calling refresh on a
- * file-based policy will cause the file to be re-read.
- *
- * @exception java.lang.SecurityException if the current thread does not
- * have permission to refresh this Policy object.
- */
- public abstract void refresh();
- }
-