001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jetty6;
017:
018: import java.io.IOException;
019:
020: import javax.security.auth.Subject;
021: import javax.servlet.ServletException;
022: import javax.servlet.ServletRequest;
023: import javax.servlet.ServletResponse;
024: import javax.servlet.UnavailableException;
025:
026: import org.apache.geronimo.jetty6.handler.AbstractImmutableHandler;
027: import org.apache.geronimo.jetty6.handler.LifecycleCommand;
028: import org.apache.geronimo.security.Callers;
029: import org.apache.geronimo.security.ContextManager;
030: import org.mortbay.jetty.servlet.ServletHolder;
031:
032: /**
033: * @version $Rev: 566471 $ $Date: 2007-08-15 19:45:46 -0700 (Wed, 15 Aug 2007) $
034: */
035: public class InternalJettyServletHolder extends ServletHolder {
036:
037: private static final ThreadLocal<String> currentServletName = new ThreadLocal<String>();
038:
039: private final AbstractImmutableHandler lifecycleChain;
040: private final Subject runAsSubject;
041: private final JettyServletRegistration servletRegistration;
042: private boolean stopped;
043:
044: public InternalJettyServletHolder(
045: AbstractImmutableHandler lifecycleChain,
046: Subject runAsSubject,
047: JettyServletRegistration servletRegistration) {
048: this .lifecycleChain = lifecycleChain;
049: this .runAsSubject = runAsSubject;
050: this .servletRegistration = servletRegistration;
051: }
052:
053: //TODO probably need to override init and destroy (?) to handle runAsSubject since we are not setting it in the superclass any more.
054:
055: /**
056: * Service a request with this servlet. Set the ThreadLocal to hold the
057: * current JettyServletHolder.
058: */
059: public void handle(ServletRequest request, ServletResponse response)
060: throws ServletException, UnavailableException, IOException {
061: String oldServletName = getCurrentServletName();
062: setCurrentServletName(getName());
063: try {
064: if (runAsSubject == null) {
065: super .handle(request, response);
066: } else {
067: Callers oldCallers = ContextManager
068: .pushNextCaller(runAsSubject);
069: try {
070: super .handle(request, response);
071: } finally {
072: ContextManager.popCallers(oldCallers);
073: }
074: }
075: } finally {
076: setCurrentServletName(oldServletName);
077: }
078: }
079:
080: public synchronized Object newInstance()
081: throws InstantiationException, IllegalAccessException {
082: return servletRegistration.newInstance(_className);
083: }
084:
085: public void destroyInstance(Object o) throws Exception {
086: if (!stopped) {
087: super .destroyInstance(o);
088: servletRegistration.destroyInstance(o);
089: }
090: }
091:
092: /**
093: * Provide the thread's current JettyServletHolder
094: *
095: * @return the thread's current JettyServletHolder
096: * @see org.apache.geronimo.jetty6.JAASJettyRealm#isUserInRole(java.security.Principal,java.lang.String)
097: */
098: static String getCurrentServletName() {
099: return currentServletName.get();
100: }
101:
102: static void setCurrentServletName(String servletName) {
103: currentServletName.set(servletName);
104: }
105:
106: public void doStart() throws Exception {
107: LifecycleCommand lifecycleCommand = new StartCommand();
108: lifecycleChain.lifecycleCommand(lifecycleCommand);
109: }
110:
111: public void doStop() {
112: LifecycleCommand lifecycleCommand = new StopCommand();
113: try {
114: lifecycleChain.lifecycleCommand(lifecycleCommand);
115: } catch (Exception e) {
116: //ignore????
117: }
118: }
119:
120: private void internalDoStart() throws Exception {
121: super .doStart();
122: }
123:
124: private void internalDoStop() {
125: super .doStop();
126: stopped = true;
127: }
128:
129: public class StartCommand implements LifecycleCommand {
130:
131: public void lifecycleMethod() throws Exception {
132: internalDoStart();
133: }
134: }
135:
136: public class StopCommand implements LifecycleCommand {
137:
138: public void lifecycleMethod() throws Exception {
139: internalDoStop();
140: }
141: }
142:
143: }
|