KickJava   Java API By Example, From Geeks To Geeks.

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


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     .NotEnoughRoomsException;
62 import org
63
    .objectweb
64     .jass
65     .examples
66     .travelagency
67     .ejbs
68     .compensations
69     .ReserveRoomsCompensation;
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 rooms reservation.
86  * It creates a ONT activity through the OpenNested service when methods that
87  * perform db changes are invoked (reserveRooms(), unreserveRooms()).
88  * @author fran
89  * Date: Feb 16, 2004
90  * org.objectweb.jass.examples.travelagency.ejbsHotelBean.java
91  */

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

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

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

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

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

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