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    package org.apache.geronimo.jetty.cluster.wadi;
018    
019    import java.io.IOException;
020    
021    import javax.servlet.FilterChain;
022    import javax.servlet.ServletException;
023    import javax.servlet.ServletRequest;
024    import javax.servlet.ServletResponse;
025    
026    import org.apache.geronimo.clustering.ClusteredInvocation;
027    import org.apache.geronimo.clustering.ClusteredInvocationException;
028    import org.apache.geronimo.clustering.wadi.WADISessionManager;
029    import org.apache.geronimo.gbean.GBeanInfo;
030    import org.apache.geronimo.gbean.GBeanInfoBuilder;
031    import org.apache.geronimo.gbean.GBeanLifecycle;
032    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
033    import org.apache.geronimo.jetty.HandleInterceptor;
034    import org.apache.geronimo.jetty.cluster.AbstractClusteredHandleInterceptor;
035    import org.codehaus.wadi.InvocationException;
036    import org.codehaus.wadi.InvocationProxy;
037    import org.codehaus.wadi.impl.ClusteredManager;
038    import org.codehaus.wadi.web.impl.WebInvocation;
039    import org.mortbay.http.HttpRequest;
040    import org.mortbay.http.HttpResponse;
041    import org.mortbay.jetty.servlet.ServletHttpRequest;
042    import org.mortbay.jetty.servlet.ServletHttpResponse;
043    
044    
045    /**
046     * 
047     * @version $Rev$ $Date$
048     */
049    public class WADIClusteredHandleInterceptor extends AbstractClusteredHandleInterceptor implements GBeanLifecycle {
050        private final WADISessionManager sessionManager;
051        
052        private ClusteredManager wadiManager;
053    
054        public WADIClusteredHandleInterceptor(WADISessionManager sessionManager) {
055            this.sessionManager = sessionManager;
056        }
057        
058        public void doStart() throws Exception {
059            wadiManager = sessionManager.getManager();
060        }
061        
062        public void doStop() throws Exception {
063            wadiManager = null;
064        }
065        
066        public void doFail() {
067            wadiManager = null;
068        }
069        
070        protected ClusteredInvocation newClusteredInvocation(String pathInContext, String pathParams, HttpRequest request,
071                HttpResponse response, HandleInterceptor end) {
072            return new WADIWebClusteredInvocation(pathInContext, pathParams, request, response, end);
073        }
074        
075        protected class WADIWebClusteredInvocation extends WebClusteredInvocation {
076            
077            public WADIWebClusteredInvocation(String pathInContext, String pathParams, HttpRequest request,
078                    HttpResponse response, HandleInterceptor end) {
079                super(pathInContext, pathParams, request, response, end);
080            }
081    
082            public void invoke() throws ClusteredInvocationException {
083                ServletHttpRequest servletHttpRequest = (ServletHttpRequest) request.getWrapper();
084                ServletHttpResponse servletHttpResponse = (ServletHttpResponse) response.getWrapper();
085    
086                WebInvocation invocation = new WebInvocation();
087                FilterChain chainAdapter = new FilterChain() {
088                    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
089                        try {
090                            invokeLocally();
091                        } catch (ClusteredInvocationException e) {
092                            throw (IOException) new IOException().initCause(e);
093                        }
094                    }
095                };
096                InvocationProxy invocationProxy = wadiManager.getInvocationProxy();
097                invocation.init(servletHttpRequest, servletHttpResponse, chainAdapter, invocationProxy);
098                try {
099                    wadiManager.contextualise(invocation);
100                } catch (InvocationException e) {
101                    Throwable throwable = e.getCause();
102                    if (throwable instanceof IOException) {
103                        throw new ClusteredInvocationException(throwable);
104                    } else if (throwable instanceof ServletException) {
105                        throw new ClusteredInvocationException(throwable);
106                    } else {
107                        throw new ClusteredInvocationException(e);
108                    }
109                }
110            }
111        }
112        
113        public static final GBeanInfo GBEAN_INFO;
114    
115        public static final String GBEAN_REF_WADI_SESSION_MANAGER = "WADISessionManager";
116    
117        static {
118            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("WADI Clustered Handle Interceptor",
119                    WADIClusteredHandleInterceptor.class, NameFactory.GERONIMO_SERVICE);
120            
121            infoBuilder.addReference(GBEAN_REF_WADI_SESSION_MANAGER, WADISessionManager.class, 
122                    NameFactory.GERONIMO_SERVICE);
123            
124            infoBuilder.setConstructor(new String[]{GBEAN_REF_WADI_SESSION_MANAGER});
125    
126            GBEAN_INFO = infoBuilder.getBeanInfo();
127        }
128        
129        public static GBeanInfo getGBeanInfo() {
130            return GBEAN_INFO;
131        }
132    }