2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / AccessControlContext.java
blob5b9ac4a254850ed1b57a39f751db19445ef0062f
1 /* AccessControlContext.java --- Access Control Context Class
2 Copyright (C) 1999 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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;
40 /**
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
52 @since JDK 1.2
54 public final class AccessControlContext
56 private ProtectionDomain protectionDomain[];
57 private DomainCombiner combiner;
59 /**
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] == protectionDomain[i])
73 break;
74 if (k != i) //it means previous loop did not complete
75 continue;
77 count2++;
80 protectionDomain = new ProtectionDomain[count2];
81 for (i = 0, j = 0; i < count2; i++)
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;
89 protectionDomain[j++] = context[i];
93 /**
94 Construct a new AccessControlContext with the specified
95 ProtectionDomains and DomainCombiner
97 @param context The ProtectionDomains to use
99 @since JDK 1.3
101 public AccessControlContext(AccessControlContext acc,
102 DomainCombiner combiner)
104 this(acc.protectionDomain);
105 this.combiner = combiner;
109 Returns the Domain Combiner associated with the AccessControlContext
111 @returns the DomainCombiner
113 public DomainCombiner getDomainCombiner()
115 return combiner;
119 Determines whether or not the specific permission is granted
120 depending on the context it is within.
122 @param perm a permission to check
124 @throws AccessControlException if the permssion is not permitted
126 public void checkPermission(Permission perm) throws AccessControlException
128 for (int i = 0; i < protectionDomain.length; i++)
129 if (protectionDomain[i].implies(perm) == true)
130 return;
132 throw new AccessControlException("Permission not granted");
136 Checks if two AccessControlContexts are equal.
138 It first checks if obj is an AccessControlContext class, and
139 then checks if each ProtectionDomain matches.
141 @param obj The object to compare this class to
143 @return true if equal, false otherwise
145 public boolean equals(Object obj)
147 if (obj instanceof AccessControlContext)
149 AccessControlContext acc = (AccessControlContext) obj;
151 if (acc.protectionDomain.length != protectionDomain.length)
152 return false;
154 for (int i = 0; i < protectionDomain.length; i++)
155 if (acc.protectionDomain[i] != protectionDomain[i])
156 return false;
157 return true;
159 return false;
163 Computes a hash code of this class
165 @return a hash code representing this class
167 public int hashCode()
169 int h = 0;
170 for (int i = 0; i < protectionDomain.length; i++)
171 h ^= protectionDomain[i].hashCode();
173 return h;