01: /*
02: * @(#)SecurePackage.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.security;
10:
11: import pnuts.lang.Context;
12: import pnuts.lang.Package;
13:
14: /**
15: * Package that can control add/write/read operation in a security context.
16: *
17: * <pre>
18: * pnuts -J-Djava.security.manager "-J-Dpnuts.package.factory=pnuts.security.SecurePackage\$Factory" <em>scripts</em>
19: * </pre>
20: */
21: public class SecurePackage extends Package {
22:
23: public SecurePackage(String name, Package parent) {
24: super (name, parent);
25: }
26:
27: protected void addPackage(Package pkg, Context context) {
28: checkPackageAccess(pkg, "add");
29: super .addPackage(pkg, context);
30: }
31:
32: public void set(String symbol, Object obj, Context context) {
33: checkPackageAccess(this , "write");
34: super .set(symbol, obj, context);
35: }
36:
37: public void clear(String symbol, Context context) {
38: checkPackageAccess(this , "write");
39: super .clear(symbol, context);
40: }
41:
42: protected void removePackage(Package pkg, Context context) {
43: checkPackageAccess(pkg, "remove");
44: super .removePackage(pkg, context);
45: }
46:
47: void checkPackageAccess(Package pkg, String action) {
48: SecurityManager security = System.getSecurityManager();
49: if (security == null) {
50: return;
51: }
52: String name = pkg.getName();
53: if (name == null) {
54: return;
55: }
56: if ("".equals(name)) {
57: name = "::";
58: }
59: security.checkPermission(new PackagePermission(name, action));
60: }
61: }
|