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 import java.net.URLEncoder ; 18 19 25 26 public class MDB_BrowseCategories implements MessageDrivenBean , MessageListener 27 { 28 private DataSource dataSource; 29 private MessageDrivenContext messageDrivenContext; 30 private TopicConnectionFactory connectionFactory; 31 private TopicConnection connection; 32 private Topic topic; 33 private TopicSession session; 34 private TopicPublisher replier; 35 private Context initialContext = null; 36 37 38 public MDB_BrowseCategories() 39 { 40 41 } 42 43 public void onMessage(Message message) 44 { 45 try 46 { 47 MapMessage request = (MapMessage)message; 48 String correlationID = request.getJMSCorrelationID(); 49 String region = request.getString("region"); 50 String nickname = request.getString("nickname"); 51 String password = request.getString("password"); 52 53 54 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 56 String html = getCategories(region, nickname, password); 58 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 60 if (temporaryTopic != null) 61 { 62 connection = connectionFactory.createTopicConnection(); 64 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 66 TextMessage reply = session.createTextMessage(); 67 reply.setJMSCorrelationID(correlationID); 68 reply.setText(html); 69 replier = session.createPublisher(null); connection.start(); 71 replier.publish(temporaryTopic, reply); 72 replier.close(); 73 session.close(); 74 connection.stop(); 75 connection.close(); 76 } 77 } 78 catch (Exception e) 79 { 80 throw new EJBException ("Message traitment failed for MDB_BrowseCategories: " +e); 81 } 82 } 83 84 85 91 92 public String getCategories(String regionName, String username, String password) throws RemoteException 93 { 94 StringBuffer html = new StringBuffer (); 95 Connection conn = null; 96 PreparedStatement stmt = null; 97 ResultSet rs = null; 98 String categoryName; 99 int categoryId; 100 int regionId = -1; 101 int userId = -1; 102 103 if (regionName != null && !regionName.equals("")) 104 { 105 try 107 { 108 conn = dataSource.getConnection(); 109 stmt = conn.prepareStatement("SELECT id FROM regions WHERE name=?"); 110 stmt.setString(1, regionName); 111 rs = stmt.executeQuery(); 112 stmt.close(); 113 } 114 catch (SQLException e) 115 { 116 try 117 { 118 if (stmt != null) stmt.close(); 119 if (conn != null) conn.close(); 120 } 121 catch (Exception ignore) 122 { 123 } 124 throw new RemoteException ("Failed to get region Id " +e); 125 } 126 try 127 { 128 if (rs.first()) 129 { 130 regionId = rs.getInt("id"); 131 } 132 } 133 catch (Exception e) 134 { 135 try 136 { 137 if (conn != null) conn.close(); 138 } 139 catch (Exception ignore) 140 { 141 } 142 throw new EJBException (" Region "+regionName+" does not exist in the database!<br>(got exception: " +e+")"); 143 } 144 } 145 else 146 { 147 if ((username != null && !username.equals("")) || (password != null && !password.equals(""))) 149 { 150 TopicConnection authConnection; 151 TopicSession authSession; 152 Topic authTopic; 153 try 154 { 155 authConnection = connectionFactory.createTopicConnection(); 157 authTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicAuth"); 159 authSession = authConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 162 catch (Exception e) 163 { 164 throw new EJBException ("Cannot connect to message bean MDB_Auth : " +e+"<br>"); 165 } 166 try 167 { 168 TopicRequestor requestor = new TopicRequestor(authSession, authTopic); 170 MapMessage m = authSession.createMapMessage(); 172 m.setStringProperty("nickname", username); 174 m.setStringProperty("password", password); 175 m.setJMSCorrelationID("auth"); 176 authConnection.start(); MapMessage authReply = (MapMessage)requestor.request(m); 179 authConnection.stop(); 180 userId = authReply.getInt("userId"); 182 requestor.close(); authConnection.close(); 185 } 186 catch (Exception e) 187 { 188 throw new EJBException ("user authentication failed: " +e+"<br>"); 189 } 190 if (userId == -1) 191 { 192 html.append(" You don't have an account on RUBiS!<br>You have to register first.<br>"); 193 return html.toString(); 194 } 195 } 196 } 197 try 198 { 199 if (conn == null) 200 conn = dataSource.getConnection(); 201 stmt = conn.prepareStatement("SELECT name, id FROM categories"); 202 rs = stmt.executeQuery(); 203 } 204 catch (SQLException e) 205 { 206 try 207 { 208 if (stmt != null) stmt.close(); 209 if (conn != null) conn.close(); 210 } 211 catch (Exception ignore) 212 { 213 } 214 throw new EJBException ("Failed to get categories list " +e); 215 } 216 try 217 { 218 if (!rs.first()) 219 html.append("<h2>Sorry, but there is no category available at this time. Database table is empty</h2><br>"); 220 else 221 { 222 do 223 { 224 categoryName = rs.getString("name"); 225 categoryId = rs.getInt("id"); 226 if (regionId != -1) 227 { 228 html.append(printCategoryByRegion(categoryName, categoryId, regionId)); 229 } 230 else 231 { 232 if (userId != -1) 233 html.append(printCategoryToSellItem(categoryName, categoryId, userId)); 234 else 235 html.append(printCategory(categoryName, categoryId)); 236 } 237 } 238 while (rs.next()); 239 } 240 if (stmt != null) stmt.close(); 241 if (conn != null) conn.close(); 242 } 243 catch (Exception e) 244 { 245 try 246 { 247 if (stmt != null) stmt.close(); 248 if (conn != null) conn.close(); 249 } 250 catch (Exception ignore) 251 { 252 } 253 throw new EJBException ("Exception getting category list: " + e); 254 } 255 return html.toString(); 256 } 257 258 264 public String printCategory(String name, int id) 265 { 266 return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByCategory?category="+id+ 267 "&categoryName="+URLEncoder.encode(name)+"\">"+name+"</a><br>\n"; 268 } 269 270 276 public String printCategoryByRegion(String name, int id, int regionId) 277 { 278 return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+id+ 279 "&categoryName="+URLEncoder.encode(name)+"®ion="+regionId+"\">"+name+"</a><br>\n"; 280 } 281 282 283 289 public String printCategoryToSellItem(String name, int id, int userId) 290 { 291 return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.SellItemForm?category="+id+"&user="+userId+"\">"+name+"</a><br>\n"; 292 } 293 294 295 297 308 public void setMessageDrivenContext(MessageDrivenContext ctx) 309 { 310 messageDrivenContext = ctx; 311 if (dataSource == null) 312 { 313 try 315 { 316 initialContext = new InitialContext (); 317 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 318 } 319 catch (Exception e) 320 { 321 throw new EJBException ("Cannot get JNDI InitialContext"); 322 } 323 } 324 } 325 326 330 public void ejbCreate() 331 { 332 333 } 334 335 344 public void ejbRemove() {} 345 346 347 348 349 } 350 | Popular Tags |