KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > earsample > servlets > ServletOp


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ServletOp.java,v 1.11 2005/06/14 14:39:26 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.earsample.servlets;
27
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.net.URL JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.rmi.PortableRemoteObject JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServlet JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.transaction.UserTransaction JavaDoc;
40
41 import org.objectweb.earsample.beans.secusb.Op;
42 import org.objectweb.earsample.beans.secusb.OpHome;
43 import org.objectweb.earsample.beans.secusb.OpLocal;
44 import org.objectweb.earsample.beans.secusb.OpLocalHome;
45
46 /**
47  * This servlet is an example to show how to access a EJB from a servlet
48  * @author JOnAS team
49  */

50 public class ServletOp extends HttpServlet JavaDoc {
51
52     /**
53      * First amount to buy
54      */

55     private static final int FIRST_BUY_AMOUNT = 10;
56
57     /**
58      * Second amount to buy
59      */

60     private static final int SECOND_BUY_AMOUNT = 20;
61
62     /**
63      * Third amount to buy (will be rollback)
64      */

65     private static final int THIRD_BUY_AMOUNT = 50;
66
67
68     /**
69      * Called by the server (via the service method) to allow a servlet to
70      * handle a GET request.
71      * @param request an HttpServletRequest object that contains the request
72      * the client has made of the servlet
73      * @param response an HttpServletResponse object that contains the
74      * response the servlet sends to the client
75      * @throws IOException if an input or output error is detected when the
76      * servlet handles the GET request
77      * @throws ServletException if the request for the GET could not be handled
78      */

79     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
80         throws IOException JavaDoc, ServletException JavaDoc {
81
82         response.setContentType("text/html");
83         PrintWriter JavaDoc out = response.getWriter();
84         out.println("<html>");
85         out.println("<head>");
86         out.println("<link type=\"text/css\" HREF=\"../ow_jonas.css\" rel=\"stylesheet\" id=\"stylesheet\">");
87         out.println("<title>");
88         out.println("Ear Sample of Servlet accessing a protected EJB</title>");
89         out.println("</head>");
90         out.println("<body style=\"background : white; color : black;\">");
91         out.println("<h1>Ear sample of Servlet accessing a protected EJB");
92         out.println("</h1><img SRC=\"../img/tomcat.gif\" alt=\"Tomcat Logo\">");
93         out.println("<img SRC=\"../img/jetty.gif\" alt=\"Jetty Logo\">");
94         out.println("<img SRC=\"../img/ow_jonas_logo.gif\" alt=\"JOnAS Logo\">");
95         out.println("<h3>Initial context / UserTransaction </h3>");
96         out.println("<ul>");
97         Context JavaDoc initialContext = null;
98         try {
99             initialContext = new InitialContext JavaDoc();
100             out.println("<li>Initial context OK</li>");
101         } catch (Exception JavaDoc e) {
102             out.print("<li>Cannot get initial context for JNDI: ");
103             out.println(e + "</li>");
104             return;
105         }
106
107         // We want to start transactions from client: get UserTransaction
108
UserTransaction JavaDoc utx = null;
109         try {
110             utx = (UserTransaction JavaDoc) initialContext.lookup("java:comp/UserTransaction");
111             out.println("<li>Get java:comp/UserTransaction OK</li>");
112         } catch (Exception JavaDoc e) {
113             out.println("<li>Cannot lookup java:comp/UserTransaction: " + e + "</li>");
114             return;
115         }
116         out.println("</ul><br />");
117
118         out.println("<h3>");
119         out.println("Lookup on env-entry with java:comp/env/envEntryString");
120         out.println("</h3>");
121         out.println("<ul>");
122         String JavaDoc envEntry = null;
123         try {
124             envEntry = (String JavaDoc) initialContext.lookup("java:comp/env/envEntryString");
125             out.println("<li>Env entry is : " + envEntry + "</li>");
126         } catch (Exception JavaDoc e) {
127             out.println("<li>Cannot get env-entry on JNDI " + e + "</li>");
128             return;
129         }
130         out.println("</ul><br />");
131
132
133         out.println("</ul><br />");
134
135         out.println("<h3>");
136         out.println("Lookup on URL with java:comp/env/url/URL");
137         out.println("</h3>");
138         out.println("<ul>");
139         URL JavaDoc jonasURL = null;
140         try {
141             jonasURL = (URL JavaDoc) initialContext.lookup("java:comp/env/url/URL");
142             out.println("<li>URL is : <a HREF=\"" + jonasURL.toString() + ">" + jonasURL.toString() + "</a></li>");
143         } catch (Exception JavaDoc e) {
144             out.println("<li>Cannot get url on JNDI " + e + "</li>");
145             return;
146         }
147         out.println("</ul><br />");
148
149         out.println("<h3>Actions realized by the servlet and the EJB</h3>");
150         out.println("<h3>With local interface</h3>");
151         out.println("<ul>");
152         // Connecting to OpLocalHome thru JNDI
153
OpLocalHome opLocalHome = null;
154         try {
155             opLocalHome = (OpLocalHome)
156                 initialContext.lookup("java:comp/env/ejb/OpLocal");
157         } catch (Exception JavaDoc e) {
158             out.println("<li>Cannot lookup java:comp/env/ejb/OpLocal: " + e + "</li>");
159             return;
160         }
161         // OpBean creation
162
OpLocal opLocal = null;
163         try {
164             opLocal = opLocalHome.create("User1");
165             out.println("<li>Create a bean</li>");
166         } catch (Exception JavaDoc e) {
167             out.println("<li>Cannot create OpBean: " + e + "</li>");
168             return;
169         }
170         // First transaction (committed)
171
try {
172             out.println("<li>Start a first transaction</li>");
173             utx.begin();
174             opLocal.buy(FIRST_BUY_AMOUNT);
175             out.println("<li>First request on the new bean</li>");
176             opLocal.buy(SECOND_BUY_AMOUNT);
177             out.println("<li>Second request on the bean</li>");
178             utx.commit();
179             out.println("<li>Commit the transaction</li>");
180         } catch (Exception JavaDoc e) {
181             out.println("<li>exception during 1st Tx: " + e + "</li>");
182             return;
183         }
184         // Start another transaction (rolled back)
185
try {
186             out.println("<li>Start a second transaction</li>");
187             utx.begin();
188             opLocal.buy(THIRD_BUY_AMOUNT);
189             utx.rollback();
190             out.println("<li>Rollback the transaction</li>");
191         } catch (Exception JavaDoc e) {
192             out.println("<li>exception during 2nd Tx: " + e + "</li>");
193             return;
194         }
195         // Get the total bought, outside the transaction
196
int val = 0;
197         try {
198             val = opLocal.read();
199             out.println("<li>Request outside any transaction</li>");
200         } catch (Exception JavaDoc e) {
201             out.println("<li>Cannot read value on t1 : " + e + "</li>");
202             return;
203         }
204         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
205             out.println("<li>Bad value read: " + val + "</li>");
206             return;
207         }
208         // Remove Session bean
209
try {
210             opLocal.remove();
211         } catch (Exception JavaDoc e) {
212             out.println("<li>Exception on buy: " + e + "</li>");
213             return;
214         }
215         out.println("</ul><br />");
216
217         out.println("<h3>With remote interface</h3>");
218         out.println("<ul>");
219         // Connecting to OpHome thru JNDI
220
OpHome opHome = null;
221         try {
222             opHome = (OpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Op"), OpHome.class);
223         } catch (Exception JavaDoc e) {
224             out.println("<li>Cannot lookup java:comp/env/ejb/Op: " + e + "</li>");
225             return;
226         }
227         // OpBean creation
228
Op op = null;
229         try {
230             op = opHome.create("User1");
231             out.println("<li>Create a bean</li>");
232         } catch (Exception JavaDoc e) {
233             out.println("<li>Cannot create OpBean: " + e + "</li>");
234             return;
235         }
236         // First transaction (committed)
237
try {
238             out.println("<li>Start a first transaction</li>");
239             utx.begin();
240             op.buy(FIRST_BUY_AMOUNT);
241             out.println("<li>First request on the new bean</li>");
242             op.buy(SECOND_BUY_AMOUNT);
243             out.println("<li>Second request on the bean</li>");
244             utx.commit();
245             out.println("<li>Commit the transaction</li>");
246         } catch (Exception JavaDoc e) {
247             out.println("<li>exception during 1st Tx: " + e + "</li>");
248             return;
249         }
250         // Start another transaction (rolled back)
251
try {
252             out.println("<li>Start a second transaction</li>");
253             utx.begin();
254             op.buy(THIRD_BUY_AMOUNT);
255             utx.rollback();
256             out.println("<li>Rollback the transaction</li>");
257         } catch (Exception JavaDoc e) {
258             out.println("<li>exception during 2nd Tx: " + e + "</li>");
259             return;
260         }
261         // Get the total bought, outside the transaction
262
val = 0;
263         try {
264             val = op.read();
265             out.println("<li>Request outside any transaction</li>");
266         } catch (Exception JavaDoc e) {
267             out.println("<li>Cannot read value on t1 : " + e + "</li>");
268             return;
269         }
270         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
271             out.println("<li>Bad value read: " + val + "</li>");
272             return;
273         }
274         // Remove Session bean
275
try {
276             op.remove();
277         } catch (Exception JavaDoc e) {
278             out.println("<li>Exception on buy: " + e + "</li>");
279             return;
280         }
281         out.println("</ul><br />");
282
283
284
285         out.println("<h3>");
286         out.println("Resource Adapter Properties");
287         out.println("</h3>");
288         out.println("<ul>");
289         String JavaDoc eisName = null;
290         try {
291             eisName = (String JavaDoc) initialContext.lookup("eisName");
292             out.println("<li>EIS Name is : " + eisName + "</li>");
293         } catch (Exception JavaDoc e) {
294             out.println("<li>Cannot get eis name on JNDI " + e + "</li>");
295             return;
296         }
297         out.println("</ul><br />");
298
299
300         out.println("<strong>Sample is OK.</strong><br />");
301         out.println("</body>");
302         out.println("</html>");
303     }
304 }
305
Popular Tags