KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > handler > InstanceContextHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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  */

17
18
19 package org.apache.geronimo.jetty6.handler;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.resource.ResourceException JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContext;
30 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContextImpl;
31 import org.apache.geronimo.connector.outbound.connectiontracking.SharedConnectorInstanceContext;
32 import org.apache.geronimo.connector.outbound.connectiontracking.TrackedConnectionAssociator;
33 import org.mortbay.jetty.Handler;
34 import org.mortbay.jetty.handler.AbstractHandler;
35
36 /**
37  * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
38  */

39 public class InstanceContextHandler extends AbstractImmutableHandler {
40
41     private final Set JavaDoc unshareableResources;
42     private final Set JavaDoc applicationManagedSecurityResources;
43     private final TrackedConnectionAssociator trackedConnectionAssociator;
44
45     public InstanceContextHandler(AbstractHandler next, Set JavaDoc unshareableResources, Set JavaDoc applicationManagedSecurityResources, TrackedConnectionAssociator trackedConnectionAssociator) {
46         super(next);
47         this.unshareableResources = unshareableResources;
48         this.applicationManagedSecurityResources = applicationManagedSecurityResources;
49         this.trackedConnectionAssociator = trackedConnectionAssociator;
50     }
51
52     public void handle(String JavaDoc target, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, int dispatch) throws IOException JavaDoc, ServletException JavaDoc {
53         try {
54             if (dispatch == Handler.REQUEST) {
55                 ConnectorInstanceContext oldContext = trackedConnectionAssociator.enter(new SharedConnectorInstanceContext(unshareableResources, applicationManagedSecurityResources, false));
56
57                 try {
58                     next.handle(target, request, response, dispatch);
59                 } finally {
60                     trackedConnectionAssociator.exit(oldContext);
61                 }
62             } else {
63                 SharedConnectorInstanceContext context = new SharedConnectorInstanceContext(unshareableResources, applicationManagedSecurityResources, true);
64                 SharedConnectorInstanceContext oldContext = (SharedConnectorInstanceContext) trackedConnectionAssociator.enter(context);
65                 context.share(oldContext);
66                 try {
67                     next.handle(target, request, response, dispatch);
68                 } finally {
69                     context.hide();
70                     trackedConnectionAssociator.exit(oldContext);
71                 }
72             }
73         } catch (ResourceException JavaDoc e) {
74             throw new ServletException JavaDoc(e);
75         }
76     }
77
78     public void lifecycleCommand(LifecycleCommand lifecycleCommand) throws Exception JavaDoc {
79         ConnectorInstanceContext oldContext = trackedConnectionAssociator.enter(new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources));
80         try {
81             super.lifecycleCommand(lifecycleCommand);
82         } finally {
83             trackedConnectionAssociator.exit(oldContext);
84         }
85     }
86 }
87
Popular Tags