KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > web > servlets > EJBServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.dd.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.net.URL JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
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 JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.transaction.UserTransaction JavaDoc;
39 import javax.ejb.EJB JavaDoc;
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 /**
53  *
54  * tests injection annotations of @EJB, @PersistenceContext, @PersistenceUnit, etc...
55
56  @author Scott.Stark@jboss.org
57  @version $Revision: 55495 $
58  */

59 public class EJBServlet extends HttpServlet JavaDoc
60 {
61    private static final Logger log = Logger.getLogger(EJBServlet.class);
62
63    @EJB JavaDoc 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 JavaDoc tx;
69
70    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
71    {
72        String JavaDoc param = config.getInitParameter("failOnError");
73        boolean failOnError = true;
74        if( param != null && Boolean.valueOf(param).booleanValue() == false )
75            failOnError = false;
76        try
77        {
78            // Access the Util.configureLog4j() method to test classpath resource
79
URL JavaDoc propsURL = Util.configureLog4j();
80            log.debug("log4j.properties = "+propsURL);
81        }
82        catch(Exception JavaDoc e)
83        {
84            log.debug("failed", e);
85            ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
86            try
87            {
88               log.debug(Util.displayClassLoaders(loader));
89            }
90            catch(NamingException JavaDoc ne)
91            {
92               log.debug("failed", ne);
93            }
94            if( failOnError == true )
95                throw new ServletException JavaDoc("Failed to init EJBOnStartupServlet", e);
96        }
97    }
98    
99    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
100          throws ServletException JavaDoc, IOException JavaDoc
101    {
102       try
103       {
104          SecurityAssociation.setPrincipal(new SimplePrincipal("jduke"));
105          SecurityAssociation.setCredential("theduke".toCharArray());
106          
107          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
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 JavaDoc access = session30.access();
117          access = injectedSession30.access();
118                
119          //TODO EMPTYSTRING can't be right - correct lookup when fixed
120
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          //tx = (UserTransaction)ctx.lookup("UserTransaction");
141
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 JavaDoc e)
150       {
151          if (tx != null)
152          {
153             try {
154                tx.rollback();
155             } catch (Exception JavaDoc e1){
156               
157             }
158          }
159          throw new ServletException JavaDoc("Failed to call OptimizedEJB/Session30 through remote and local interfaces", e);
160       }
161       if (overridenConstant != 42) throw new RuntimeException JavaDoc("@Resource constant not overriden by XML");
162       if (nonOverridentConstant != 5) throw new RuntimeException JavaDoc("@Resource constant should not have been overriden");
163       response.setContentType("text/html");
164       PrintWriter JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
173          throws ServletException JavaDoc, IOException JavaDoc
174    {
175       processRequest(request, response);
176    }
177
178    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
179          throws ServletException JavaDoc, IOException JavaDoc
180    {
181       processRequest(request, response);
182    }
183 }
184
Popular Tags