KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jass > examples > travelagency > ejbs > TravelAgencyBean


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.ejbs;
53
54 import javax.activity.opennested.UserOpenNested;
55 import javax.ejb.EJBException JavaDoc;
56 import javax.ejb.SessionBean JavaDoc;
57 import javax.ejb.SessionContext JavaDoc;
58 import javax.naming.Context JavaDoc;
59 import javax.naming.InitialContext JavaDoc;
60
61 import org
62
    .objectweb
63     .jass
64     .examples
65     .travelagency
66     .exceptions
67     .NotEnoughRoomsException;
68 import org
69
    .objectweb
70     .jass
71     .examples
72     .travelagency
73     .exceptions
74     .NotEnoughSeatsException;
75
76 /**
77  * Makes the composed reservation by invoking the Airline and Hotel EJBs.
78  * It creates a ONT activity that scopes both invocations through the
79  * OpenNested service.
80  * @author fran
81  * Date: Feb 16, 2004
82  * org.objectweb.jass.examples.travelagency.ejbsTravelAgencyBean.java
83  */

84 public class TravelAgencyBean implements SessionBean JavaDoc {
85
86     private Airline airline;
87     private Hotel hotel;
88     private UserOpenNested uon;
89
90     public void ejbCreate() throws javax.ejb.CreateException JavaDoc {
91         airline = getAdaptAirlineEJB();
92         hotel = getAdaptHotelEJB();
93         uon = getUserOpenNested();
94         System.out.println("Travel Agency stateless EJB created!!!");
95     }
96
97     public void ejbActivate() throws EJBException JavaDoc {
98     }
99
100     public void ejbPassivate() throws EJBException JavaDoc {
101     }
102
103     public void ejbRemove() throws EJBException JavaDoc {
104         airline = null;
105         hotel = null;
106         uon = null;
107         System.out.println("Travel Agency stateless EJB removed!!!");
108     }
109
110     public void setSessionContext(SessionContext JavaDoc sessionContext)
111         throws EJBException JavaDoc {
112
113     }
114
115     /**
116      * Creates a ONT activity and invokes the Airline and the Hotel EJBs.
117      * If is not possible to make any reservation, the activity is rolled back
118      * and an exception is thrown to the application that uses this EJB.
119      * @param name - not used.
120      * @param nSeats - number of seats to reserve.
121      * @param nRooms - number of rooms to reserve.
122      * @throws NotEnoughSeatsException - thrown if there are not enough seats.
123      * @throws NotEnoughRoomsException - thrown if there are not enough rooms.
124      */

125     public void makeReservation(String JavaDoc name, int nSeats, int nRooms)
126         throws Exception JavaDoc {
127
128         try {
129             uon.activityBegin(0);
130             airline.reserveSeats(nSeats);
131             hotel.reserveRooms(nRooms);
132             uon.activityCommit(null);
133         } catch (NotEnoughSeatsException e) {
134             uon.activityRollback();
135             throw e;
136         } catch (NotEnoughRoomsException e) {
137             uon.activityRollback();
138             throw e;
139         } catch (Exception JavaDoc e) {
140             throw e;
141         }
142     }
143
144     /**
145      * Gets the reference to the UserOpenNested service.
146      */

147     private UserOpenNested getUserOpenNested() {
148         Context JavaDoc ctx = null;
149
150         try {
151             ctx = new InitialContext JavaDoc();
152             uon = (UserOpenNested) ctx.lookup("UserOpenNested");
153         } catch (Exception JavaDoc e) {
154             e.printStackTrace();
155         }
156         return uon;
157     }
158
159     /**
160      * Gets the reference to the Airline EJB.
161      */

162     private Airline getAdaptAirlineEJB() {
163         Object JavaDoc obj = null;
164         Context JavaDoc ctx = null;
165         try {
166             ctx = new InitialContext JavaDoc();
167             obj = ctx.lookup("AirlineEJB");
168             AirlineHome airlineHome =
169                 (AirlineHome) javax.rmi.PortableRemoteObject.narrow(
170                     obj,
171                     AirlineHome.class);
172             airline = airlineHome.create();
173         } catch (Exception JavaDoc e) {
174             e.printStackTrace();
175         }
176         return airline;
177     }
178
179     /**
180      * Gets the reference to the Hotel EJB.
181      */

182     private Hotel getAdaptHotelEJB() {
183         Object JavaDoc obj = null;
184         Context JavaDoc ctx = null;
185         try {
186             ctx = new InitialContext JavaDoc();
187             obj = ctx.lookup("HotelEJB");
188             HotelHome hotelHome =
189                 (HotelHome) javax.rmi.PortableRemoteObject.narrow(
190                     obj,
191                     HotelHome.class);
192             hotel = hotelHome.create();
193         } catch (Exception JavaDoc e) {
194             e.printStackTrace();
195         }
196         return hotel;
197     }
198
199 }
200
Popular Tags