01: /* Licensed to the Apache Software Foundation (ASF) under one or more
02: * contributor license agreements. See the NOTICE file distributed with
03: * this work for additional information regarding copyright ownership.
04: * The ASF licenses this file to You under the Apache License, Version 2.0
05: * (the "License"); you may not use this file except in compliance with
06: * the License. You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.harmony.luni.tests.java.lang;
18:
19: import java.security.Permission;
20: import java.security.PermissionCollection;
21: import java.security.Permissions;
22:
23: class MutableSecurityManager extends SecurityManager {
24:
25: static final RuntimePermission SET_SECURITY_MANAGER = new RuntimePermission(
26: "setSecurityManager");
27:
28: private PermissionCollection enabled;
29:
30: private PermissionCollection denied;
31:
32: public MutableSecurityManager() {
33: super ();
34: this .enabled = new Permissions();
35: }
36:
37: public MutableSecurityManager(Permission... permissions) {
38: this ();
39: for (int i = 0; i < permissions.length; i++) {
40: this .enabled.add(permissions[i]);
41: }
42: }
43:
44: void addPermission(Permission permission) {
45: enabled.add(permission);
46: }
47:
48: void clearPermissions() {
49: enabled = new Permissions();
50: }
51:
52: void denyPermission(Permission p) {
53: if (denied == null) {
54: denied = new Permissions();
55: }
56: denied.add(p);
57: }
58:
59: @Override
60: public void checkPermission(Permission permission) {
61: // denied should take precedence over allowed
62: if (denied != null && denied.implies(permission)) {
63: throw new SecurityException("Denied " + permission);
64: }
65:
66: if (enabled.implies(permission)) {
67: return;
68: }
69:
70: super.checkPermission(permission);
71: }
72: }
|