01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.java.security.more;
21:
22: import org.apache.axis2.java.security.AccessController;
23: import org.apache.axis2.java.security.interf.Actor;
24: import org.apache.axis2.AbstractTestCase;
25:
26: import java.security.AccessControlContext;
27: import java.security.Permission;
28: import java.security.PrivilegedAction;
29:
30: /**
31: * MorePermissionAccessControllerContext has read permission to both public.txt and private.txt
32: */
33:
34: public class MorePermissionAccessControlContext implements Actor {
35:
36: private Actor _actor;
37: private boolean _usingDoPrivilege;
38:
39: // Constructor
40: public MorePermissionAccessControlContext(Actor a,
41: boolean usingDoPrivilege) {
42: _actor = a;
43: _usingDoPrivilege = usingDoPrivilege;
44:
45: }
46:
47: // Implementing Actor's takeAction method
48: public void takeAction() {
49: try {
50: if (_usingDoPrivilege) {
51: final AccessControlContext acc = AccessController
52: .getContext();
53: // Print out maven's base,build, and test direcotories
54: String baseDir = AbstractTestCase.basedir;
55: System.out.println("basedir => " + baseDir);
56:
57: // Convert the \ (back slash) to / (forward slash)
58: String baseDirM = baseDir.replace('\\', '/');
59: System.out.println("baseDirM => " + baseDirM);
60:
61: String fs = "/";
62: String fileName = "private/private.txt";
63:
64: // Build the file URL
65: String fileURL = baseDirM + fs + "test-resources" + fs
66: + "java2javaec" + fs + fileName;
67: Permission perm = new java.io.FilePermission(fileURL,
68: "read");
69: acc.checkPermission(perm);
70: // Demostrate the usage of AccessController's doPrivilege(PrivilegeAction action, AccessContext ctx)
71: AccessController.doPrivileged(new PrivilegedAction() {
72: public Object run() {
73: _actor.takeAction();
74: return null;
75: }
76: }, acc);
77: } else {
78: // Use no doPrivileged
79: _actor.takeAction();
80: }
81: } catch (Exception e) {
82: e.printStackTrace(System.out);
83: }
84: }
85: }
|