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     */
017    
018    
019    package org.apache.geronimo.jetty6;
020    
021    import java.io.IOException;
022    
023    import javax.security.auth.Subject;
024    import javax.servlet.ServletException;
025    import javax.servlet.ServletRequest;
026    import javax.servlet.ServletResponse;
027    import javax.servlet.UnavailableException;
028    
029    import org.apache.geronimo.jetty6.handler.AbstractImmutableHandler;
030    import org.apache.geronimo.jetty6.handler.LifecycleCommand;
031    import org.apache.geronimo.security.Callers;
032    import org.apache.geronimo.security.ContextManager;
033    import org.mortbay.jetty.servlet.ServletHolder;
034    
035    /**
036     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
037     */
038    public class InternalJettyServletHolder extends ServletHolder {
039    
040        private static final ThreadLocal<String> currentServletName = new ThreadLocal<String>();
041    
042        private final AbstractImmutableHandler lifecycleChain;
043        private final Subject runAsSubject;
044        private final JettyServletRegistration servletRegistration;
045        private boolean stopped;
046    
047        public InternalJettyServletHolder(AbstractImmutableHandler lifecycleChain, Subject runAsSubject, 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.pushNextCaller(runAsSubject);
068                    try {
069                        super.handle(request, response);
070                    } finally {
071                        ContextManager.popCallers(oldCallers);
072                    }
073                }
074            } finally {
075                setCurrentServletName(oldServletName);
076            }
077        }
078    
079    
080        public synchronized Object newInstance() throws InstantiationException, IllegalAccessException {
081            return servletRegistration.newInstance(_className);
082        }
083    
084        public void destroyInstance(Object o) throws Exception {
085            if (!stopped) {
086                super.destroyInstance(o);
087                servletRegistration.destroyInstance(o);
088            }
089        }
090    
091        /**
092         * Provide the thread's current JettyServletHolder
093         *
094         * @return the thread's current JettyServletHolder
095         * @see org.apache.geronimo.jetty6.JAASJettyRealm#isUserInRole(java.security.Principal,java.lang.String)
096         */
097        static String getCurrentServletName() {
098            return currentServletName.get();
099        }
100    
101        static void setCurrentServletName(String servletName) {
102            currentServletName.set(servletName);
103        }
104    
105        public void doStart() throws Exception {
106            LifecycleCommand lifecycleCommand = new StartCommand();
107            lifecycleChain.lifecycleCommand(lifecycleCommand);
108        }
109    
110        public void doStop() {
111            LifecycleCommand lifecycleCommand = new StopCommand();
112            try {
113                lifecycleChain.lifecycleCommand(lifecycleCommand);
114            } catch (Exception e) {
115                //ignore????
116            }
117        }
118    
119        private void internalDoStart() throws Exception {
120            super.doStart();
121        }
122    
123        private void internalDoStop() {
124            super.doStop();
125            stopped = true;
126        }
127    
128        public class StartCommand implements LifecycleCommand {
129    
130            public void lifecycleMethod() throws Exception {
131                internalDoStart();
132            }
133        }
134    
135        public class StopCommand implements LifecycleCommand {
136    
137            public void lifecycleMethod() throws Exception {
138                internalDoStop();
139            }
140        }
141    
142    }