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: */package org.apache.geronimo.tomcat.listener;
17:
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: import javax.security.auth.Subject;
22:
23: import org.apache.catalina.Container;
24: import org.apache.catalina.InstanceEvent;
25: import org.apache.catalina.InstanceListener;
26: import org.apache.catalina.Wrapper;
27: import org.apache.geronimo.security.Callers;
28: import org.apache.geronimo.security.ContextManager;
29: import org.apache.geronimo.tomcat.GeronimoStandardContext;
30:
31: public class RunAsInstanceListener implements InstanceListener {
32:
33: private static final ThreadLocal<List<Callers>> threadLocal = new ThreadLocal<List<Callers>>() {
34: protected List<Callers> initialValue() {
35: return new ArrayList<Callers>(2);
36: }
37: };
38:
39: public void instanceEvent(InstanceEvent event) {
40:
41: if (event.getType().equals(InstanceEvent.BEFORE_SERVICE_EVENT)) {
42: Container parent = event.getWrapper().getParent();
43: if (parent instanceof GeronimoStandardContext) {
44: GeronimoStandardContext context = (GeronimoStandardContext) parent;
45: Wrapper wrapper = event.getWrapper();
46: String runAsRole = wrapper.getRunAs();
47: Subject runAsSubject = context
48: .getSubjectForRole(runAsRole);
49: List<Callers> callersStack = threadLocal.get();
50: if (runAsSubject != null) {
51: Callers oldCallers = ContextManager
52: .pushNextCaller(runAsSubject);
53: callersStack.add(oldCallers);
54: } else {
55: callersStack.add(null);
56: }
57: }
58: }
59:
60: else if (event.getType().equals(
61: InstanceEvent.AFTER_SERVICE_EVENT)) {
62: List<Callers> callersStack = threadLocal.get();
63: Callers oldCallers = callersStack.remove(callersStack
64: .size() - 1);
65: if (oldCallers != null) {
66: ContextManager.popCallers(oldCallers);
67: }
68: }
69: }
70: }
|