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;
021
022 import java.io.IOException;
023
024 import javax.servlet.ServletException;
025
026 import org.apache.catalina.connector.Request;
027 import org.apache.catalina.connector.Response;
028 import org.apache.catalina.valves.ValveBase;
029 import org.apache.geronimo.clustering.ClusteredInvocation;
030 import org.apache.geronimo.clustering.ClusteredInvocationException;
031
032 /**
033 *
034 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
035 */
036 public abstract class AbstractClusteredValve extends ValveBase {
037
038 @Override
039 public void invoke(Request request, Response response) throws IOException, ServletException {
040 ClusteredInvocation invocation = newClusteredInvocation(request, response);
041 try {
042 invocation.invoke();
043 } catch (ClusteredInvocationException e) {
044 Throwable cause = e.getCause();
045 if (cause instanceof ServletException) {
046 throw (ServletException) cause;
047 } else if (cause instanceof IOException) {
048 throw (IOException) cause;
049 } else {
050 throw (IOException) new IOException().initCause(e);
051 }
052 }
053 }
054
055 protected abstract ClusteredInvocation newClusteredInvocation(Request request, Response response);
056
057 protected abstract class WebClusteredInvocation implements ClusteredInvocation {
058 protected final Request request;
059 protected final Response response;
060
061 protected WebClusteredInvocation(Request request, Response response) {
062 this.request = request;
063 this.response = response;
064 }
065
066 protected void invokeLocally() throws ClusteredInvocationException {
067 try {
068 next.invoke(request, response);
069 } catch (IOException e) {
070 throw new ClusteredInvocationException(e);
071 } catch (ServletException e) {
072 throw new ClusteredInvocationException(e);
073 }
074 }
075
076 public String getRequestedSessionId() {
077 if (null == request) {
078 return null;
079 }
080 return request.getRequestedSessionId();
081 }
082 }
083
084 }