1 25 26 package org.objectweb.sampleCluster2.web; 27 28 import java.io.IOException ; 29 import java.util.Properties ; 30 31 import javax.ejb.Handle ; 32 import javax.rmi.PortableRemoteObject ; 33 import javax.servlet.RequestDispatcher ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpSession ; 38 39 import org.objectweb.sampleCluster2.ejb.MyEjb1; 40 import org.objectweb.sampleCluster2.ejb.MyStateful; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 49 public class SessionServlet extends AbstractServlet { 50 51 54 private static final long serialVersionUID = 1L; 55 56 63 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 64 String jonasEJBServer = null; 65 String ejbId = null; 66 String ejbTotalCallsCount = null; 67 String ejbEntityCreated = null; 68 Properties prop = null; 69 70 77 HttpSession session = req.getSession(true); 78 79 81 Integer ival = (Integer ) session.getAttribute("sessiontest.counter"); 82 if (ival == null) { ival = new Integer (1); 84 } else { 85 ival = new Integer (ival.intValue() + 1); 86 } 87 session.setAttribute("sessiontest.counter", ival); 88 89 91 MyEjb1 bean = theEJB(); 93 try { 94 prop = bean.getInfoProps(); 95 jonasEJBServer = prop.getProperty("EJB server"); 96 ejbId = prop.getProperty("EJB id"); 97 ejbTotalCallsCount = prop.getProperty("EJB total calls"); 98 ejbEntityCreated = prop.getProperty("EJB server entity created"); 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 103 Handle h = (Handle ) session.getAttribute("myStateful"); 105 MyStateful statefulBean = null; 106 if (h == null) { 107 108 statefulBean = createStatefulEJB(); 109 if (statefulBean != null) { 110 h = statefulBean.getHandle(); 111 session.setAttribute("myStateful", h); 112 statefulBean.setHTTPSessionId(session.getId()); 113 } 114 } else { 115 statefulBean = (MyStateful) PortableRemoteObject.narrow(h.getEJBObject(), MyStateful.class); 116 } 117 118 if (!session.isNew()) { 119 session.setAttribute("sessiontest.old.EJB", session.getAttribute("sessiontest.EJB")); 120 session.setAttribute("sessiontest.old.WEB", session.getAttribute("sessiontest.WEB")); 121 } 122 session.setAttribute("sessiontest.EJB", jonasEJBServer); 123 session.setAttribute("sessiontest.WEB", getMyJonasInstanceName()); 124 125 req.setAttribute("jonas.EJB.served.by", jonasEJBServer); 126 req.setAttribute("jonas.WEB.served.by", getMyJonasInstanceName()); 127 128 session.setAttribute("sessiontest.jonas.EJB_Server", jonasEJBServer); 129 session.setAttribute("sessiontest.jonas.EJB_id", ejbId); 130 session.setAttribute("sessiontest.jonas.nb_EJB_totalCalls", ejbTotalCallsCount); 131 132 134 getLogger().log(BasicLevel.INFO, "session=" + session.getId() + " on jonas=" + getMyJonasInstanceName() 135 + " - calls=" + ival + " - EJB served by jonas=" + jonasEJBServer + " - EJB total calls=" 136 + ejbTotalCallsCount); 137 138 if (statefulBean != null) { 139 try { 140 statefulBean.log("<tr>" + "<td>" + getMyJonasInstanceName() + "</td>" + "<td>" + jonasEJBServer 141 + "</td>" + "<td>" + ejbTotalCallsCount + "</td>" + "<td>" + ejbEntityCreated + "</td>" 142 + "</tr>"); 143 } catch (Exception e) { 144 getLogger().log(BasicLevel.INFO, "session=" + session.getId() 145 + " : Exception when calling MyStateful EJB (" + e.getMessage() + ")"); 146 session.removeAttribute("myStateful"); 147 } 148 } 149 153 RequestDispatcher disp = req.getRequestDispatcher("../jsp/sessionRsp.jsp"); 154 disp.forward(req, res); 155 156 return; 157 } 158 } 159 160 | Popular Tags |