KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jass > examples > travelagency > servlets > ReservationServlet


1 /**
2  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3  -
4  - JASS: Java Advanced tranSaction Support
5  -
6  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7  -
8  - This module was originally developed by
9  -
10  - LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
11  - at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
12  - (http://www.objectweb.org) project.
13  -
14  - This project has been partially funded by the European Commission under
15  - the IST programme of V FP grant IST-2001-37126 and by the Spanish
16  - Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
17  - TIC2001-1586-C03-02
18  -
19  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20  - The original code and portions created by LSD are
21  - Copyright (c) 2004 LSD (UPM)
22  - All rights reserved.
23  -
24  - Redistribution and use in source and binary forms, with or without
25  - modification, are permitted provided that the following conditions are met:
26  -
27  - -Redistributions of source code must retain the above copyright notice, this
28  - list of conditions and the following disclaimer.
29  -
30  - -Redistributions in binary form must reproduce the above copyright notice,
31  - this list of conditions and the following disclaimer in the documentation
32  - and/or other materials provided with the distribution.
33  -
34  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38  - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41  - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42  - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43  - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  - POSSIBILITY OF SUCH DAMAGE.
45  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46  -
47  - Author: Francisco Perez Sorrosal (frperezs)
48  -
49  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50 */

51
52 package org.objectweb.jass.examples.travelagency.servlets;
53
54 import org.objectweb.jass.examples.travelagency.ejbs.AirlineHome;
55 import org.objectweb.jass.examples.travelagency.ejbs.Airline;
56 import org.objectweb.jass.examples.travelagency.ejbs.HotelHome;
57 import org.objectweb.jass.examples.travelagency.ejbs.Hotel;
58 import org.objectweb.jass.examples.travelagency.ejbs.TravelAgency;
59 import org.objectweb.jass.examples.travelagency.ejbs.TravelAgencyHome;
60 import org.objectweb.jass.examples.travelagency.exceptions.DBException;
61 import org
62
    .objectweb
63     .jass
64     .examples
65     .travelagency
66     .exceptions
67     .NotEnoughSeatsException;
68 import org
69
    .objectweb
70     .jass
71     .examples
72     .travelagency
73     .exceptions
74     .NotEnoughRoomsException;
75
76 import javax.servlet.http.HttpServlet JavaDoc;
77 import javax.servlet.http.HttpServletRequest JavaDoc;
78 import javax.servlet.http.HttpServletResponse JavaDoc;
79 import javax.naming.Context JavaDoc;
80 import javax.naming.NamingException JavaDoc;
81 import javax.naming.InitialContext JavaDoc;
82 import javax.ejb.CreateException JavaDoc;
83 import javax.activity.opennested.UserOpenNested;
84 import java.io.IOException JavaDoc;
85 import java.io.PrintWriter JavaDoc;
86 import java.rmi.RemoteException JavaDoc;
87
88 /**
89  * Creates the application web interface by invoking the corresponding EJBs
90  * (TravelAgencyEJB, AirlineEJB and HotelEJB).
91  * @author fran
92  * Date: Feb 16, 2004
93  * org.objectweb.jass.examples.travelagency.servletsReservationServlet.java
94  */

