KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > InternalJettyServletHolder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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
19 package org.apache.geronimo.jetty6;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.ServletRequest JavaDoc;
24 import javax.servlet.ServletResponse JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.UnavailableException JavaDoc;
27 import javax.security.auth.Subject JavaDoc;
28
29 import org.mortbay.jetty.servlet.ServletHolder;
30 import org.apache.geronimo.jetty6.handler.AbstractImmutableHandler;
31 import org.apache.geronimo.jetty6.handler.LifecycleCommand;
32 import org.apache.geronimo.security.Callers;
33 import org.apache.geronimo.security.ContextManager;
34
35 /**
36  * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
37  */

38 public class InternalJettyServletHolder extends ServletHolder {
39
40     private static final ThreadLocal JavaDoc<String JavaDoc> currentServletName = new ThreadLocal JavaDoc<String JavaDoc>();
41
42     private final AbstractImmutableHandler lifecycleChain;
43     private final Subject JavaDoc runAsSubject;
44
45     public InternalJettyServletHolder(AbstractImmutableHandler lifecycleChain, Subject JavaDoc runAsSubject) {
46         this.lifecycleChain = lifecycleChain;
47         this.runAsSubject = runAsSubject;
48     }
49
50     //TODO probably need to override init and destroy (?) to handle runAsSubject since we are not setting it in the superclass any more.
51

52     /**
53      * Service a request with this servlet. Set the ThreadLocal to hold the
54      * current JettyServletHolder.
55      */

56     public void handle(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
57             throws ServletException JavaDoc, UnavailableException JavaDoc, IOException JavaDoc {
58         String JavaDoc oldServletName = getCurrentServletName();
59         setCurrentServletName(getName());
60         try {
61             if (runAsSubject == null) {
62                 super.handle(request, response);
63             } else {
64                 Callers oldCallers = ContextManager.pushNextCaller(runAsSubject);
65                 try {
66                     super.handle(request, response);
67                 } finally {
68                     ContextManager.popCallers(oldCallers);
69                 }
70             }
71         } finally {
72             setCurrentServletName(oldServletName);
73         }
74     }
75
76     /**
77      * Provide the thread's current JettyServletHolder
78      *
79      * @return the thread's current JettyServletHolder
80      * @see org.apache.geronimo.jetty6.JAASJettyRealm#isUserInRole(java.security.Principal, java.lang.String)
81      */

82     static String JavaDoc getCurrentServletName() {
83         return currentServletName.get();
84     }
85
86     static void setCurrentServletName(String JavaDoc servletName) {
87         currentServletName.set(servletName);
88     }
89
90     public void doStart() throws Exception JavaDoc {
91         LifecycleCommand lifecycleCommand = new StartCommand();
92         lifecycleChain.lifecycleCommand(lifecycleCommand);
93     }
94
95     public void doStop() {
96         LifecycleCommand lifecycleCommand = new StopCommand();
97         try {
98             lifecycleChain.lifecycleCommand(lifecycleCommand);
99         } catch (Exception JavaDoc e) {
100             //ignore????
101
}
102     }
103
104     private void internalDoStart() throws Exception JavaDoc {
105         super.doStart();
106     }
107
108     private void internalDoStop() {
109         super.doStop();
110     }
111
112     public class StartCommand implements LifecycleCommand {
113
114         public void lifecycleMethod() throws Exception JavaDoc {
115             internalDoStart();
116         }
117     }
118
119     public class StopCommand implements LifecycleCommand {
120
121         public void lifecycleMethod() throws Exception JavaDoc {
122             internalDoStop();
123         }
124     }
125
126 }
127
Popular Tags