01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package javax.security.auth;
19:
20: import java.security.DomainCombiner;
21: import java.security.Principal;
22: import java.security.ProtectionDomain;
23: import java.util.Set;
24:
25: public class SubjectDomainCombiner implements DomainCombiner {
26:
27: // subject to be associated
28: private Subject subject;
29:
30: // permission required to get a subject object
31: private static final AuthPermission _GET = new AuthPermission(
32: "getSubjectFromDomainCombiner"); //$NON-NLS-1$
33:
34: public SubjectDomainCombiner(Subject subject) {
35: super ();
36: if (subject == null) {
37: throw new NullPointerException();
38: }
39: this .subject = subject;
40: }
41:
42: public Subject getSubject() {
43: SecurityManager sm = System.getSecurityManager();
44: if (sm != null) {
45: sm.checkPermission(_GET);
46: }
47:
48: return subject;
49: }
50:
51: public ProtectionDomain[] combine(
52: ProtectionDomain[] currentDomains,
53: ProtectionDomain[] assignedDomains) {
54: // get array length for combining protection domains
55: int len = 0;
56: if (currentDomains != null) {
57: len += currentDomains.length;
58: }
59: if (assignedDomains != null) {
60: len += assignedDomains.length;
61: }
62: if (len == 0) {
63: return null;
64: }
65:
66: ProtectionDomain[] pd = new ProtectionDomain[len];
67:
68: // for each current domain substitute set of principal with subject's
69: int cur = 0;
70: if (currentDomains != null) {
71:
72: Set<Principal> s = subject.getPrincipals();
73: Principal[] p = s.toArray(new Principal[s.size()]);
74:
75: for (cur = 0; cur < currentDomains.length; cur++) {
76: ProtectionDomain newPD;
77: newPD = new ProtectionDomain(currentDomains[cur]
78: .getCodeSource(), currentDomains[cur]
79: .getPermissions(), currentDomains[cur]
80: .getClassLoader(), p);
81: pd[cur] = newPD;
82: }
83: }
84:
85: // copy assigned domains
86: if (assignedDomains != null) {
87: System.arraycopy(assignedDomains, 0, pd, cur,
88: assignedDomains.length);
89: }
90:
91: return pd;
92: }
93: }
|