95 public class ReservationServlet extends HttpServlet JavaDoc {
96     private Airline airline;
97     private Hotel hotel;
98     private TravelAgency travelAgency;
99     private UserOpenNested uon;
100
101     /**
102      * Invokes methods to obtain the required EJBs references
103      * (TravelAgencyEJB, AirlineEJB and HotelEJB).
104      */

105     public void init() {
106         airline = getAdaptAirlineEJB();
107         hotel = getAdaptHotelEJB();
108         travelAgency = getAdaptTravelAgencyEJB();
109         System.out.println("Reservation Servlet. Init called!!!");
110     }
111
112     /**
113      * Executed on servlet destruction.
114      */

115     public void destroy() {
116         System.out.println("Reservation Servlet. Destroy called!!!");
117     }
118
119     /**
120      * Servlet main method.
121      */

122     public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
123         throws IOException JavaDoc {
124         res.setContentType("text/html");
125         PrintWriter JavaDoc out = res.getWriter();
126
127         out.println(
128             "<HTML><HEAD></HEAD><BODY>"
129                 + "<H1><U>ADAPT RESERVATION SERVICE</U></H1>");
130
131         String JavaDoc action = req.getParameter("action");
132         if (action == null) {
133             try {
134                 showFlightInfo(out);
135                 showHotelInfo(out);
136                 showRequest(out);
137             } catch (DBException e) {
138                 out.println(
139                     "<H2><FONT COLOR=\"red\">Database ERROR!!!" + "</FONT></H2>");
140             }
141         } else {
142             if (action.equals("reserve")) {
143                 String JavaDoc name = req.getParameter("name");
144                 String JavaDoc nSeats = req.getParameter("number_of_seats");
145                 String JavaDoc nRooms = req.getParameter("number_of_rooms");
146                 makeReservation(
147                     out,
148                     name,
149                     Integer.parseInt(nSeats),
150                     Integer.parseInt(nRooms));
151             }
152         }
153         insertReturn(out);
154         out.println("</BODY></HTML>");
155     }
156
157     /**
158      * Shows the number of free seats in the flight.
159      * @param out
160      */

161     private void showFlightInfo(PrintWriter JavaDoc out) throws DBException {
162         int freeSeats = 0;
163
164         try {
165             freeSeats = airline.getFreeSeats();
166         } catch (RemoteException JavaDoc e) {
167             e.printStackTrace();
168         }
169
170         out.println(
171             "<CENTER><H3>Flight Info (ADPT001):</H3>"
172                 + "<FONT COLOR=\"red\"><U>Free</U> seats in flight: </FONT>"
173                 + freeSeats
174                 + "</CENTER><HR SIZE=3>");
175     }
176
177     /**
178      * Shows the number of free rooms in the hotel.
179      * @param out
180      */

181     private void showHotelInfo(PrintWriter JavaDoc out) throws DBException {
182         int freeRooms = 0;
183
184         try {
185             freeRooms = hotel.getFreeRooms();
186         } catch (RemoteException JavaDoc e) {
187             e.printStackTrace();
188         }
189         
190         out.println(
191             "<CENTER><H3>Hotel Info (ADAPT PALACE *****):</H3>"
192                 + "<FONT COLOR=\"red\"><U>Free</U> rooms in hotel: </FONT>"
193                 + freeRooms
194                 + "</CENTER><HR SIZE=3>");
195     }
196
197     /**
198      * Shows application instructions and reservation request form.
199      * @param out
200      */

201     private void showRequest(PrintWriter JavaDoc out) {
202         out.println(
203             "<BR><FONT COLOR=\"red\"><U>Instructions:</U></FONT> Please, "
204                 + "insert below your name and the number of seats "
205                 + "and rooms that you want to reserve. If it is possible, the system "
206                 + "will made the corresponding reserves for you -attending to the "
207                 + "values shown above- and it will report to you the results or any "
208                 + "possible failure.<BR><BR>");
209         out.println(
210             "<FORM METHOD=\"POST\" ACTION=\"reservation\"> "
211                 + "<H3>Reservation Info:</H3>"
212                 + "Reservation Name:<INPUT TYPE=\"TEXT\" NAME=\"name\"><BR><BR>"
213                 + "Flight info (Number of seats): <INPUT TYPE=\"TEXT\" NAME=\"number_of_seats\"><BR>"
214                 + "Hotel info (Number of rooms): <INPUT TYPE=\"TEXT\" NAME=\"number_of_rooms\"><BR>"
215                 + "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"reserve\">"
216                 + "<BR><INPUT TYPE=\"RESET\" VALUE=\"Clear\">"
217                 + "<INPUT TYPE=\"SUBMIT\" VALUE=\"Send\">"
218                 + "</FORM>");
219         out.println("</CENTER><HR SIZE=3>");
220     }
221
222     /**
223      * Inserts the a form with a button to return to the main application page.
224      * @param out
225      */

226     private void insertReturn(PrintWriter JavaDoc out) {
227         out.println(
228             "<FORM METHOD=\"POST\" ACTION=\"reservation\"> "
229                 + "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go Reservation Page\"> "
230                 + "</FORM>");
231     }
232
233     /**
234      * Gets the reference to the Airline EJB.
235      */

236     private Airline getAdaptAirlineEJB() {
237         Object JavaDoc obj = null;
238         Context JavaDoc ctx = null;
239         Airline airline = null;
240         try {
241             ctx = new InitialContext JavaDoc();
242             obj = ctx.lookup("AirlineEJB");
243             AirlineHome airlineHome =
244                 (AirlineHome) javax.rmi.PortableRemoteObject.narrow(
245                     obj,
246                     AirlineHome.class);
247             airline = airlineHome.create();
248         } catch (NamingException JavaDoc e) {
249             e.printStackTrace();
250         } catch (CreateException JavaDoc e) {
251             e.printStackTrace();
252         } catch (RemoteException JavaDoc e) {
253             e.printStackTrace();
254         }
255         return airline;
256     }
257
258     /**
259      * Gets the reference to the Hotel EJB.
260      */

261     private Hotel getAdaptHotelEJB() {
262         Object JavaDoc obj = null;
263         Context JavaDoc ctx = null;
264         Hotel hotel = null;
265         try {
266             ctx = new InitialContext JavaDoc();
267             obj = ctx.lookup("HotelEJB");
268             HotelHome hotelHome =
269                 (HotelHome) javax.rmi.PortableRemoteObject.narrow(
270                     obj,
271                     HotelHome.class);
272             hotel = hotelHome.create();
273         } catch (NamingException JavaDoc e) {
274             e.printStackTrace();
275         } catch (CreateException JavaDoc e) {
276             e.printStackTrace();
277         } catch (RemoteException JavaDoc e) {
278             e.printStackTrace();
279         }
280         return hotel;
281     }
282
283     /**
284      * Gets the reference to the TravelAgency EJB.
285      */

286     private TravelAgency getAdaptTravelAgencyEJB() {
287         Object JavaDoc obj = null;
288         Context JavaDoc ctx = null;
289         TravelAgency travelAgency = null;
290         try {
291             ctx = new InitialContext JavaDoc();
292             obj = ctx.lookup("TravelAgencyEJB");
293             TravelAgencyHome travelAgencyHome =
294                 (TravelAgencyHome) javax.rmi.PortableRemoteObject.narrow(
295                     obj,
296                     TravelAgencyHome.class);
297             travelAgency = travelAgencyHome.create();
298         } catch (NamingException JavaDoc e) {
299             e.printStackTrace();
300         } catch (CreateException JavaDoc e) {
301             e.printStackTrace();
302         } catch (RemoteException JavaDoc e) {
303             e.printStackTrace();
304         }
305         return travelAgency;
306     }
307
308     /**
309      * Invokes the reservation method on the TravelAgency EJB showing
310      * the results.
311      * @param out
312      * @param name
313      * @param nSeats
314      * @param nRooms
315      */

316     private void makeReservation(
317         PrintWriter JavaDoc out,
318         String JavaDoc name,
319         int nSeats,
320         int nRooms) {
321         try {
322             travelAgency.makeReservation(name, nSeats, nRooms);
323             out.println(
324                 "<CENTER><H2>Congratulations!!!</H2>"
325                     + "<H3>Customer <FONT COLOR=\"red\">"
326                     + name
327                     + "</FONT> has reserved <FONT COLOR=\"red\">"
328                     + nSeats
329                     + "</FONT> seats and <FONT COLOR=\"red\">"
330                     + nRooms
331                     + "</FONT> rooms</H3></CENTER>");
332         } catch (RemoteException JavaDoc e) {
333             e.printStackTrace();
334         } catch (NotEnoughSeatsException e) {
335             out.println(
336                 "<CENTER><H2>An ERROR has occurred!!!</H2><BR>"
337                     + "<H3><FONT COLOR=\"red\">Not enough seats in flight!!!</FONT></H3></CENTER>");
338         } catch (NotEnoughRoomsException e) {
339             out.println(
340                 "<CENTER><H2>An ERROR has occurred!!!</H2><BR>"
341                     + "<H3><FONT COLOR=\"red\">Not enough rooms in hotel!!!</FONT></H3></CENTER>");
342         } catch (DBException e) {
343             out.println(
344                 "<H2><FONT COLOR=\"red\">Database ERROR!!!" + "</FONT></H2>");
345         } catch (Exception JavaDoc e) {
346             out.println(
347                 "<H2><FONT COLOR=\"red\">Unespected ERROR!!!" + "</FONT></H2>");
348         }
349     }
350 }
351
Popular Tags