1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.geronimo.jetty.cluster.wadi; 19 20 import java.io.IOException; 21 22 import javax.servlet.FilterChain; 23 import javax.servlet.ServletException; 24 import javax.servlet.ServletRequest; 25 import javax.servlet.ServletResponse; 26 27 import org.apache.geronimo.clustering.ClusteredInvocation; 28 import org.apache.geronimo.clustering.ClusteredInvocationException; 29 import org.apache.geronimo.clustering.wadi.WADISessionManager; 30 import org.apache.geronimo.gbean.GBeanInfo; 31 import org.apache.geronimo.gbean.GBeanInfoBuilder; 32 import org.apache.geronimo.gbean.GBeanLifecycle; 33 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 34 import org.apache.geronimo.jetty.HandleInterceptor; 35 import org.apache.geronimo.jetty.cluster.AbstractClusteredHandleInterceptor; 36 import org.codehaus.wadi.InvocationException; 37 import org.codehaus.wadi.impl.ClusteredManager; 38 import org.codehaus.wadi.web.impl.WebInvocation; 39 import org.mortbay.http.HttpRequest; 40 import org.mortbay.http.HttpResponse; 41 import org.mortbay.jetty.servlet.ServletHttpRequest; 42 import org.mortbay.jetty.servlet.ServletHttpResponse; 43 44 45 /** 46 * 47 * @version $Rev$ $Date$ 48 */ 49 public class WADIClusteredHandleInterceptor extends AbstractClusteredHandleInterceptor implements GBeanLifecycle { 50 private final WADISessionManager sessionManager; 51 52 private ClusteredManager wadiManager; 53 54 public WADIClusteredHandleInterceptor(WADISessionManager sessionManager) { 55 this.sessionManager = sessionManager; 56 } 57 58 public void doStart() throws Exception { 59 wadiManager = sessionManager.getManager(); 60 } 61 62 public void doStop() throws Exception { 63 wadiManager = null; 64 } 65 66 public void doFail() { 67 wadiManager = null; 68 } 69 70 protected ClusteredInvocation newClusteredInvocation(String pathInContext, String pathParams, HttpRequest request, 71 HttpResponse response, HandleInterceptor end) { 72 return new WADIWebClusteredInvocation(pathInContext, pathParams, request, response, end); 73 } 74 75 protected class WADIWebClusteredInvocation extends WebClusteredInvocation { 76 77 public WADIWebClusteredInvocation(String pathInContext, String pathParams, HttpRequest request, 78 HttpResponse response, HandleInterceptor end) { 79 super(pathInContext, pathParams, request, response, end); 80 } 81 82 public void invoke() throws ClusteredInvocationException { 83 ServletHttpRequest servletHttpRequest = (ServletHttpRequest) request.getWrapper(); 84 ServletHttpResponse servletHttpResponse = (ServletHttpResponse) response.getWrapper(); 85 86 WebInvocation invocation = WebInvocation.getThreadLocalInstance(); 87 FilterChain chainAdapter = new FilterChain() { 88 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { 89 try { 90 invokeLocally(); 91 } catch (ClusteredInvocationException e) { 92 throw (IOException) new IOException().initCause(e); 93 } 94 } 95 }; 96 invocation.init(servletHttpRequest, servletHttpResponse, chainAdapter); 97 try { 98 wadiManager.contextualise(invocation); 99 } catch (InvocationException e) { 100 Throwable throwable = e.getCause(); 101 if (throwable instanceof IOException) { 102 throw new ClusteredInvocationException(throwable); 103 } else if (throwable instanceof ServletException) { 104 throw new ClusteredInvocationException(throwable); 105 } else { 106 throw new ClusteredInvocationException(e); 107 } 108 } 109 } 110 } 111 112 public static final GBeanInfo GBEAN_INFO; 113 114 public static final String GBEAN_REF_WADI_SESSION_MANAGER = "WADISessionManager"; 115 116 static { 117 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("WADI Clustered Handle Interceptor", 118 WADIClusteredHandleInterceptor.class, NameFactory.GERONIMO_SERVICE); 119 120 infoBuilder.addReference(GBEAN_REF_WADI_SESSION_MANAGER, WADISessionManager.class, 121 NameFactory.GERONIMO_SERVICE); 122 123 infoBuilder.setConstructor(new String[]{GBEAN_REF_WADI_SESSION_MANAGER}); 124 125 GBEAN_INFO = infoBuilder.getBeanInfo(); 126 } 127 128 public static GBeanInfo getGBeanInfo() { 129 return GBEAN_INFO; 130 } 131 }