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.tomcat.interceptor;
018
019 import java.util.Set;
020
021 import javax.resource.ResourceException;
022 import javax.servlet.ServletRequest;
023 import javax.servlet.ServletResponse;
024
025 import org.apache.geronimo.connector.outbound.connectiontracking.TrackedConnectionAssociator;
026 import org.apache.geronimo.connector.outbound.connectiontracking.SharedConnectorInstanceContext;
027
028 public class InstanceContextBeforeAfter implements BeforeAfter{
029
030 private final BeforeAfter next;
031 private final int oldIndex;
032 private final int newIndex;
033 private final Set unshareableResources;
034 private final Set applicationManagedSecurityResources;
035 private final TrackedConnectionAssociator trackedConnectionAssociator;
036
037 public InstanceContextBeforeAfter(BeforeAfter next, int oldIndex, int newIndex, Set unshareableResources, Set applicationManagedSecurityResources, TrackedConnectionAssociator trackedConnectionAssociator) {
038 this.next = next;
039 this.oldIndex = oldIndex;
040 this.newIndex = newIndex;
041 this.unshareableResources = unshareableResources;
042 this.applicationManagedSecurityResources = applicationManagedSecurityResources;
043 this.trackedConnectionAssociator = trackedConnectionAssociator;
044 }
045
046 public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse, int dispatch) {
047 try {
048 SharedConnectorInstanceContext newConnectorInstanceContext = new SharedConnectorInstanceContext(unshareableResources, applicationManagedSecurityResources, false);
049 SharedConnectorInstanceContext oldContext = (SharedConnectorInstanceContext) trackedConnectionAssociator.enter(newConnectorInstanceContext);
050 if (oldContext != null) {
051 newConnectorInstanceContext.share(oldContext);
052 }
053 context[oldIndex] = oldContext;
054 context[newIndex] = newConnectorInstanceContext;
055 } catch (ResourceException e) {
056 throw new RuntimeException(e);
057 }
058 if (next != null) {
059 next.before(context, httpRequest, httpResponse, dispatch);
060 }
061 }
062
063 public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse, int dispatch) {
064 if (next != null) {
065 next.after(context, httpRequest, httpResponse, dispatch);
066 }
067 try {
068 SharedConnectorInstanceContext oldConnectorInstanceContext = (SharedConnectorInstanceContext) context[oldIndex];
069 SharedConnectorInstanceContext newConnectorInstanceContext = (SharedConnectorInstanceContext) context[newIndex];
070 if (oldConnectorInstanceContext != null) {
071 newConnectorInstanceContext.hide();
072 }
073 trackedConnectionAssociator.exit(oldConnectorInstanceContext);
074 } catch (ResourceException e) {
075 throw new RuntimeException(e);
076 }
077 }
078 }