Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/gssapi/jaas/GlobusSubject.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.gssapi.jaas;
16
17 import javax.security.auth.Subject;
18 import java.security.PrivilegedAction;
19 import java.security.PrivilegedExceptionAction;
20 import java.security.PrivilegedActionException;
21 import java.util.LinkedList;
22
23 /**
24 * An implementation of the <code>JaasSubject</code> API to circumvent
25 * the JAAS problem of Subject propagation. The implementation uses
26 * a stackable version of
27 * {@link java.lang.InheritableThreadLocal InheritableThreadLocal}
28 * class to associate the Subject object with the current thread.
29 * Any new thread started within a thread that has a Subject object
30 * associated with it, will inherit the parent's Subject object.
31 * Also, nested <code>doAs</code>, <code>runAs</code> calls are supported.
32 */
33 public class GlobusSubject extends JaasSubject {
34
35 private static StackableInheritableThreadLocal subjects =
36 new StackableInheritableThreadLocal();
37
38 protected GlobusSubject() {
39 super();
40 }
41
42 public Subject getSubject() {
43 return (Subject)subjects.peek();
44 }
45
46 public Object runAs(Subject subject, PrivilegedAction action) {
47 subjects.push(subject);
48 try {
49 return Subject.doAs(subject, action);
50 } finally {
51 subjects.pop();
52 }
53 }
54
55 public Object runAs(Subject subject, PrivilegedExceptionAction action)
56 throws PrivilegedActionException {
57 subjects.push(subject);
58 try {
59 return Subject.doAs(subject, action);
60 } finally {
61 subjects.pop();
62 }
63 }
64
65 }
66
67 class StackableInheritableThreadLocal extends InheritableThreadLocal {
68
69 protected Object initialValue() {
70 return new LinkedList();
71 }
72
73 protected Object childValue(Object parentValue) {
74 LinkedList list = (LinkedList)parentValue;
75 LinkedList newList = new LinkedList();
76 if (!list.isEmpty()) {
77 newList.add(list.getLast());
78 }
79 return newList;
80 }
81
82 public void push(Object object) {
83 LinkedList list = (LinkedList)get();
84 list.add(object);
85 }
86
87 public Object pop() {
88 LinkedList list = (LinkedList)get();
89 return (list.isEmpty()) ? null : list.removeLast();
90 }
91
92 public Object peek() {
93 LinkedList list = (LinkedList)get();
94 return (list.isEmpty()) ? null : list.getLast();
95 }
96
97 }