KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > bridge > JBossServletContextProvider


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.bridge;
10
11 import org.apache.portals.bridges.common.ServletContextProvider;
12 import org.jboss.portal.server.PortalRequest;
13 import org.jboss.portal.server.PortalResponse;
14
15 import javax.servlet.ServletContext JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import javax.portlet.GenericPortlet;
19 import javax.portlet.PortletRequest;
20 import javax.portlet.PortletResponse;
21
22 /**
23  * The JBoss implementation of <code>org.apache.portals.bridges.common.ServletContextProvider</code> use thread local variables
24  * to keep the request associated with the current thread of execution.
25  *
26  * @author <a HREF="mailto:dr@vizuri.com">Swarn Dhaliwal</a>
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.2 $
29  */

30 public class JBossServletContextProvider implements ServletContextProvider
31 {
32
33    private static final ThreadLocal JavaDoc local = new ThreadLocal JavaDoc();
34
35    public static void set(BridgeInfo info)
36    {
37       local.set(info);
38    }
39
40    public static BridgeInfo get()
41    {
42       return (BridgeInfo)local.get();
43    }
44
45    /**
46     * @throws IllegalStateException if no bridge info is found
47     */

48    public ServletContext JavaDoc getServletContext(GenericPortlet genericPortlet) throws IllegalStateException JavaDoc
49    {
50       BridgeInfo info = ((BridgeInfo)local.get());
51       if (info == null)
52       {
53          throw new IllegalStateException JavaDoc("No bridge set");
54       }
55       return info.ctx;
56    }
57
58    /**
59     * @throws IllegalStateException if no bridge info is found
60     */

61    public HttpServletRequest JavaDoc getHttpServletRequest(GenericPortlet genericPortlet, PortletRequest portletRequest) throws IllegalStateException JavaDoc
62    {
63       BridgeInfo info = (BridgeInfo)JBossServletContextProvider.local.get();
64       if (info == null)
65       {
66          throw new IllegalStateException JavaDoc("No bridge set");
67       }
68       if (info.breq == null)
69       {
70          init(info);
71       }
72       return info.breq;
73    }
74
75    /**
76     * @throws IllegalStateException if no bridge info is found
77     */

78    public HttpServletResponse JavaDoc getHttpServletResponse(GenericPortlet genericPortlet, PortletResponse portletResponse) throws IllegalStateException JavaDoc
79    {
80       BridgeInfo info = (BridgeInfo)JBossServletContextProvider.local.get();
81       if (info == null)
82       {
83          throw new IllegalStateException JavaDoc("No bridge set");
84       }
85       if (info.breq == null)
86       {
87          init(info);
88       }
89       return info.bresp;
90    }
91
92    /**
93     * Lazy initialisation of the bridge info.
94     */

95    private void init(BridgeInfo bridgeInfo)
96    {
97       PortletRequest rreq = (PortletRequest)bridgeInfo.hreq.getAttribute("javax.portlet.request");
98       PortletResponse rresp = (PortletResponse)bridgeInfo.hreq.getAttribute("javax.portlet.response");
99       bridgeInfo.breq = new BridgeRequest(bridgeInfo.preq, rreq, bridgeInfo.hreq);
100       bridgeInfo.bresp = new BridgeResponse(bridgeInfo.preq, bridgeInfo.presp, rresp, bridgeInfo.hresp);
101    }
102
103    public static class BridgeInfo
104    {
105       /** Servlet context of the dispatched application. */
106       private final ServletContext JavaDoc ctx;
107
108       /** Portlet request made to the application. */
109       private final PortalRequest preq;
110
111       /** Portlet response made to the application. */
112       private final PortalResponse presp;
113
114       /** The dispatched request. */
115       private final HttpServletRequest JavaDoc hreq;
116
117       /** The dispatched response. */
118       private final HttpServletResponse JavaDoc hresp;
119
120       /** The bridge response. */
121       private BridgeRequest breq;
122
123       /** The bridge response. */
124       private BridgeResponse bresp;
125
126       public BridgeInfo(ServletContext JavaDoc ctx, PortalRequest preq, PortalResponse presp, HttpServletRequest JavaDoc hreq, HttpServletResponse JavaDoc hresp)
127       {
128          this.ctx = ctx;
129          this.preq = preq;
130          this.presp = presp;
131          this.hreq = hreq;
132          this.hresp = hresp;
133          this.breq = null;
134          this.bresp = null;
135       }
136    }
137 }
138
Popular Tags