1 package ejava.examples.txagent.ejb;
2
3 import java.rmi.RemoteException;
4 import java.util.Date;
5
6 import javax.annotation.PostConstruct;
7 import javax.annotation.PreDestroy;
8 import javax.annotation.Resource;
9 import javax.ejb.EJB;
10 import javax.ejb.EJBException;
11 import javax.ejb.Remove;
12 import javax.ejb.SessionContext;
13 import javax.ejb.SessionSynchronization;
14 import javax.ejb.Stateful;
15 import javax.ejb.TransactionAttribute;
16 import javax.ejb.TransactionAttributeType;
17 import javax.persistence.EntityManager;
18 import javax.persistence.PersistenceContext;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import ejava.examples.txagent.bl.AgentReservationException;
24 import ejava.examples.txagent.bl.AgentReservationSession;
25 import ejava.examples.txagent.blimpl.AgentSessionImpl;
26 import ejava.examples.txagent.bo.Booking;
27 import ejava.examples.txagent.dao.BookingDAO;
28 import ejava.examples.txagent.jpa.JPABookingDAO;
29 import ejava.examples.txhotel.bo.Person;
30 import ejava.examples.txhotel.ejb.HotelReservationSessionRemote;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 @Stateful
54 @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
55 public class AgentReservationSessionEJB
56 implements AgentReservationSessionLocal, AgentReservationSessionRemote,
57 SessionSynchronization {
58 private static final Log log =
59 LogFactory.getLog(AgentReservationSessionEJB.class);
60
61 @Resource
62 private SessionContext ctx;
63
64 @PersistenceContext(unitName="txagent")
65 private EntityManager em;
66
67 @EJB(name="ejb/HotelReservationSession")
68 private HotelReservationSessionRemote reservationSession;
69
70 private AgentReservationSession agentSession;
71
72
73
74
75
76
77
78
79 @PostConstruct
80 public void init() {
81 log.info("*** AgentReservationSessionEJB ***");
82
83
84 log.debug("ctx=" + ctx);
85 log.debug("em=" + em);
86
87 log.debug("reservationSession=" + reservationSession);
88
89 BookingDAO dao = new JPABookingDAO();
90 ((JPABookingDAO)dao).setEntityManager(em);
91 agentSession = new AgentSessionImpl();
92 ((AgentSessionImpl)agentSession).setBookingDAO(dao);
93 ((AgentSessionImpl)agentSession).setReservationist(reservationSession);
94 }
95
96
97
98
99
100 @PreDestroy
101 public void closing() {
102 log.info("*** AgentReservationSessionEJB closing ***");
103 }
104
105 public void createBooking() throws AgentReservationException {
106 agentSession.createBooking();
107 }
108
109 public void addReservation(Person person, Date startDate, Date endDate)
110 throws AgentReservationException {
111 agentSession.addReservation(person, startDate, endDate);
112 }
113
114 public void cancelBooking() throws AgentReservationException {
115 agentSession.cancelBooking();
116 }
117
118
119
120
121
122
123
124
125
126
127
128 @TransactionAttribute(TransactionAttributeType.REQUIRED)
129 public Booking commit() throws AgentReservationException {
130 return agentSession.commit();
131 }
132
133 @Remove(retainIfException=false)
134 public void close() {
135 log.info("*** AgentReservationSessionEJB close ***");
136 agentSession.close();
137 }
138
139
140
141
142
143 public void afterBegin() {
144 log.debug("*** Transaction Started ***");
145 }
146
147
148
149
150 public void beforeCompletion() throws EJBException, RemoteException {
151 log.debug("*** Transaction about to complete, rollback:" +
152 ctx.getRollbackOnly() + " ***");
153 }
154
155
156
157
158 public void afterCompletion(boolean status) {
159 log.debug("*** Transaction Completed:" + status + " ***");
160 }
161 }