1 package ejava.examples.txagent.ejb;
2
3 import java.util.List;
4
5 import javax.annotation.PostConstruct;
6 import javax.annotation.PreDestroy;
7 import javax.annotation.Resource;
8 import javax.ejb.EJB;
9 import javax.ejb.SessionContext;
10 import javax.ejb.Stateless;
11 import javax.ejb.TransactionAttribute;
12 import javax.ejb.TransactionAttributeType;
13 import javax.persistence.EntityManager;
14 import javax.persistence.PersistenceContext;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import ejava.examples.txagent.bl.AgentReservationException;
20 import ejava.examples.txagent.bl.BookingAgent;
21 import ejava.examples.txagent.blimpl.AgentImpl;
22 import ejava.examples.txagent.bo.Booking;
23 import ejava.examples.txagent.dao.BookingDAO;
24 import ejava.examples.txagent.jpa.JPABookingDAO;
25 import ejava.examples.txhotel.ejb.HotelRegistrationRemote;
26
27
28
29
30
31
32
33
34
35 @Stateless
36 @TransactionAttribute(TransactionAttributeType.REQUIRED)
37 public class BookingAgentEJB implements BookingAgentRemote,
38 BookingAgentLocal {
39 private Log log = LogFactory.getLog(BookingAgentEJB.class);
40
41 @EJB(name="ejb/HotelReservation")
42
43 private HotelRegistrationRemote hotel;
44
45 @PersistenceContext(unitName="txagent")
46 private EntityManager em;
47
48 @Resource
49 private SessionContext ctx;
50
51
52 private BookingAgent agent;
53
54
55
56
57
58
59 @PostConstruct
60 public void init() {
61 log.info("*** BookingAgentEJB initializing ***");
62
63
64 log.debug("em=" + em);
65 log.debug("ctx=" + ctx);
66 log.debug("hotel=" + hotel);
67
68
69 if (hotel == null) {
70 StringBuilder text = new StringBuilder();
71 try {
72 Object object = ctx.lookup("ejb/HotelReservation");
73 text.append("ctx.lookup(ejb/HotelReservation)=" + object);
74 text.append(", class=" + object.getClass().getName());
75 @SuppressWarnings("rawtypes")
76 Class clazz = object.getClass();
77 for (Object iface : clazz.getInterfaces()) {
78 text.append(", iface=" + iface);
79 }
80 hotel = (HotelRegistrationRemote) object;
81 }
82 catch (Exception ex) {
83 text.append(", exception=" + ex);
84 try {
85 Object object = new javax.naming.InitialContext().lookup(
86 "ejava/examples/txhotel/HotelRegistrationEJB/remote");
87 text.append(", jndi.lookup(HotelRegistrationEJB)=" + object);
88 text.append(", class=" + object.getClass().getName());
89 @SuppressWarnings("rawtypes")
90 Class clazz = object.getClass();
91 for (Object iface : clazz.getInterfaces()) {
92 text.append(", iface=" + iface);
93 }
94 hotel = (HotelRegistrationRemote) object;
95 }
96 catch (Exception ex2) {
97 text.append(", exception=" + ex2);
98 }
99 }
100 finally { log.debug(text); }
101 }
102
103
104 BookingDAO dao = new JPABookingDAO();
105 ((JPABookingDAO)dao).setEntityManager(em);
106 agent = new AgentImpl();
107 ((AgentImpl)agent).setReservationist(hotel);
108 ((AgentImpl)agent).setBookingDAO(dao);
109 }
110
111
112
113
114
115
116 @PreDestroy
117 public void close() {
118 log.info("*** BookingAgentEJB closing ***");
119 agent=null;
120 hotel=null;
121 }
122
123 public Booking getBookingByConfirmation(String confirmation)
124 throws AgentReservationException {
125 return agent.getBookingByConfirmation(confirmation);
126 }
127
128 public List<Booking> getBookings(int index, int count)
129 throws AgentReservationException {
130 return agent.getBookings(index, count);
131 }
132
133 public void cleanupBooking(String confirmation)
134 throws AgentReservationException {
135 agent.cleanupBooking(confirmation);
136 }
137 }