1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.MessageDrivenBean ; 5 import javax.ejb.MessageDrivenContext ; 6 import javax.ejb.EJBException ; 7 import javax.jms.*; 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.rmi.PortableRemoteObject ; 11 import javax.sql.DataSource ; 12 import java.io.Serializable ; 13 14 20 21 public class MDB_PutBid implements MessageDrivenBean , MessageListener 22 { 23 private DataSource dataSource; 24 private MessageDrivenContext messageDrivenContext; 25 private TopicConnectionFactory connectionFactory; 26 private TopicConnection connection; 27 private Topic topic; 28 private TopicSession session; 29 private TopicPublisher replier; 30 private Context 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 correlationID = request.getJMSCorrelationID(); 44 int itemId = request.getInt("itemId"); 45 String username = request.getString("username"); 46 String password = request.getString("password"); 47 48 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 50 51 String html = getBiddingForm(itemId, username, password); 53 54 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 56 if (temporaryTopic != null) 57 { 58 connection = connectionFactory.createTopicConnection(); 60 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); connection.start(); 67 replier.publish(temporaryTopic, reply); 68 replier.close(); 69 session.close(); 70 connection.stop(); 71 connection.close(); 72 } 73 } 74 catch (Exception e) 75 { 76 throw new EJBException ("Message traitment failed for MDB_PutBid: " +e); 77 } 78 } 79 80 86 public String getBiddingForm(int itemId, String username, String password) throws RemoteException 87 { 88 int userId = -1; 89 String html = ""; 90 91 if ((username != null && !username.equals("")) || (password != null && !password.equals(""))) 93 { 94 TopicConnection authConnection; 95 TopicSession authSession; 96 Topic authTopic; 97 try 98 { 99 authConnection = connectionFactory.createTopicConnection(); 101 authTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicAuth"); 103 authSession = authConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 106 catch (Exception e) 107 { 108 throw new EJBException ("Cannot connect to message bean MDB_Auth : " +e+"<br>"); 109 } 110 try 111 { 112 TopicRequestor requestor = new TopicRequestor(authSession, authTopic); 114 MapMessage m = authSession.createMapMessage(); 116 m.setStringProperty("nickname", username); 118 m.setStringProperty("password", password); 119 m.setJMSCorrelationID("auth"); 120 authConnection.start(); MapMessage authReply = (MapMessage)requestor.request(m); 123 authConnection.stop(); 124 userId = authReply.getInt("userId"); 126 requestor.close(); authConnection.close(); 129 } 130 catch (Exception e) 131 { 132 throw new EJBException ("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 itemConnection = connectionFactory.createTopicConnection(); 148 itemTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicViewItem"); 150 itemSession = itemConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 153 catch (Exception e) 154 { 155 throw new EJBException ("Cannot connect to message bean MDB_ViewItem : " +e+"<br>"); 156 } 157 try 158 { 159 TopicRequestor requestor = new TopicRequestor(itemSession, itemTopic); 161 MapMessage m = itemSession.createMapMessage(); 163 m.setInt("itemId", itemId); 165 m.setInt("userId", userId); 166 m.setJMSCorrelationID("viewItem"); 167 itemConnection.start(); TextMessage itemReply = (TextMessage)requestor.request(m); 170 itemConnection.stop(); 171 html = itemReply.getText(); 173 requestor.close(); itemConnection.close(); 176 } 177 catch (Exception e) 178 { 179 throw new EJBException ("Exception getting the item information: " +e+"<br>"); 180 } 181 return html; 182 } 183 184 186 197 public void setMessageDrivenContext(MessageDrivenContext ctx) 198 { 199 messageDrivenContext = ctx; 200 if (dataSource == null) 201 { 202 try 204 { 205 initialContext = new InitialContext (); 206 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 207 } 208 catch (Exception e) 209 { 210 throw new EJBException ("Cannot get JNDI InitialContext"); 211 } 212 } 213 } 214 215 219 public void ejbCreate() 220 { 221 222 } 223 224 233 public void ejbRemove() {} 234 235 236 237 } 238 | Popular Tags |