KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > servlet > 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.servlet.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.NamingEnumeration JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /** A servlet that accesses an EJB and tests whether the call argument
47  is serialized.
48
49  @author Scott.Stark@jboss.org
50  @version $Revision: 56581 $
51  */

52 public class EJBServlet extends HttpServlet JavaDoc
53 {
54    private static final Logger log = Logger.getLogger(EJBServlet.class);
55    
56    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57          throws ServletException JavaDoc, IOException JavaDoc
58    {
59       try
60       {
61          SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
62          SecurityAssociation.setCredential("password".toCharArray());
63          
64          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
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 JavaDoc e)
99       {
100          e.printStackTrace();
101         
102          throw new ServletException JavaDoc("Failed to call OptimizedEJB/Session30 through remote and local interfaces", e);
103       }
104       response.setContentType("text/html");
105       PrintWriter JavaDoc 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 JavaDoc name)
114    {
115       log.info("lookup " + name);
116       try {
117          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
118          NamingEnumeration JavaDoc names = jndiContext.list(name);
119          if (names != null){
120             while (names.hasMore()){
121                log.info(" " + names.next());
122             }
123          }
124       } catch (Exception JavaDoc e){
125       }
126    }
127
128    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
129          throws ServletException JavaDoc, IOException JavaDoc
130    {
131       processRequest(request, response);
132    }
133
134    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
135          throws ServletException JavaDoc, IOException JavaDoc
136    {
137       processRequest(request, response);
138    }
139 }
140
Popular Tags