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: 522036 $ $Date: 2007-03-24 10:38:03 -0400 (Sat, 24 Mar 2007) $
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                servletRegistration.destroyInstance(o);
087            }
088        }
089    
090        /**
091         * Provide the thread's current JettyServletHolder
092         *
093         * @return the thread's current JettyServletHolder
094         * @see org.apache.geronimo.jetty6.JAASJettyRealm#isUserInRole(java.security.Principal,java.lang.String)
095         */
096        static String getCurrentServletName() {
097            return currentServletName.get();
098        }
099    
100        static void setCurrentServletName(String servletName) {
101            currentServletName.set(servletName);
102        }
103    
104        public void doStart() throws Exception {
105            LifecycleCommand lifecycleCommand = new StartCommand();
106            lifecycleChain.lifecycleCommand(lifecycleCommand);
107        }
108    
109        public void doStop() {
110            LifecycleCommand lifecycleCommand = new StopCommand();
111            try {
112                lifecycleChain.lifecycleCommand(lifecycleCommand);
113            } catch (Exception e) {
114                //ignore????
115            }
116        }
117    
118        private void internalDoStart() throws Exception {
119            super.doStart();
120        }
121    
122        private void internalDoStop() {
123            super.doStop();
124            stopped = true;
125        }
126    
127        public class StartCommand implements LifecycleCommand {
128    
129            public void lifecycleMethod() throws Exception {
130                internalDoStart();
131            }
132        }
133    
134        public class StopCommand implements LifecycleCommand {
135    
136            public void lifecycleMethod() throws Exception {
137                internalDoStop();
138            }
139        }
140    
141    }