Frames | No Frames |
1: /* AccessController.java --- Access control context and permission checker 2: Copyright (C) 2001, 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package java.security; 39: 40: /** 41: * Access control context and permission checker. 42: * Can check permissions in the access control context of the current thread 43: * through the <code>checkPermission()</code> method. 44: * Manipulates the access control context for code that needs to be executed 45: * the protection domain of the calling class (by explicitly ignoring the 46: * context of the calling code) in the <code>doPrivileged()</code> methods. 47: * And provides a <code>getContext()</code> method which gives the access 48: * control context of the current thread that can be used for checking 49: * permissions at a later time and/or in another thread. 50: * <p> 51: * XXX - Mostly a stub implementation at the moment. Needs native support 52: * from the VM to function correctly. XXX - Do not forget to think about 53: * how to handle <code>java.lang.reflect.Method.invoke()</code> on the 54: * <code>doPrivileged()</code> methods. 55: * 56: * @author Mark Wielaard (mark@klomp.org) 57: * @since 1.2 58: */ 59: public final class AccessController 60: { 61: /** 62: * This class only has static methods so there is no public contructor. 63: */ 64: private AccessController() 65: { 66: } 67: 68: /** 69: * Checks wether the access control context of the current thread allows 70: * the given Permission. Throws an <code>AccessControlException</code> 71: * when the permission is not allowed in the current context. Otherwise 72: * returns silently without throwing an exception. 73: * 74: * @param perm the permission to be checked. 75: * @exception AccessControlException thrown if the current context does not 76: * allow the given permission. 77: */ 78: public static void checkPermission(Permission perm) 79: throws AccessControlException 80: { 81: getContext().checkPermission(perm); 82: } 83: 84: /** 85: * Calls the <code>run()</code> method of the given action with as 86: * (initial) access control context only the protection domain of the 87: * calling class. Calls to <code>checkPermission()</code> in the 88: * <code>run()</code> method ignore all earlier protection domains of 89: * classes in the call chain. Note that the protection domains of classes 90: * called by the code in the <code>run()</code> method are not ignored. 91: * 92: * @param action the <code>PrivilegedAction</code> whose <code>run()</code> 93: * should be be called. 94: * @return the result of the <code>action.run()</code> method. 95: */ 96: public static Object doPrivileged(PrivilegedAction action) 97: { 98: return action.run(); 99: } 100: 101: /** 102: * Calls the <code>run()</code> method of the given action with as 103: * (initial) access control context the given context combined with the 104: * protection domain of the calling class. Calls to 105: * <code>checkPermission()</code> in the <code>run()</code> method ignore 106: * all earlier protection domains of classes in the call chain, but add 107: * checks for the protection domains given in the supplied context. 108: * 109: * @param action the <code>PrivilegedAction</code> whose <code>run()</code> 110: * should be be called. 111: * @param context the <code>AccessControlContext</code> whose protection 112: * domains should be added to the protection domain of the calling class. 113: * @return the result of the <code>action.run()</code> method. 114: */ 115: public static Object doPrivileged(PrivilegedAction action, 116: AccessControlContext context) 117: { 118: return action.run(); 119: } 120: 121: /** 122: * Calls the <code>run()</code> method of the given action with as 123: * (initial) access control context only the protection domain of the 124: * calling class. Calls to <code>checkPermission()</code> in the 125: * <code>run()</code> method ignore all earlier protection domains of 126: * classes in the call chain. Note that the protection domains of classes 127: * called by the code in the <code>run()</code> method are not ignored. 128: * If the <code>run()</code> method throws an exception then this method 129: * will wrap that exception in an <code>PrivilegedActionException</code>. 130: * 131: * @param action the <code>PrivilegedExceptionAction</code> whose 132: * <code>run()</code> should be be called. 133: * @return the result of the <code>action.run()</code> method. 134: * @exception PrivilegedActionException wrapped around any exception that 135: * is thrown in the <code>run()</code> method. 136: */ 137: public static Object doPrivileged(PrivilegedExceptionAction action) 138: throws PrivilegedActionException 139: { 140: try 141: { 142: return action.run(); 143: } 144: catch (Exception e) 145: { 146: throw new PrivilegedActionException(e); 147: } 148: } 149: 150: /** 151: * Calls the <code>run()</code> method of the given action with as 152: * (initial) access control context the given context combined with the 153: * protection domain of the calling class. Calls to 154: * <code>checkPermission()</code> in the <code>run()</code> method ignore 155: * all earlier protection domains of classes in the call chain, but add 156: * checks for the protection domains given in the supplied context. 157: * If the <code>run()</code> method throws an exception then this method 158: * will wrap that exception in an <code>PrivilegedActionException</code>. 159: * 160: * @param action the <code>PrivilegedExceptionAction</code> whose 161: * <code>run()</code> should be be called. 162: * @param context the <code>AccessControlContext</code> whose protection 163: * domains should be added to the protection domain of the calling class. 164: * @return the result of the <code>action.run()</code> method. 165: * @exception PrivilegedActionException wrapped around any exception that 166: * is thrown in the <code>run()</code> method. 167: */ 168: public static Object doPrivileged(PrivilegedExceptionAction action, 169: AccessControlContext context) 170: throws PrivilegedActionException 171: { 172: try 173: { 174: return action.run(); 175: } 176: catch (Exception e) 177: { 178: throw new PrivilegedActionException(e); 179: } 180: } 181: 182: /** 183: * Returns the complete access control context of the current thread. 184: * <p> 185: * XXX - Should this include all the protection domains in the call chain 186: * or only the domains till the last <code>doPrivileged()</code> call? 187: * <p> 188: * XXX - needs native support. Currently returns an empty context. 189: */ 190: public static AccessControlContext getContext() 191: { 192: // For now just return an new empty context 193: return new AccessControlContext(new ProtectionDomain[0]); 194: } 195: }