| 1 | /* AccessControlContext.java --- Access Control Context Class
|
|---|
| 2 | Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 | AccessControlContext makes system resource access decsion
|
|---|
| 42 | based on permission rights.
|
|---|
| 43 |
|
|---|
| 44 | It is used for a specific context and has only one method
|
|---|
| 45 | checkPermission. It is similar to AccessController except
|
|---|
| 46 | that it makes decsions based on the current context instead
|
|---|
| 47 | of the the current thread.
|
|---|
| 48 |
|
|---|
| 49 | It is created by call AccessController.getContext method.
|
|---|
| 50 |
|
|---|
| 51 | @author Mark Benvenuto
|
|---|
| 52 | @since JDK 1.2
|
|---|
| 53 | */
|
|---|
| 54 | public final class AccessControlContext
|
|---|
| 55 | {
|
|---|
| 56 | private ProtectionDomain protectionDomain[];
|
|---|
| 57 | private DomainCombiner combiner;
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | Construct a new AccessControlContext with the specified
|
|---|
| 61 | ProtectionDomains. <code>context</code> must not be
|
|---|
| 62 | null and duplicates will be removed.
|
|---|
| 63 |
|
|---|
| 64 | @param context The ProtectionDomains to use
|
|---|
| 65 | */
|
|---|
| 66 | public AccessControlContext(ProtectionDomain[]context)
|
|---|
| 67 | {
|
|---|
| 68 | int i, j, k, count = context.length, count2 = 0;
|
|---|
| 69 | for (i = 0, j = 0; i < count; i++)
|
|---|
| 70 | {
|
|---|
| 71 | for (k = 0; k < i; k++)
|
|---|
| 72 | if (context[k] == protectionDomain[i])
|
|---|
| 73 | break;
|
|---|
| 74 | if (k != i) //it means previous loop did not complete
|
|---|
| 75 | continue;
|
|---|
| 76 |
|
|---|
| 77 | count2++;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | protectionDomain = new ProtectionDomain[count2];
|
|---|
| 81 | for (i = 0, j = 0; i < count2; i++)
|
|---|
| 82 | {
|
|---|
| 83 | for (k = 0; k < i; k++)
|
|---|
| 84 | if (context[k] == protectionDomain[i])
|
|---|
| 85 | break;
|
|---|
| 86 | if (k != i) //it means previous loop did not complete
|
|---|
| 87 | continue;
|
|---|
| 88 |
|
|---|
| 89 | protectionDomain[j++] = context[i];
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | Construct a new AccessControlContext with the specified
|
|---|
| 95 | ProtectionDomains and DomainCombiner
|
|---|
| 96 |
|
|---|
| 97 | @param context The ProtectionDomains to use
|
|---|
| 98 |
|
|---|
| 99 | @since JDK 1.3
|
|---|
| 100 | */
|
|---|
| 101 | public AccessControlContext(AccessControlContext acc,
|
|---|
| 102 | DomainCombiner combiner)
|
|---|
| 103 | {
|
|---|
| 104 | this(acc.protectionDomain);
|
|---|
| 105 | this.combiner = combiner;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | Returns the Domain Combiner associated with the AccessControlContext
|
|---|
| 110 |
|
|---|
| 111 | @returns the DomainCombiner
|
|---|
| 112 | */
|
|---|
| 113 | public DomainCombiner getDomainCombiner()
|
|---|
| 114 | {
|
|---|
| 115 | return combiner;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | Determines whether or not the specific permission is granted
|
|---|
| 120 | depending on the context it is within.
|
|---|
| 121 |
|
|---|
| 122 | @param perm a permission to check
|
|---|
| 123 |
|
|---|
| 124 | @throws AccessControlException if the permssion is not permitted
|
|---|
| 125 | */
|
|---|
| 126 | public void checkPermission(Permission perm) throws AccessControlException
|
|---|
| 127 | {
|
|---|
| 128 | for (int i = 0; i < protectionDomain.length; i++)
|
|---|
| 129 | if (protectionDomain[i].implies(perm) == true)
|
|---|
| 130 | return;
|
|---|
| 131 |
|
|---|
| 132 | throw new AccessControlException("Permission not granted");
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | Checks if two AccessControlContexts are equal.
|
|---|
| 137 |
|
|---|
| 138 | It first checks if obj is an AccessControlContext class, and
|
|---|
| 139 | then checks if each ProtectionDomain matches.
|
|---|
| 140 |
|
|---|
| 141 | @param obj The object to compare this class to
|
|---|
| 142 |
|
|---|
| 143 | @return true if equal, false otherwise
|
|---|
| 144 | */
|
|---|
| 145 | public boolean equals(Object obj)
|
|---|
| 146 | {
|
|---|
| 147 | if (obj instanceof AccessControlContext)
|
|---|
| 148 | {
|
|---|
| 149 | AccessControlContext acc = (AccessControlContext) obj;
|
|---|
| 150 |
|
|---|
| 151 | if (acc.protectionDomain.length != protectionDomain.length)
|
|---|
| 152 | return false;
|
|---|
| 153 |
|
|---|
| 154 | for (int i = 0; i < protectionDomain.length; i++)
|
|---|
| 155 | if (acc.protectionDomain[i] != protectionDomain[i])
|
|---|
| 156 | return false;
|
|---|
| 157 | return true;
|
|---|
| 158 | }
|
|---|
| 159 | return false;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | Computes a hash code of this class
|
|---|
| 164 |
|
|---|
| 165 | @return a hash code representing this class
|
|---|
| 166 | */
|
|---|
| 167 | public int hashCode()
|
|---|
| 168 | {
|
|---|
| 169 | int h = 0;
|
|---|
| 170 | for (int i = 0; i < protectionDomain.length; i++)
|
|---|
| 171 | h ^= protectionDomain[i].hashCode();
|
|---|
| 172 |
|
|---|
| 173 | return h;
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|