KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > servlet > AbstractMainServlet


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.server.servlet;
10
11 import org.apache.log4j.Logger;
12 import org.jboss.mx.util.MBeanServerLocator;
13 import org.jboss.portal.common.util.Exceptions;
14 import org.jboss.portal.server.PortalRequest;
15 import org.jboss.portal.server.PortalResponse;
16 import org.jboss.portal.server.PortalServer;
17 import org.jboss.portal.server.ServerManager;
18 import org.jboss.portal.server.invocation.AttachmentKey;
19 import org.jboss.portal.server.invocation.Invocation;
20 import org.jboss.portal.server.invocation.InvocationFactory;
21
22 import javax.management.MBeanServer JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
32  * @version $Revision: 1.6 $
33  */

34 public abstract class AbstractMainServlet extends HttpServlet JavaDoc
35 {
36
37    protected Logger log = Logger.getLogger(getClass());
38
39    private PortalServer server;
40
41    protected final PortalServer getContainer()
42    {
43       if (server == null)
44       {
45          try
46          {
47             MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
48             ObjectName JavaDoc name = new ObjectName JavaDoc(getServletContext().getInitParameter("org.jboss.portal.server#SERVER_MANAGER"));
49             server = (PortalServer)mbeanServer.getAttribute(name, "PortalServer");
50          }
51          catch (Exception JavaDoc e)
52          {
53             log.error(e);
54             throw new IllegalStateException JavaDoc("Cannot get portal server");
55          }
56       }
57       return server;
58    }
59
60    protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
61    {
62       doGet(req, resp);
63    }
64
65    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
66    {
67       PortalServer container = getContainer();
68       ServerManager manager = container.getManager();
69       InvocationFactory ctx = manager.getInvocationContext();
70       Invocation invocation = ctx.decode(req, resp);
71       invoke(invocation);
72    }
73
74    protected void invoke(Invocation invocation) throws ServletException JavaDoc, IOException JavaDoc
75    {
76       try
77       {
78          getContainer().invoke(invocation);
79          PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
80          PortalResponse resp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE);
81          render(req, resp);
82       }
83       catch (IOException JavaDoc e)
84       {
85          throw e;
86       }
87       catch (ServletException JavaDoc e)
88       {
89          throw e;
90       }
91       catch (Exception JavaDoc e)
92       {
93          log.error("Exception caught in processing servlet", e);
94          Throwable JavaDoc nested = Exceptions.unwrap(e);
95          throw new ServletException JavaDoc(nested);
96       }
97    }
98
99    protected abstract void render(PortalRequest presp, PortalResponse preq) throws IOException JavaDoc, ServletException JavaDoc;
100 }
101
Popular Tags