1 22 package org.jboss.ejb3.test.dd.web.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.NamingException ; 29 import javax.persistence.EntityManager; 30 import javax.persistence.PersistenceContext; 31 import javax.persistence.PersistenceUnit; 32 import javax.persistence.EntityManagerFactory; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServlet ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.transaction.UserTransaction ; 39 import javax.ejb.EJB ; 40 import javax.annotation.Resource; 41 42 import org.jboss.ejb3.test.dd.web.ejb.Address; 43 import org.jboss.ejb3.test.dd.web.interfaces.ReferenceTest; 44 import org.jboss.ejb3.test.dd.web.interfaces.Session30; 45 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSession; 46 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSessionLocal; 47 import org.jboss.ejb3.test.dd.web.util.Util; 48 import org.jboss.logging.Logger; 49 import org.jboss.security.SecurityAssociation; 50 import org.jboss.security.SimplePrincipal; 51 52 59 public class EJBServlet extends HttpServlet 60 { 61 private static final Logger log = Logger.getLogger(EJBServlet.class); 62 63 @EJB Session30 injectedSession30; 64 @PersistenceContext(unitName="../dd-web-ejbs.jar#tempdb") EntityManager injectedEntityManager; 65 @PersistenceUnit(unitName="../dd-web-ejbs.jar#tempdb") EntityManagerFactory injectedEntityManagerFactory; 66 @Resource int nonOverridentConstant = 5; 67 @Resource(name="overridenConstant") int overridenConstant = 1; 68 @Resource UserTransaction tx; 69 70 public void init(ServletConfig config) throws ServletException 71 { 72 String param = config.getInitParameter("failOnError"); 73 boolean failOnError = true; 74 if( param != null && Boolean.valueOf(param).booleanValue() == false ) 75 failOnError = false; 76 try 77 { 78 URL propsURL = Util.configureLog4j(); 80 log.debug("log4j.properties = "+propsURL); 81 } 82 catch(Exception e) 83 { 84 log.debug("failed", e); 85 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 86 try 87 { 88 log.debug(Util.displayClassLoaders(loader)); 89 } 90 catch(NamingException ne) 91 { 92 log.debug("failed", ne); 93 } 94 if( failOnError == true ) 95 throw new ServletException ("Failed to init EJBOnStartupServlet", e); 96 } 97 } 98 99 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 100 throws ServletException , IOException 101 { 102 try 103 { 104 SecurityAssociation.setPrincipal(new SimplePrincipal("jduke")); 105 SecurityAssociation.setCredential("theduke".toCharArray()); 106 107 InitialContext ctx = new InitialContext (); 108 109 StatelessSession optimizedBean = (StatelessSession)ctx.lookup("java:comp/env/ejb/OptimizedEJB"); 110 optimizedBean.noop(new ReferenceTest(), false); 111 112 StatelessSessionLocal localOptimizedBean = (StatelessSessionLocal)ctx.lookup("java:comp/env/ejb/local/OptimizedEJB"); 113 localOptimizedBean.noop(new ReferenceTest(), true); 114 115 Session30 session30 = (Session30)ctx.lookup("java:comp/env/ejb/Session30"); 116 String access = session30.access(); 117 access = injectedSession30.access(); 118 119 EntityManager entityManager = (EntityManager)ctx.lookup("java:/WebDDEntityManager"); 121 122 Address address = new Address(); 123 address.setStreet("Clarendon Street"); 124 address.setCity("Boston"); 125 address.setState("MA"); 126 address.setZip("02116"); 127 128 Address address2 = new Address(); 129 address.setStreet("Clarendon Street"); 130 address.setCity("Boston"); 131 address.setState("MA"); 132 address.setZip("02116"); 133 134 Address address3 = new Address(); 135 address.setStreet("Clarendon Street"); 136 address.setCity("Boston"); 137 address.setState("MA"); 138 address.setZip("02116"); 139 140 EntityManager em = injectedEntityManagerFactory.createEntityManager(); 142 tx.begin(); 143 entityManager.persist(address); 144 injectedEntityManager.persist(address2); 145 em.persist(address3); 146 tx.commit(); 147 em.close(); 148 } 149 catch (Exception e) 150 { 151 if (tx != null) 152 { 153 try { 154 tx.rollback(); 155 } catch (Exception e1){ 156 157 } 158 } 159 throw new ServletException ("Failed to call OptimizedEJB/Session30 through remote and local interfaces", e); 160 } 161 if (overridenConstant != 42) throw new RuntimeException ("@Resource constant not overriden by XML"); 162 if (nonOverridentConstant != 5) throw new RuntimeException ("@Resource constant should not have been overriden"); 163 response.setContentType("text/html"); 164 PrintWriter out = response.getWriter(); 165 out.println("<html>"); 166 out.println("<head><title>EJBServlet</title></head>"); 167 out.println("<body>Tests passed<br>Time:" + Util.getTime() + "</body>"); 168 out.println("</html>"); 169 out.close(); 170 } 171 172 protected void doGet(HttpServletRequest request, HttpServletResponse response) 173 throws ServletException , IOException 174 { 175 processRequest(request, response); 176 } 177 178 protected void doPost(HttpServletRequest request, HttpServletResponse response) 179 throws ServletException , IOException 180 { 181 processRequest(request, response); 182 } 183 } 184 | Popular Tags |