1 |
14 |
jlechner |
/* AccessControlContext.java --- Access Control Context Class
|
2 |
|
|
Copyright (C) 1999, 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 |
|
|
* 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 1.2
|
53 |
|
|
*/
|
54 |
|
|
public final class AccessControlContext
|
55 |
|
|
{
|
56 |
|
|
private ProtectionDomain[] protectionDomains;
|
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] == protectionDomains[i])
|
73 |
|
|
break;
|
74 |
|
|
if (k != i) //it means previous loop did not complete
|
75 |
|
|
continue;
|
76 |
|
|
|
77 |
|
|
count2++;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
protectionDomains = new ProtectionDomain[count2];
|
81 |
|
|
for (i = 0, j = 0; i < count2; i++)
|
82 |
|
|
{
|
83 |
|
|
for (k = 0; k < i; k++)
|
84 |
|
|
if (context[k] == protectionDomains[i])
|
85 |
|
|
break;
|
86 |
|
|
if (k != i) //it means previous loop did not complete
|
87 |
|
|
continue;
|
88 |
|
|
|
89 |
|
|
protectionDomains[j++] = context[i];
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* Construct a new AccessControlContext with the specified
|
95 |
|
|
* ProtectionDomains and DomainCombiner
|
96 |
|
|
*
|
97 |
|
|
* @since 1.3
|
98 |
|
|
*/
|
99 |
|
|
public AccessControlContext(AccessControlContext acc,
|
100 |
|
|
DomainCombiner combiner)
|
101 |
|
|
{
|
102 |
|
|
this(acc.protectionDomains);
|
103 |
|
|
this.combiner = combiner;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Returns the Domain Combiner associated with the AccessControlContext
|
108 |
|
|
*
|
109 |
|
|
* @return the DomainCombiner
|
110 |
|
|
*/
|
111 |
|
|
public DomainCombiner getDomainCombiner()
|
112 |
|
|
{
|
113 |
|
|
return combiner;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
/**
|
117 |
|
|
* Determines whether or not the specific permission is granted
|
118 |
|
|
* depending on the context it is within.
|
119 |
|
|
*
|
120 |
|
|
* @param perm a permission to check
|
121 |
|
|
*
|
122 |
|
|
* @throws AccessControlException if the permssion is not permitted
|
123 |
|
|
*/
|
124 |
|
|
public void checkPermission(Permission perm) throws AccessControlException
|
125 |
|
|
{
|
126 |
|
|
for (int i = 0; i < protectionDomains.length; i++)
|
127 |
|
|
if (protectionDomains[i].implies(perm) == true)
|
128 |
|
|
return;
|
129 |
|
|
|
130 |
|
|
throw new AccessControlException("Permission not granted");
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/**
|
134 |
|
|
* Checks if two AccessControlContexts are equal.
|
135 |
|
|
*
|
136 |
|
|
* It first checks if obj is an AccessControlContext class, and
|
137 |
|
|
* then checks if each ProtectionDomain matches.
|
138 |
|
|
*
|
139 |
|
|
* @param obj The object to compare this class to
|
140 |
|
|
*
|
141 |
|
|
* @return true if equal, false otherwise
|
142 |
|
|
*/
|
143 |
|
|
public boolean equals(Object obj)
|
144 |
|
|
{
|
145 |
|
|
if (obj instanceof AccessControlContext)
|
146 |
|
|
{
|
147 |
|
|
AccessControlContext acc = (AccessControlContext) obj;
|
148 |
|
|
|
149 |
|
|
if (acc.protectionDomains.length != protectionDomains.length)
|
150 |
|
|
return false;
|
151 |
|
|
|
152 |
|
|
for (int i = 0; i < protectionDomains.length; i++)
|
153 |
|
|
if (acc.protectionDomains[i] != protectionDomains[i])
|
154 |
|
|
return false;
|
155 |
|
|
return true;
|
156 |
|
|
}
|
157 |
|
|
return false;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
/**
|
161 |
|
|
* Computes a hash code of this class
|
162 |
|
|
*
|
163 |
|
|
* @return a hash code representing this class
|
164 |
|
|
*/
|
165 |
|
|
public int hashCode()
|
166 |
|
|
{
|
167 |
|
|
int h = 0;
|
168 |
|
|
for (int i = 0; i < protectionDomains.length; i++)
|
169 |
|
|
h ^= protectionDomains[i].hashCode();
|
170 |
|
|
|
171 |
|
|
return h;
|
172 |
|
|
}
|
173 |
|
|
}
|