1 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 ; 23 import javax.management.ObjectName ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import java.io.IOException ; 29 30 34 public abstract class AbstractMainServlet extends HttpServlet 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 mbeanServer = MBeanServerLocator.locateJBoss(); 48 ObjectName name = new ObjectName (getServletContext().getInitParameter("org.jboss.portal.server#SERVER_MANAGER")); 49 server = (PortalServer)mbeanServer.getAttribute(name, "PortalServer"); 50 } 51 catch (Exception e) 52 { 53 log.error(e); 54 throw new IllegalStateException ("Cannot get portal server"); 55 } 56 } 57 return server; 58 } 59 60 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException 61 { 62 doGet(req, resp); 63 } 64 65 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException 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 , IOException 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 e) 84 { 85 throw e; 86 } 87 catch (ServletException e) 88 { 89 throw e; 90 } 91 catch (Exception e) 92 { 93 log.error("Exception caught in processing servlet", e); 94 Throwable nested = Exceptions.unwrap(e); 95 throw new ServletException (nested); 96 } 97 } 98 99 protected abstract void render(PortalRequest presp, PortalResponse preq) throws IOException , ServletException ; 100 } 101 | Popular Tags |