KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > servlet > jsp > PortalJsp


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.core.servlet.jsp;
10
11 import java.io.IOException JavaDoc;
12 import java.util.LinkedList JavaDoc;
13
14 import javax.portlet.PortletRequest;
15 import javax.servlet.ServletConfig JavaDoc;
16 import javax.servlet.ServletException JavaDoc;
17 import javax.servlet.ServletRequest JavaDoc;
18 import javax.servlet.ServletResponse JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.servlet.jsp.HttpJspPage JavaDoc;
22
23 import org.jboss.logging.Logger;
24 import org.jboss.portal.common.context.Context;
25 import org.jboss.portal.common.context.NamedContext;
26
27 /**
28  * Any JSP page using the JBoss library should extend that class. It is done by
29  * adding the following line at the top of the JSP <%@ page language="java"
30  * extends="org.jboss.portal.core.servlet.jsp.JBossJsp" %>
31  *
32  * @author <a HREF="theute@jboss.org">Thomas Heute </a> $Revision: 1.1 $
33  */

34 public abstract class PortalJsp
35       implements HttpJspPage JavaDoc
36 {
37
38    /**
39     * Key for context attribute in the portlet request.
40     */

41    public static final String JavaDoc CTX_REQUEST = "org.jboss.portal.core.context";
42
43    /**
44     * HttpServletRequest so that it can be accessed in expression language
45     * static methods
46     */

47    public static final ThreadLocal JavaDoc request = new ThreadLocal JavaDoc();
48
49    /**
50     * Stack of context, needed by expression language static methods
51     */

52    public static final ThreadLocal JavaDoc contextStack = new ThreadLocal JavaDoc();
53
54    /**
55     * To log JSP related information
56     */

57    public static Logger log = Logger.getLogger(PortalJsp.class);
58
59    private ServletConfig JavaDoc config;
60
61    /**
62     * HttpJspPage implementation
63     *
64     * @see javax.servlet.jsp.HttpJspPage#_jspService(javax.servlet.http.HttpServletRequest,
65     * javax.servlet.http.HttpServletResponse)
66     */

67    abstract public void _jspService(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc,
68          IOException JavaDoc;
69
70    /**
71     * HttpJspPage implementation
72     *
73     * @see javax.servlet.jsp.JspPage#jspInit()
74     */

75    public void jspInit()
76    {
77    }
78
79    /**
80     * HttpJspPage implementation
81     *
82     * @see javax.servlet.jsp.JspPage#jspDestroy()
83     */

84    public void jspDestroy()
85    {
86    }
87
88    /**
89     * HttpJspPage implementation
90     *
91     * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
92     */

93    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
94    {
95       this.config = config;
96       jspInit();
97    }
98
99    /**
100     * HttpJspPage implementation
101     *
102     * @see javax.servlet.Servlet#getServletConfig()
103     */

104    public ServletConfig JavaDoc getServletConfig()
105    {
106       return config;
107    }
108
109    /**
110     * HttpJspPage implementation
111     *
112     * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest,
113     * javax.servlet.ServletResponse)
114     */

115    public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
116    {
117       // Get HttpServlet* from Servlet*
118
HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc) request;
119       HttpServletResponse JavaDoc httpResponse = (HttpServletResponse JavaDoc) response;
120
121       // Keep the request and contextStack
122
Object JavaDoc formerRequest = PortalJsp.request.get();
123       Object JavaDoc formerContextStack = PortalJsp.contextStack.get();
124
125       // Initialize the ThreadLocal for contextMap
126
PortletRequest req = (PortletRequest) request.getAttribute("javax.portlet.request");
127       Object JavaDoc obj = req.getAttribute(CTX_REQUEST);
128
129       LinkedList JavaDoc stack = new LinkedList JavaDoc();
130       if (obj != null)
131       {
132          Context ctx = (Context) obj;
133          NamedContext namedCtx = new NamedContext("", ctx);
134          stack.addLast(namedCtx);
135       }
136       try
137       {
138          PortalJsp.request.set(httpRequest);
139          PortalJsp.contextStack.set(stack);
140          _jspService(httpRequest, httpResponse);
141       }
142       finally
143       {
144          PortalJsp.request.set(formerRequest);
145          PortalJsp.contextStack.set(formerContextStack);
146       }
147
148    }
149
150    /**
151     * HttpJspPage implementation
152     *
153     * @see javax.servlet.Servlet#getServletInfo()
154     */

155    public String JavaDoc getServletInfo()
156    {
157       return "JBoss JSP superclass";
158    }
159
160    /**
161     * HttpJspPage implementation
162     *
163     * @see javax.servlet.Servlet#destroy()
164     */

165    public void destroy()
166    {
167       jspDestroy();
168    }
169 }
Popular Tags