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.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
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 @Stateful
52 @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
53 public class AgentReservationSessionEJB
54 implements AgentReservationSessionLocal, AgentReservationSessionRemote,
55 SessionSynchronization {
56 private static final Logger log = LoggerFactory.getLogger(AgentReservationSessionEJB.class);
57
58 @Resource
59 private SessionContext ctx;
60
61 @PersistenceContext(unitName="txagent")
62 private EntityManager em;
63
64 @EJB(name="ejb/HotelReservationSession")
65 private HotelReservationSessionRemote reservationSession;
66
67 private AgentReservationSession agentSession;
68
69
70
71
72
73
74
75
76 @PostConstruct
77 public void init() {
78 log.info("*** AgentReservationSessionEJB ***");
79
80
81 log.debug("ctx=" + ctx);
82 log.debug("em=" + em);
83
84 log.debug("reservationSession=" + reservationSession);
85
86 BookingDAO dao = new JPABookingDAO();
87 ((JPABookingDAO)dao).setEntityManager(em);
88 agentSession = new AgentSessionImpl();
89 ((AgentSessionImpl)agentSession).setBookingDAO(dao);
90 ((AgentSessionImpl)agentSession).setReservationist(reservationSession);
91 }
92
93
94
95
96
97 @PreDestroy
98 public void closing() {
99 log.info("*** AgentReservationSessionEJB closing ***");
100 }
101
102 public void createBooking() throws AgentReservationException {
103 agentSession.createBooking();
104 }
105
106 public void addReservation(Person person, Date startDate, Date endDate)
107 throws AgentReservationException {
108 agentSession.addReservation(person, startDate, endDate);
109 }
110
111 public void cancelBooking() throws AgentReservationException {
112 agentSession.cancelBooking();
113 }
114
115
116
117
118
119
120
121
122
123
124
125 @TransactionAttribute(TransactionAttributeType.REQUIRED)
126 public Booking commit() throws AgentReservationException {
127 return agentSession.commit();
128 }
129
130 @Remove(retainIfException=false)
131 public void close() {
132 log.info("*** AgentReservationSessionEJB close ***");
133 agentSession.close();
134 }
135
136
137
138
139
140 public void afterBegin() {
141 log.debug("*** Transaction Started ***");
142 }
143
144
145
146
147 public void beforeCompletion() throws EJBException, RemoteException {
148 log.debug("*** Transaction about to complete, rollback:" +
149 ctx.getRollbackOnly() + " ***");
150 }
151
152
153
154
155 public void afterCompletion(boolean status) {
156 log.debug("*** Transaction Completed:" + status + " ***");
157 }
158 }