KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > MDB_PutBid


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.MessageDrivenBean JavaDoc;
5 import javax.ejb.MessageDrivenContext JavaDoc;
6 import javax.ejb.EJBException JavaDoc;
7 import javax.jms.*;
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.rmi.PortableRemoteObject JavaDoc;
11 import javax.sql.DataSource JavaDoc;
12 import java.io.Serializable JavaDoc;
13
14 /**
15  * This is a stateless session bean used to build the html form to put a bid.
16  *
17  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
18  * @version 1.1
19  */

20
21 public class MDB_PutBid implements MessageDrivenBean JavaDoc, MessageListener
22 {
23   private DataSource JavaDoc dataSource;
24   private MessageDrivenContext JavaDoc messageDrivenContext;
25   private TopicConnectionFactory connectionFactory;
26   private TopicConnection connection;
27   private Topic topic;
28   private TopicSession session;
29   private TopicPublisher replier;
30   private Context JavaDoc initialContext = null;
31
32
33   public MDB_PutBid()
34   {
35
36   }
37
38   public void onMessage(Message message)
39   {
40     try
41     {
42       MapMessage request = (MapMessage)message;
43       String JavaDoc correlationID = request.getJMSCorrelationID();
44       int itemId = request.getInt("itemId");
45       String JavaDoc username = request.getString("username");
46       String JavaDoc password = request.getString("password");
47
48         // Retrieve the connection factory
49
connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName);
50
51       // get the post comment form
52
String JavaDoc html = getBiddingForm(itemId, username, password);
53
54       // send the reply
55
TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo();
56       if (temporaryTopic != null)
57       {
58         // create a connection
59
connection = connectionFactory.createTopicConnection();
60         // create a session: no transaction, auto ack
61
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
62         TextMessage reply = session.createTextMessage();
63         reply.setJMSCorrelationID(correlationID);
64         reply.setText(html);
65         replier = session.createPublisher(null); // unidentified publisher
66
connection.start();
67         replier.publish(temporaryTopic, reply);
68         replier.close();
69         session.close();
70         connection.stop();
71         connection.close();
72       }
73     }
74     catch (Exception JavaDoc e)
75     {
76       throw new EJBException JavaDoc("Message traitment failed for MDB_PutBid: " +e);
77     }
78   }
79
80   /**
81    * Authenticate the user and get the information to build the html form.
82    *
83    * @return a string in html format
84    * @since 1.1
85    */

86   public String JavaDoc getBiddingForm(int itemId, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
87   {
88     int userId = -1;
89     String JavaDoc html = "";
90
91     // Authenticate the user who want to bid
92
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
93       {
94         TopicConnection authConnection;
95         TopicSession authSession;
96         Topic authTopic;
97         try
98         {
99           // create a connection
100
authConnection = connectionFactory.createTopicConnection();
101           // lookup the destination
102
authTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicAuth");
103           // create a session
104
authSession = authConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
105
}
106         catch (Exception JavaDoc e)
107         {
108           throw new EJBException JavaDoc("Cannot connect to message bean MDB_Auth : " +e+"<br>");
109         }
110         try
111         {
112           // create a requestor to receive the reply
113
TopicRequestor requestor = new TopicRequestor(authSession, authTopic);
114           // create a message
115
MapMessage m = authSession.createMapMessage();
116           // set parameters
117
m.setStringProperty("nickname", username);
118           m.setStringProperty("password", password);
119           m.setJMSCorrelationID("auth");
120           // send the message and receive the reply
121
authConnection.start(); // allows message to be delivered (default is connection stopped)
122
MapMessage authReply = (MapMessage)requestor.request(m);
123           authConnection.stop();
124           // read the reply
125
userId = authReply.getInt("userId");
126           // close connection and session
127
requestor.close(); // also close the session
128
authConnection.close();
129         }
130         catch (Exception JavaDoc e)
131         {
132           throw new EJBException JavaDoc("user authentication failed: " +e+"<br>");
133         }
134         if (userId == -1)
135         {
136            html = "You don't have an account on RUBiS!<br>You have to register first.<br>";
137            return html;
138         }
139       }
140
141         TopicConnection itemConnection;
142         TopicSession itemSession;
143         Topic itemTopic;
144         try
145         {
146           // create a connection
147
itemConnection = connectionFactory.createTopicConnection();
148           // lookup the destination
149
itemTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicViewItem");
150           // create a session
151
itemSession = itemConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
152
}
153         catch (Exception JavaDoc e)
154         {
155           throw new EJBException JavaDoc("Cannot connect to message bean MDB_ViewItem : " +e+"<br>");
156         }
157         try
158         {
159           // create a requestor to receive the reply
160
TopicRequestor requestor = new TopicRequestor(itemSession, itemTopic);
161           // create a message
162
MapMessage m = itemSession.createMapMessage();
163           // set parameters
164
m.setInt("itemId", itemId);
165           m.setInt("userId", userId);
166           m.setJMSCorrelationID("viewItem");
167           // send the message and receive the reply
168
itemConnection.start(); // allows message to be delivered (default is connection stopped)
169
TextMessage itemReply = (TextMessage)requestor.request(m);
170           itemConnection.stop();
171           // read the reply
172
html = itemReply.getText();
173           // close connection and session
174
requestor.close(); // also close the session
175
itemConnection.close();
176         }
177         catch (Exception JavaDoc e)
178         {
179           throw new EJBException JavaDoc("Exception getting the item information: " +e+"<br>");
180         }
181         return html;
182   }
183                    
184   // ======================== EJB related methods ============================
185

186   /**
187    * Set the associated context. The container call this method
188    * after the instance creation.
189    * The enterprise Bean instance should store the reference to the context
190    * object in an instance variable.
191    * This method is called with no transaction context.
192    *
193    * @param MessageDrivenContext A MessageDrivenContext interface for the instance.
194    * @throws EJBException Thrown by the method to indicate a failure caused by
195    * a system-level error.
196    */

197   public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
198   {
199     messageDrivenContext = ctx;
200     if (dataSource == null)
201     {
202       // Finds DataSource from JNDI
203
try
204       {
205         initialContext = new InitialContext JavaDoc();
206         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
207       }
208       catch (Exception JavaDoc e)
209       {
210         throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
211       }
212     }
213   }
214
215   /**
216    * The Message driven bean must define an ejbCreate methods with no args.
217    *
218    */

219   public void ejbCreate()
220   {
221
222   }
223  
224   /**
225    * A container invokes this method before it ends the life of the message-driven object.
226    * This happens when a container decides to terminate the message-driven object.
227    *
228    * This method is called with no transaction context.
229    *
230    * @throws EJBException Thrown by the method to indicate a failure caused by
231    * a system-level error.
232    */

233   public void ejbRemove() {}
234
235
236
237 }
238
Popular Tags