1 22 package org.jboss.ejb3.test.servlet.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.net.URL ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingEnumeration ; 29 import javax.naming.NamingException ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.jboss.ejb3.Container; 37 import org.jboss.ejb3.test.servlet.Session30; 38 import org.jboss.ejb3.test.servlet.Session30LocalHome; 39 import org.jboss.ejb3.test.servlet.Session30Home; 40 import org.jboss.ejb3.test.servlet.TestObject; 41 import org.jboss.ejb3.test.servlet.WarTestObject; 42 import org.jboss.logging.Logger; 43 import org.jboss.security.SecurityAssociation; 44 import org.jboss.security.SimplePrincipal; 45 46 52 public class EJBServlet extends HttpServlet 53 { 54 private static final Logger log = Logger.getLogger(EJBServlet.class); 55 56 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 57 throws ServletException , IOException 58 { 59 try 60 { 61 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 62 SecurityAssociation.setCredential("password".toCharArray()); 63 64 InitialContext ctx = new InitialContext (); 65 66 Session30 session = (Session30)ctx.lookup("ejb/Session30"); 67 68 session.hello(); 69 session.goodbye(); 70 71 TestObject o = new TestObject(); 72 session.access(o); 73 o = session.createTestObject(); 74 75 session = (Session30)ctx.lookup("ejb/Session30Local"); 76 o = new TestObject(); 77 session.access(o); 78 o = session.createTestObject(); 79 80 WarTestObject warObject = (WarTestObject)session.getWarTestObject(); 81 82 Session30Home home = (Session30Home)ctx.lookup("ejb/Session30Home"); 83 session = home.create(); 84 session.access(o); 85 86 Session30LocalHome localHome = (Session30LocalHome)ctx.lookup("ejb/Session30LocalHome"); 87 session = localHome.create(); 88 session.access(o); 89 90 home = (Session30Home)ctx.lookup("java:comp/env/ejb/remote/Session30"); 91 session = home.create(); 92 session.access(o); 93 94 localHome = (Session30LocalHome)ctx.lookup("java:comp/env/ejb/local/Session30"); 95 session = localHome.create(); 96 session.access(o); 97 } 98 catch (Exception e) 99 { 100 e.printStackTrace(); 101 102 throw new ServletException ("Failed to call OptimizedEJB/Session30 through remote and local interfaces", e); 103 } 104 response.setContentType("text/html"); 105 PrintWriter out = response.getWriter(); 106 out.println("<html>"); 107 out.println("<head><title>EJBServlet</title></head>"); 108 out.println("<body>Tests passed<br></body>"); 109 out.println("</html>"); 110 out.close(); 111 } 112 113 private void lookup(String name) 114 { 115 log.info("lookup " + name); 116 try { 117 InitialContext jndiContext = new InitialContext (); 118 NamingEnumeration names = jndiContext.list(name); 119 if (names != null){ 120 while (names.hasMore()){ 121 log.info(" " + names.next()); 122 } 123 } 124 } catch (Exception e){ 125 } 126 } 127 128 protected void doGet(HttpServletRequest request, HttpServletResponse response) 129 throws ServletException , IOException 130 { 131 processRequest(request, response); 132 } 133 134 protected void doPost(HttpServletRequest request, HttpServletResponse response) 135 throws ServletException , IOException 136 { 137 processRequest(request, response); 138 } 139 } 140 | Popular Tags |