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.sql.Connection ; 13 import java.sql.PreparedStatement ; 14 import java.sql.ResultSet ; 15 import java.sql.SQLException ; 16 import java.io.Serializable ; 17 18 24 25 public class MDB_PutComment implements MessageDrivenBean , MessageListener 26 { 27 private DataSource dataSource; 28 private MessageDrivenContext messageDrivenContext; 29 private TopicConnectionFactory connectionFactory; 30 private TopicConnection connection; 31 private Topic topic; 32 private TopicSession session; 33 private TopicPublisher replier; 34 private Context initialContext = null; 35 36 37 public MDB_PutComment() 38 { 39 40 } 41 42 public void onMessage(Message message) 43 { 44 try 45 { 46 MapMessage request = (MapMessage)message; 47 String correlationID = request.getJMSCorrelationID(); 48 int itemId = request.getInt("itemId"); 49 int toId = request.getInt("toId"); 50 String username = request.getString("username"); 51 String password = request.getString("password"); 52 53 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 55 56 String html = getCommentForm(itemId, toId, username, password); 58 59 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 61 if (temporaryTopic != null) 62 { 63 connection = connectionFactory.createTopicConnection(); 65 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 67 TextMessage reply = session.createTextMessage(); 68 reply.setJMSCorrelationID(correlationID); 69 reply.setText(html); 70 replier = session.createPublisher(null); connection.start(); 72 replier.publish(temporaryTopic, reply); 73 replier.close(); 74 session.close(); 75 connection.stop(); 76 connection.close(); 77 } 78 } 79 catch (Exception e) 80 { 81 throw new EJBException ("Message traitment failed for MDB_PutComment: " +e); 82 } 83 } 84 85 86 92 public String getCommentForm(int itemId, int toId, String username, String password) throws RemoteException 93 { 94 int userId = -1; 95 StringBuffer html = new StringBuffer (); 96 PreparedStatement stmt = null; 97 ResultSet rs = null; 98 Connection conn = null; 99 100 if ((username != null && !username.equals("")) || (password != null && !password.equals(""))) 102 { 103 TopicConnection authConnection; 104 TopicSession authSession; 105 Topic authTopic; 106 try 107 { 108 authConnection = connectionFactory.createTopicConnection(); 110 authTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicAuth"); 112 authSession = authConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 115 catch (Exception e) 116 { 117 throw new EJBException ("Cannot connect to message bean MDB_Auth : " +e+"<br>"); 118 } 119 try 120 { 121 TopicRequestor requestor = new TopicRequestor(authSession, authTopic); 123 MapMessage m = authSession.createMapMessage(); 125 m.setStringProperty("nickname", username); 127 m.setStringProperty("password", password); 128 m.setJMSCorrelationID("auth"); 129 authConnection.start(); MapMessage authReply = (MapMessage)requestor.request(m); 132 authConnection.stop(); 133 userId = authReply.getInt("userId"); 135 requestor.close(); authConnection.close(); 138 } 139 catch (Exception e) 140 { 141 throw new EJBException ("user authentication failed: " +e+"<br>"); 142 } 143 if (userId == -1) 144 { 145 html.append(" You don't have an account on RUBiS!<br>You have to register first.<br>"); 146 return html.toString(); 147 } 148 } 149 String toName=null, itemName=null; 151 try 152 { 153 conn = dataSource.getConnection(); 154 stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 155 stmt.setInt(1, toId); 156 rs = stmt.executeQuery(); 157 if (rs.first()) 158 toName = rs.getString("nickname"); 159 stmt.close(); 160 } 161 catch (Exception e) 162 { 163 try 164 { 165 if (stmt != null) stmt.close(); 166 if (conn != null) conn.close(); 167 } 168 catch (Exception ignore) 169 { 170 } 171 throw new RemoteException ("Failed to execute Query for user name: " +e); 172 } 173 174 try 175 { 176 stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?"); 177 stmt.setInt(1, itemId); 178 rs = stmt.executeQuery(); 179 if (rs.first()) 180 itemName = rs.getString("name"); 181 if (stmt != null) stmt.close(); 182 if (conn != null) conn.close(); 183 } 184 catch (Exception e) 185 { 186 try 187 { 188 if (stmt != null) stmt.close(); 189 if (conn != null) conn.close(); 190 } 191 catch (Exception ignore) 192 { 193 } 194 throw new RemoteException ("Failed to execute Query for item name: " +e); 195 } 196 197 try 198 { 199 html.append("<center><h2>Give feedback about your experience with "+toName+"</h2><br>\n"); 200 html.append("<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreComment\" method=POST>\n"); 201 html.append("<input type=hidden name=to value="+toId+">\n"); 202 html.append("<input type=hidden name=from value="+userId+">\n"); 203 html.append("<input type=hidden name=itemId value="+itemId+">\n"); 204 html.append("<center><table>\n"); 205 html.append("<tr><td><b>From</b><td>"+username+"\n"); 206 html.append("<tr><td><b>To</b><td>"+toName+"\n"); 207 html.append("<tr><td><b>About item</b><td>"+itemName+"\n"); 208 } 209 catch (Exception e) 210 { 211 throw new RemoteException ("Cannot build comment form: " +e); 212 } 213 214 return html.toString(); 215 } 216 217 218 219 220 222 233 public void setMessageDrivenContext(MessageDrivenContext ctx) 234 { 235 messageDrivenContext = ctx; 236 if (dataSource == null) 237 { 238 try 240 { 241 initialContext = new InitialContext (); 242 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 243 } 244 catch (Exception e) 245 { 246 throw new EJBException ("Cannot get JNDI InitialContext"); 247 } 248 } 249 } 250 251 255 public void ejbCreate() 256 { 257 258 } 259 260 269 public void ejbRemove() {} 270 271 } 272 | Popular Tags |