KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.jass.examples.travelagency.exceptions.DBException;
55 import org
56
    .objectweb
57     .jass
58     .examples
59     .travelagency
60     .exceptions
61     .NotEnoughSeatsException;
62 import org
63
    .objectweb
64     .jass
65     .examples
66     .travelagency
67     .ejbs
68     .compensations
69     .ReserveSeatsCompensation;
70
71 import javax.ejb.EJBException JavaDoc;
72 import javax.ejb.SessionBean JavaDoc;
73 import javax.ejb.SessionContext JavaDoc;
74 import javax.naming.Context JavaDoc;
75 import javax.naming.InitialContext JavaDoc;
76 import javax.naming.NamingException JavaDoc;
77 import javax.sql.DataSource JavaDoc;
78 import javax.activity.opennested.UserOpenNested;
79 import java.sql.Connection JavaDoc;
80 import java.sql.Statement JavaDoc;
81 import java.sql.ResultSet JavaDoc;
82 import java.sql.SQLException JavaDoc;
83
84 /**
85  * Makes the seats reservation.
86  * It creates a ONT activity through the OpenNested service when methods that
87  * perform db changes are invoked (reserveSeats(), unreserveSeats()).
88  * @author fran
89  * Date: Feb 16, 2004
90  * org.objectweb.jass.examples.travelagency.ejbsAirlineBean.java
91  */

92 public class AirlineBean implements SessionBean JavaDoc {
93     private UserOpenNested uon = null;
94     private DataSource JavaDoc ds = null;
95
96     public void ejbCreate() throws javax.ejb.CreateException JavaDoc {
97         uon = getUserOpenNested();
98         ds = getDataSource();
99         System.out.println("Airline stateless EJB created!!!");
100     }
101
102     public void ejbActivate() throws EJBException JavaDoc {
103     }
104
105     public void ejbPassivate() throws EJBException JavaDoc {
106     }
107
108     public void ejbRemove() throws EJBException JavaDoc {
109         uon = null;
110         ds = null;
111         System.out.println("Airline stateless EJB removed!!!");
112     }
113
114     public void setSessionContext(SessionContext JavaDoc sessionContext)
115         throws EJBException JavaDoc {
116     }
117
118     /**
119      * Obtains the number of free seats.
120      * @return - the number of free seats.
121      */

122     public int getFreeSeats() throws DBException {
123         Connection JavaDoc c = null;
124         Statement JavaDoc stmt = null;
125         ResultSet JavaDoc rs = null;
126         int seats = -1;
127
128         try {
129             c = ds.getConnection();
130             stmt = c.createStatement();
131             rs = stmt.executeQuery("SELECT seats FROM flights");
132             if (rs.next()) {
133                 seats = rs.getInt(1);
134             }
135             rs.close();
136             stmt.close();
137             c.close();
138         } catch (SQLException JavaDoc e) {
139             throw new DBException();
140         }
141         return seats;
142     }
143
144     /**
145      * Performs the seats reservation. If there are not enough seats, it throws
146      * the NotEnoughSeatsException.
147      * @param nSeats - the number of seats to reserve.
148      * @return
149      * @throws NotEnoughSeatsException - thown if there are not enough seats.
150      */

151     public int reserveSeats(int nSeats)
152         throws NotEnoughSeatsException, DBException {
153         Connection JavaDoc c = null;
154         Statement JavaDoc stmt = null;
155         ResultSet JavaDoc rs = null;
156         int result = -1;
157
158         try {
159             uon.activityBegin(0);
160             c = ds.getConnection();
161             stmt = c.createStatement();
162             rs = stmt.executeQuery("SELECT seats FROM flights");
163             rs.next();
164             int freeSeats = rs.getInt(1);
165             if ((freeSeats - nSeats) < 0) {
166                 rs.close();
167                 stmt.close();
168                 c.close();
169                 uon.activityRollback();
170                 throw new NotEnoughSeatsException();
171             } else {
172                 stmt.executeUpdate(
173                     "UPDATE flights SET seats = " + (freeSeats - nSeats));
174                 ReserveSeatsCompensation compensation =
175                     new ReserveSeatsCompensation(this, nSeats);
176                 uon.activityCommit(compensation);
177             }
178             rs.close();
179             stmt.close();
180             c.close();
181             result = nSeats;
182         } catch (SQLException JavaDoc e) {
183             try {
184                 uon.activityRollback();
185             } catch (Exception JavaDoc e1) {
186                 e1.printStackTrace();
187             }
188             throw new DBException();
189         } catch (NotEnoughSeatsException e) {
190             throw e;
191         } catch (Exception JavaDoc e) {
192             e.printStackTrace();
193         }
194         return result;
195     }
196
197     /**
198      * Performs the compensation for the reserveSeats() operation.
199      * @param nSeats - the number of seats to cancel.
200      * @return
201      */

202     public int unreserveSeats(int nSeats) throws DBException {
203         Connection JavaDoc c = null;
204         Statement JavaDoc stmt = null;
205         ResultSet JavaDoc rs = null;
206         int result = -1;
207
208         try {
209             uon.activityBegin(0);
210             c = ds.getConnection();
211             stmt = c.createStatement();
212             rs = stmt.executeQuery("SELECT seats FROM flights");
213             rs.next();
214             int freeSeats = rs.getInt(1);
215             stmt.executeUpdate(
216                 "UPDATE flights SET seats = " + (freeSeats + nSeats));
217             uon.activityCommit(null);
218             rs.close();
219             stmt.close();
220             c.close();
221             result = nSeats;
222         } catch (SQLException JavaDoc e) {
223             try {
224                 uon.activityRollback();
225             } catch (Exception JavaDoc e1) {
226                 e1.printStackTrace();
227             }
228             throw new DBException();
229         } catch (Exception JavaDoc e) {
230             e.printStackTrace();
231         }
232         return result;
233     }
234
235     /**
236     * Gets the reference to the UserOpenNested service.
237     */

238     private UserOpenNested getUserOpenNested() {
239         UserOpenNested uon = null;
240         Context JavaDoc ctx = null;
241
242         try {
243             ctx = new InitialContext JavaDoc();
244             uon = (UserOpenNested) ctx.lookup("UserOpenNested");
245         } catch (Exception JavaDoc e) {
246             e.printStackTrace();
247         }
248         return uon;
249     }
250
251     /**
252     * Gets the Airline datasource.
253     */

254     private DataSource JavaDoc getDataSource() {
255         Context JavaDoc ctx = null;
256         DataSource JavaDoc ds = null;
257         try {
258             ctx = new InitialContext JavaDoc();
259             ds = (DataSource JavaDoc) ctx.lookup("java:/AdaptAirlineDS");
260         } catch (NamingException JavaDoc e) {
261             e.printStackTrace();
262         }
263         return ds;
264     }
265 }
266
Popular Tags