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 javax.transaction.UserTransaction ; 18 19 25 26 public class MDB_RegisterUser implements MessageDrivenBean , MessageListener 27 { 28 30 private DataSource dataSource; 31 private MessageDrivenContext messageDrivenContext; 32 private TopicConnectionFactory connectionFactory; 33 private TopicConnection connection; 34 private Topic topic; 35 private TopicSession session; 36 private TopicPublisher replier; 37 private Context initialContext = null; 38 39 40 public MDB_RegisterUser() 41 { 42 43 } 44 45 public void onMessage(Message message) 46 { 47 try 48 { 49 MapMessage request = (MapMessage)message; 50 String correlationID = request.getJMSCorrelationID(); 51 String firstname = request.getString("firstname"); 52 String lastname = request.getString("lastname"); 53 String nickname = request.getString("nickname"); 54 String email = request.getString("email"); 55 String password = request.getString("password"); 56 String region = request.getString("region"); 57 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 59 60 String html = createUser(firstname, lastname, nickname, email, password, region); 62 63 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 65 if (temporaryTopic != null) 66 { 67 connection = connectionFactory.createTopicConnection(); 69 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 71 TextMessage reply = session.createTextMessage(); 72 reply.setJMSCorrelationID(correlationID); 73 reply.setText(html); 74 replier = session.createPublisher(null); connection.start(); 76 replier.publish(temporaryTopic, reply); 77 replier.close(); 78 session.close(); 79 connection.stop(); 80 connection.close(); 81 } 82 } 83 catch (Exception e) 84 { 85 throw new EJBException ("Message traitment failed for MDB_RegisterUser: " +e); 86 } 87 } 88 89 101 public String createUser(String firstname, String lastname, String nickname, String email, String password, String regionName) throws RemoteException 102 { 103 StringBuffer html = new StringBuffer (); 104 Connection conn = null; 105 PreparedStatement stmt = null; 106 ResultSet rs = null; 107 int regionId = -1; 108 int userId = -1; 109 int rating = 0; 110 float balance = 0; 111 String creationDate = null; 112 113 try 115 { 116 conn = dataSource.getConnection(); 118 stmt = conn.prepareStatement("SELECT id FROM regions WHERE name=?"); 119 stmt.setString(1, regionName); 120 rs = stmt.executeQuery(); 121 if (rs.first()) 122 { 123 regionId = rs.getInt("id"); 124 } 125 stmt.close(); 126 } 127 catch (Exception e) 128 { 129 try { stmt.close(); } catch (Exception ignore) {} 130 try { conn.close(); } catch (Exception ignore) {} 131 throw new RemoteException (" Region "+regionName+" does not exist in the database!<br>(got exception: " +e+")<br>\n"); 132 } 133 try 135 { 136 stmt = conn.prepareStatement("SELECT nickname FROM users WHERE nickname=?"); 137 stmt.setString(1, nickname); 138 rs = stmt.executeQuery(); 139 stmt.close(); 140 conn.close(); 141 if (rs.first()) 142 { 143 html.append("The nickname you have choosen is already taken by someone else. Please choose a new nickname.<br>"); 144 return html.toString(); 145 } 146 } 147 catch (Exception fe) 148 { 149 try { stmt.close(); } catch (Exception ignore) {} 150 try { conn.close(); } catch (Exception ignore) {} 151 throw new RemoteException ("Failed to execute Query to check the nickname: " +fe); 152 } 153 try 154 { 155 try 156 { 157 creationDate = TimeManagement.currentDateToString(); 158 conn = dataSource.getConnection(); 159 stmt = conn.prepareStatement("INSERT INTO users VALUES (NULL, \""+firstname+ 160 "\", \""+lastname+"\", \""+nickname+"\", \""+ 161 password+"\", \""+email+"\", 0, 0,\""+creationDate+"\", "+ 162 regionId+")"); 163 stmt.executeUpdate(); 164 stmt.close(); 165 } 166 catch (SQLException e) 167 { 168 try { stmt.close(); } catch (Exception ignore) {} 169 try { conn.close(); } catch (Exception ignore) {} 170 throw new RemoteException ("RUBiS internal error: User registration failed (got exception: " +e+")<br>"); 171 } 172 try 173 { 174 stmt = conn.prepareStatement("SELECT id, rating, balance FROM users WHERE nickname=?"); 175 stmt.setString(1, nickname); 176 ResultSet urs = stmt.executeQuery(); 177 if (urs.first()) 178 { 179 userId = urs.getInt("id"); 180 rating = urs.getInt("rating"); 181 balance = urs.getFloat("balance"); 182 } 183 stmt.close(); 184 } 185 catch (SQLException e) 186 { 187 try { stmt.close(); } catch (Exception ignore) {} 188 try { conn.close(); } catch (Exception ignore) {} 189 throw new RemoteException ("Failed to execute Query for user: " +e); 190 } 191 if (conn != null) conn.close(); 192 193 html.append("User id :"+userId+"<br>\n"); 194 html.append("Creation date :"+creationDate+"<br>\n"); 195 html.append("Rating :"+rating+"<br>\n"); 196 html.append("Balance :"+balance+"<br>\n"); 197 198 } 200 catch (Exception e) 201 { 202 try { stmt.close(); } catch (Exception ignore) {} 203 try { conn.close(); } catch (Exception ignore) {} 204 throw new RemoteException ("User registration failed (got exception: " +e+")<br>"); 208 } 214 return html.toString(); 215 } 216 217 219 230 public void setMessageDrivenContext(MessageDrivenContext ctx) 231 { 232 messageDrivenContext = ctx; 233 if (dataSource == null) 234 { 235 try 237 { 238 initialContext = new InitialContext (); 239 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 240 } 241 catch (Exception e) 242 { 243 throw new EJBException ("Cannot get JNDI InitialContext"); 244 } 245 } 246 } 247 248 252 public void ejbCreate() 253 { 254 255 } 256 257 266 public void ejbRemove() {} 267 268 } 269 | Popular Tags |