FileDocCategorySizeDatePackage
XYZPolicy.javaAPI DocExample3294Sun Oct 25 18:13:36 GMT 1998None

XYZPolicy.java

/*
 *
 * Copyright (c) 1998 Scott Oaks. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and
 * without fee is hereby granted.
 *
 * This sample source code is provided for example only,
 * on an unsupported, as-is basis. 
 *
 * AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  AUTHOR
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 */


import java.security.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class XYZPolicy extends Policy {

	class XYZPolicyEntry {
		Permission p;
		CodeSource cs;

		XYZPolicyEntry(Permission p, CodeSource cs) {
			this.p = p;
			this.cs = cs;
		}
	}

	private KeyStore ks;
	Vector permissions;

	public XYZPolicy() {
		permissions = new Vector();

		AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				initKeyStore();
				initPolicy();
				return null;
			}
		});
	}

	private void initKeyStore() {
		try {
			// In 1.2 beta 4, we must supply an argument to the getInstance
			// method
			ks = KeyStore.getInstance(KeyStore.getDefaultType());
			ks.load(new FileInputStream(System.getProperty("user.home") +
						File.separator + ".keystore"), null);
		} catch (Exception e) {
			ks = null;
		}
	}

	private void initPolicy() {
		CodeSource cs = null;
		permissions.add(new XYZPolicyEntry(
						new PropertyPermission("java.version", "read"), cs));
		
		// http://piccolo/ sdo
		try {
			if (ks != null) {
				URL u = new URL("http://piccolo/");
				// In 1.2 beta 4, we need the certificates to initialize
				// the code source (instead of the public keys, which is
				// what we used earlier
				java.security.cert.Certificate c[] = ks.getCertificateChain("sdo");
	
				cs = new CodeSource(u, c);
				permissions.add(new XYZPolicyEntry(
						new PropertyPermission("os.arch", "read"), cs));
			}
		} catch (Exception e) {}
	}

	// In 1.2 beta 4, the evaluate method has been renamed to getPermissions
	public PermissionCollection getPermissions(CodeSource codesource) {
		Permissions perms = new Permissions();
		for (Enumeration e = permissions.elements(); e.hasMoreElements(); ) {
			XYZPolicyEntry pe = (XYZPolicyEntry) e.nextElement();
			if (pe.cs == null)
				perms.add(pe.p);
			else if (pe.cs.equals(codesource))
				perms.add(pe.p);
		}
		return perms;
	}

	public void refresh() {
		initPolicy();
	}
}