001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.tomcat.cluster.wadi;
021    
022    import java.io.BufferedReader;
023    import java.io.IOException;
024    import java.io.UnsupportedEncodingException;
025    import java.security.Principal;
026    import java.util.Enumeration;
027    import java.util.Locale;
028    import java.util.Map;
029    
030    import javax.servlet.FilterChain;
031    import javax.servlet.RequestDispatcher;
032    import javax.servlet.ServletException;
033    import javax.servlet.ServletInputStream;
034    import javax.servlet.ServletRequest;
035    import javax.servlet.ServletResponse;
036    import javax.servlet.http.Cookie;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpSession;
039    
040    import org.apache.catalina.connector.Request;
041    import org.apache.catalina.connector.Response;
042    import org.apache.geronimo.clustering.ClusteredInvocation;
043    import org.apache.geronimo.clustering.ClusteredInvocationException;
044    import org.apache.geronimo.tomcat.cluster.AbstractClusteredValve;
045    import org.codehaus.wadi.core.contextualiser.InvocationException;
046    import org.codehaus.wadi.core.manager.Manager;
047    import org.codehaus.wadi.web.impl.WebInvocation;
048    
049    
050    /**
051     * 
052     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
053     */
054    public class WADIClusteredValve extends AbstractClusteredValve {
055        private final Manager wadiManager;
056    
057        public WADIClusteredValve(Manager wadiManager) {
058            this.wadiManager = wadiManager;
059        }
060        
061        protected ClusteredInvocation newClusteredInvocation(Request request, Response response) {
062            return new WADIWebClusteredInvocation(request, response);
063        }
064        
065        protected class WADIWebClusteredInvocation extends WebClusteredInvocation {
066            
067            public WADIWebClusteredInvocation(Request request, Response response) {
068                super(request, response);
069            }
070    
071            public void invoke() throws ClusteredInvocationException {
072                WebInvocation invocation = new WebInvocation(5000);
073                FilterChain chainAdapter = new FilterChain() {
074                    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
075                        try {
076                            invokeLocally();
077                        } catch (ClusteredInvocationException e) {
078                            throw (IOException) new IOException().initCause(e);
079                        }
080                    }
081                };
082                invocation.init(null == request? NoOpHttpServletRequest.SINGLETON: request, response, chainAdapter);
083                try {
084                    wadiManager.contextualise(invocation);
085                } catch (InvocationException e) {
086                    Throwable throwable = e.getCause();
087                    if (throwable instanceof IOException) {
088                        throw new ClusteredInvocationException(throwable);
089                    } else if (throwable instanceof ServletException) {
090                        throw new ClusteredInvocationException(throwable);
091                    } else {
092                        throw new ClusteredInvocationException(e);
093                    }
094                }
095            }
096        }
097    
098        protected static class NoOpHttpServletRequest implements HttpServletRequest {
099            public static final NoOpHttpServletRequest SINGLETON = new NoOpHttpServletRequest();
100    
101            public String getAuthType() {
102                throw new UnsupportedOperationException();
103            }
104    
105            public String getContextPath() {
106                throw new UnsupportedOperationException();
107            }
108    
109            public Cookie[] getCookies() {
110                throw new UnsupportedOperationException();
111            }
112    
113            public long getDateHeader(String arg0) {
114                throw new UnsupportedOperationException();
115            }
116    
117            public String getHeader(String arg0) {
118                throw new UnsupportedOperationException();
119            }
120    
121            public Enumeration getHeaderNames() {
122                throw new UnsupportedOperationException();
123            }
124    
125            public Enumeration getHeaders(String arg0) {
126                throw new UnsupportedOperationException();
127            }
128    
129            public int getIntHeader(String arg0) {
130                throw new UnsupportedOperationException();
131            }
132    
133            public String getMethod() {
134                throw new UnsupportedOperationException();
135            }
136    
137            public String getPathInfo() {
138                throw new UnsupportedOperationException();
139            }
140    
141            public String getPathTranslated() {
142                throw new UnsupportedOperationException();
143            }
144    
145            public String getQueryString() {
146                throw new UnsupportedOperationException();
147            }
148    
149            public String getRemoteUser() {
150                throw new UnsupportedOperationException();
151            }
152    
153            public String getRequestURI() {
154                throw new UnsupportedOperationException();
155            }
156    
157            public StringBuffer getRequestURL() {
158                throw new UnsupportedOperationException();
159            }
160    
161            public String getRequestedSessionId() {
162                return null;
163            }
164    
165            public String getServletPath() {
166                throw new UnsupportedOperationException();
167            }
168    
169            public HttpSession getSession() {
170                throw new UnsupportedOperationException();
171            }
172    
173            public HttpSession getSession(boolean arg0) {
174                throw new UnsupportedOperationException();
175            }
176    
177            public Principal getUserPrincipal() {
178                throw new UnsupportedOperationException();
179            }
180    
181            public boolean isRequestedSessionIdFromCookie() {
182                throw new UnsupportedOperationException();
183            }
184    
185            public boolean isRequestedSessionIdFromURL() {
186                throw new UnsupportedOperationException();
187            }
188    
189            public boolean isRequestedSessionIdFromUrl() {
190                throw new UnsupportedOperationException();
191            }
192    
193            public boolean isRequestedSessionIdValid() {
194                throw new UnsupportedOperationException();
195            }
196    
197            public boolean isUserInRole(String arg0) {
198                throw new UnsupportedOperationException();
199            }
200    
201            public Object getAttribute(String arg0) {
202                throw new UnsupportedOperationException();
203            }
204    
205            public Enumeration getAttributeNames() {
206                throw new UnsupportedOperationException();
207            }
208    
209            public String getCharacterEncoding() {
210                throw new UnsupportedOperationException();
211            }
212    
213            public int getContentLength() {
214                throw new UnsupportedOperationException();
215            }
216    
217            public String getContentType() {
218                throw new UnsupportedOperationException();
219            }
220    
221            public ServletInputStream getInputStream() throws IOException {
222                throw new UnsupportedOperationException();
223            }
224    
225            public String getLocalAddr() {
226                throw new UnsupportedOperationException();
227            }
228    
229            public String getLocalName() {
230                throw new UnsupportedOperationException();
231            }
232    
233            public int getLocalPort() {
234                throw new UnsupportedOperationException();
235            }
236    
237            public Locale getLocale() {
238                throw new UnsupportedOperationException();
239            }
240    
241            public Enumeration getLocales() {
242                throw new UnsupportedOperationException();
243            }
244    
245            public String getParameter(String arg0) {
246                throw new UnsupportedOperationException();
247            }
248    
249            public Map getParameterMap() {
250                throw new UnsupportedOperationException();
251            }
252    
253            public Enumeration getParameterNames() {
254                throw new UnsupportedOperationException();
255            }
256    
257            public String[] getParameterValues(String arg0) {
258                throw new UnsupportedOperationException();
259            }
260    
261            public String getProtocol() {
262                throw new UnsupportedOperationException();
263            }
264    
265            public BufferedReader getReader() throws IOException {
266                throw new UnsupportedOperationException();
267            }
268    
269            public String getRealPath(String arg0) {
270                throw new UnsupportedOperationException();
271            }
272    
273            public String getRemoteAddr() {
274                throw new UnsupportedOperationException();
275            }
276    
277            public String getRemoteHost() {
278                throw new UnsupportedOperationException();
279            }
280    
281            public int getRemotePort() {
282                throw new UnsupportedOperationException();
283            }
284    
285            public RequestDispatcher getRequestDispatcher(String arg0) {
286                throw new UnsupportedOperationException();
287            }
288    
289            public String getScheme() {
290                throw new UnsupportedOperationException();
291            }
292    
293            public String getServerName() {
294                throw new UnsupportedOperationException();
295            }
296    
297            public int getServerPort() {
298                throw new UnsupportedOperationException();
299            }
300    
301            public boolean isSecure() {
302                throw new UnsupportedOperationException();
303            }
304    
305            public void removeAttribute(String arg0) {
306                throw new UnsupportedOperationException();
307            }
308    
309            public void setAttribute(String arg0, Object arg1) {
310                throw new UnsupportedOperationException();
311            }
312    
313            public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
314                throw new UnsupportedOperationException();
315            }
316            
317        }
318        
319    }