01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.jetty6.handler;
17:
18: import java.io.IOException;
19: import java.util.Set;
20:
21: import javax.resource.ResourceException;
22: import javax.servlet.ServletException;
23: import javax.servlet.http.HttpServletRequest;
24: import javax.servlet.http.HttpServletResponse;
25:
26: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContext;
27: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContextImpl;
28: import org.apache.geronimo.connector.outbound.connectiontracking.SharedConnectorInstanceContext;
29: import org.apache.geronimo.connector.outbound.connectiontracking.TrackedConnectionAssociator;
30: import org.mortbay.jetty.Handler;
31: import org.mortbay.jetty.handler.AbstractHandler;
32:
33: /**
34: * @version $Rev: 601149 $ $Date: 2007-12-04 15:30:18 -0800 (Tue, 04 Dec 2007) $
35: */
36: public class InstanceContextHandler extends AbstractImmutableHandler {
37:
38: private final Set unshareableResources;
39: private final Set applicationManagedSecurityResources;
40: private final TrackedConnectionAssociator trackedConnectionAssociator;
41:
42: public InstanceContextHandler(Handler next,
43: Set unshareableResources,
44: Set applicationManagedSecurityResources,
45: TrackedConnectionAssociator trackedConnectionAssociator) {
46: super (next);
47: this .unshareableResources = unshareableResources;
48: this .applicationManagedSecurityResources = applicationManagedSecurityResources;
49: this .trackedConnectionAssociator = trackedConnectionAssociator;
50: }
51:
52: public void handle(String target, HttpServletRequest request,
53: HttpServletResponse response, int dispatch)
54: throws IOException, ServletException {
55: try {
56: if (dispatch == Handler.REQUEST) {
57: ConnectorInstanceContext oldContext = trackedConnectionAssociator
58: .enter(new SharedConnectorInstanceContext(
59: unshareableResources,
60: applicationManagedSecurityResources,
61: false));
62:
63: try {
64: next.handle(target, request, response, dispatch);
65: } finally {
66: trackedConnectionAssociator.exit(oldContext);
67: }
68: } else {
69: SharedConnectorInstanceContext context = new SharedConnectorInstanceContext(
70: unshareableResources,
71: applicationManagedSecurityResources, true);
72: SharedConnectorInstanceContext oldContext = (SharedConnectorInstanceContext) trackedConnectionAssociator
73: .enter(context);
74: context.share(oldContext);
75: try {
76: next.handle(target, request, response, dispatch);
77: } finally {
78: context.hide();
79: trackedConnectionAssociator.exit(oldContext);
80: }
81: }
82: } catch (ResourceException e) {
83: throw new ServletException(e);
84: }
85: }
86:
87: public void lifecycleCommand(LifecycleCommand lifecycleCommand)
88: throws Exception {
89: ConnectorInstanceContext oldContext = trackedConnectionAssociator
90: .enter(new ConnectorInstanceContextImpl(
91: unshareableResources,
92: applicationManagedSecurityResources));
93: try {
94: super.lifecycleCommand(lifecycleCommand);
95: } finally {
96: trackedConnectionAssociator.exit(oldContext);
97: }
98: }
99: }
|