View Javadoc
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.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
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   * This class provides a few conveniences methods to help inspect the
29   * impact of the actions taken with the stateful AgentReservationSession bean.
30   * It is stateless and can answer questions and perform cleanup actions 
31   * associated with Bookings. 
32   *
33   */
34  @Stateless
35  @TransactionAttribute(TransactionAttributeType.REQUIRED)
36  public class BookingAgentEJB implements BookingAgentRemote,
37          BookingAgentLocal {
38      private static final Logger log = LoggerFactory.getLogger(BookingAgentEJB.class);
39      
40      @EJB(name="ejb/HotelReservation")
41      //@EJB(mappedName="ejb:txHotelEAR/txHotelEJB//HotelRegistrationEJB!ejava.examples.txhotel.ejb.HotelRegistrationRemote")
42      private HotelRegistrationRemote hotel;
43      
44      @PersistenceContext(unitName="txagent")
45      private EntityManager em;
46      
47      @Resource
48      private SessionContext ctx;
49  
50      //initialized within init() method
51      private BookingAgent agent;
52  
53  
54      /**
55       * This method creates the business logic, assigns a DAO, and registers
56       * the EntityManager for the DAO(s) to use.
57       */
58      @PostConstruct
59      public void init() {
60          log.info("*** BookingAgentEJB initializing ***");
61          
62          //injected by container
63          log.debug("em=" + em);
64          log.debug("ctx=" + ctx);
65          log.debug("hotel=" + hotel);
66          
67          //the following ugly debug was for testing with classloader issues
68          if (hotel == null) {
69  	        StringBuilder text = new StringBuilder();
70  	        try {
71  	            Object object = ctx.lookup("ejb/HotelReservation");
72  	            text.append("ctx.lookup(ejb/HotelReservation)=" + object);
73  	            text.append(", class=" + object.getClass().getName());
74  	            @SuppressWarnings("rawtypes")
75  				Class clazz = object.getClass();
76  	            for (Object iface : clazz.getInterfaces()) {
77  	            	text.append(", iface=" + iface);
78  	            }
79  	            hotel = (HotelRegistrationRemote) object;
80  	        }
81  	        catch (Exception ex) { 
82  	        	text.append(", exception=" + ex);
83  	        	try {
84  		            Object object = new javax.naming.InitialContext().lookup(
85  		            		"ejava/examples/txhotel/HotelRegistrationEJB/remote");
86  		            text.append(", jndi.lookup(HotelRegistrationEJB)=" + object);
87  		            text.append(", class=" + object.getClass().getName());
88  		            @SuppressWarnings("rawtypes")
89  					Class clazz = object.getClass();
90  		            for (Object iface : clazz.getInterfaces()) {
91  		            	text.append(", iface=" + iface);
92  		            }
93  		            hotel = (HotelRegistrationRemote) object;
94  	        	}
95  		        catch (Exception ex2) { 
96  		         	text.append(", exception=" + ex2);
97  		        }
98  	        }
99  	        finally              { log.debug("{}",text); }
100         }
101         
102         //manual construction
103         BookingDAO dao = new JPABookingDAO();
104         ((JPABookingDAO)dao).setEntityManager(em);
105         agent = new AgentImpl();
106         ((AgentImpl)agent).setReservationist(hotel);
107         ((AgentImpl)agent).setBookingDAO(dao);
108     }
109     
110     /**
111      * This method is called whenever the bean is ejected from the container;
112      * which isn't too often for stateless session beans under normal 
113      * circumstances.
114      */
115     @PreDestroy
116     public void close() {
117         log.info("*** BookingAgentEJB closing ***");
118         agent=null;
119         hotel=null;
120     }    
121 
122     public Booking getBookingByConfirmation(String confirmation) 
123         throws AgentReservationException {
124         return agent.getBookingByConfirmation(confirmation);
125     }
126 
127     public List<Booking> getBookings(int index, int count) 
128         throws AgentReservationException {
129         return agent.getBookings(index, count);
130     }
131 
132     public void cleanupBooking(String confirmation) 
133         throws AgentReservationException {
134         agent.cleanupBooking(confirmation);
135     }
136 }