1 /* AccessControlContext.java --- Access Control Context Class
2 Copyright (C) 1999, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package java.security;
41 * AccessControlContext makes system resource access decsion
42 * based on permission rights.
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.
49 * It is created by call AccessController.getContext method.
51 * @author Mark Benvenuto
54 public final class AccessControlContext
56 private ProtectionDomain[] protectionDomains;
57 private DomainCombiner combiner;
60 * Construct a new AccessControlContext with the specified
61 * ProtectionDomains. <code>context</code> must not be
62 * null and duplicates will be removed.
64 * @param context The ProtectionDomains to use
66 public AccessControlContext(ProtectionDomain[] context)
68 int i, j, k, count = context.length, count2 = 0;
69 for (i = 0, j = 0; i < count; i++)
71 for (k = 0; k < i; k++)
72 if (context[k] == protectionDomains[i])
74 if (k != i) //it means previous loop did not complete
80 protectionDomains = new ProtectionDomain[count2];
81 for (i = 0, j = 0; i < count2; i++)
83 for (k = 0; k < i; k++)
84 if (context[k] == protectionDomains[i])
86 if (k != i) //it means previous loop did not complete
89 protectionDomains[j++] = context[i];
94 * Construct a new AccessControlContext with the specified
95 * ProtectionDomains and DomainCombiner
99 public AccessControlContext(AccessControlContext acc,
100 DomainCombiner combiner)
102 this(acc.protectionDomains);
103 this.combiner = combiner;
107 * Returns the Domain Combiner associated with the AccessControlContext
109 * @return the DomainCombiner
111 public DomainCombiner getDomainCombiner()
117 * Determines whether or not the specific permission is granted
118 * depending on the context it is within.
120 * @param perm a permission to check
122 * @throws AccessControlException if the permssion is not permitted
124 public void checkPermission(Permission perm) throws AccessControlException
126 for (int i = 0; i < protectionDomains.length; i++)
127 if (protectionDomains[i].implies(perm) == true)
130 throw new AccessControlException("Permission not granted");
134 * Checks if two AccessControlContexts are equal.
136 * It first checks if obj is an AccessControlContext class, and
137 * then checks if each ProtectionDomain matches.
139 * @param obj The object to compare this class to
141 * @return true if equal, false otherwise
143 public boolean equals(Object obj)
145 if (obj instanceof AccessControlContext)
147 AccessControlContext acc = (AccessControlContext) obj;
149 if (acc.protectionDomains.length != protectionDomains.length)
152 for (int i = 0; i < protectionDomains.length; i++)
153 if (acc.protectionDomains[i] != protectionDomains[i])
161 * Computes a hash code of this class
163 * @return a hash code representing this class
165 public int hashCode()
168 for (int i = 0; i < protectionDomains.length; i++)
169 h ^= protectionDomains[i].hashCode();