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.jetty6.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    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.geronimo.clustering.ClusteredInvocation;
029    import org.apache.geronimo.clustering.ClusteredInvocationException;
030    import org.apache.geronimo.jetty6.cluster.AbstractClusteredPreHandler;
031    import org.codehaus.wadi.core.contextualiser.InvocationException;
032    import org.codehaus.wadi.core.manager.Manager;
033    import org.codehaus.wadi.web.impl.WebInvocation;
034    
035    
036    /**
037     * 
038     * @version $Rev$ $Date$
039     */
040    public class WADIClusteredPreHandler extends AbstractClusteredPreHandler {
041        private final Manager wadiManager;
042    
043        public WADIClusteredPreHandler(Manager wadiManager) {
044            this.wadiManager = wadiManager;
045        }
046        
047        protected ClusteredInvocation newClusteredInvocation(String target, HttpServletRequest request,
048                HttpServletResponse response, int dispatch) {
049            return new WADIWebClusteredInvocation(target, request, response, dispatch);
050        }
051        
052        protected class WADIWebClusteredInvocation extends WebClusteredInvocation {
053            
054            public WADIWebClusteredInvocation(String target, HttpServletRequest request,
055                    HttpServletResponse response, int dispatch) {
056                super(target, request, response, dispatch);
057            }
058    
059            public void invoke() throws ClusteredInvocationException {
060                WebInvocation invocation = new WebInvocation(5000);
061                FilterChain chainAdapter = new FilterChain() {
062                    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
063                        try {
064                            invokeLocally();
065                        } catch (ClusteredInvocationException e) {
066                            throw (IOException) new IOException().initCause(e);
067                        }
068                    }
069                };
070                invocation.init(request, response, chainAdapter);
071                try {
072                    wadiManager.contextualise(invocation);
073                } catch (InvocationException e) {
074                    Throwable throwable = e.getCause();
075                    if (throwable instanceof IOException) {
076                        throw new ClusteredInvocationException(throwable);
077                    } else if (throwable instanceof ServletException) {
078                        throw new ClusteredInvocationException(throwable);
079                    } else {
080                        throw new ClusteredInvocationException(e);
081                    }
082                }
083            }
084        }
085        
086    }