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_RegisterItem implements MessageDrivenBean , MessageListener 27 { 28 private UserTransaction utx = null; 29 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_RegisterItem() 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 name = request.getString("name"); 52 String description = request.getString("description"); 53 float initialPrice = request.getFloat("initialPrice"); 54 int quantity = request.getInt("quantity"); 55 float reservePrice = request.getFloat("reservePrice"); 56 float buyNow = request.getFloat("buyNow"); 57 String startDate = request.getString("startDate"); 58 String endDate = request.getString("endDate"); 59 int userId = request.getInt("userId"); 60 int categoryId = request.getInt("categoryId"); 61 62 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 64 65 String html = createItem(name, description, initialPrice, quantity, reservePrice, buyNow, startDate, endDate, userId, categoryId); 67 68 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 70 if (temporaryTopic != null) 71 { 72 connection = connectionFactory.createTopicConnection(); 74 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 76 TextMessage reply = session.createTextMessage(); 77 reply.setJMSCorrelationID(correlationID); 78 reply.setText(html); 79 replier = session.createPublisher(null); connection.start(); 81 replier.publish(temporaryTopic, reply); 82 replier.close(); 83 session.close(); 84 connection.stop(); 85 connection.close(); 86 } 87 } 88 catch (Exception e) 89 { 90 throw new EJBException ("Message traitment failed for MDB_RegisterItem: " +e); 91 } 92 } 93 94 110 public String createItem(String name, String description, float initialPrice, int quantity, float reservePrice, float buyNow, String startDate, String endDate, int userId, int categoryId) throws RemoteException 111 { 112 String html; 113 int itemId = -1; 114 Connection conn = null; 115 PreparedStatement stmt = null; 116 117 utx = messageDrivenContext.getUserTransaction(); 118 try 120 { 121 utx.begin(); 122 try 123 { 124 conn = dataSource.getConnection(); 125 stmt = conn.prepareStatement("INSERT INTO items VALUES (NULL, \""+name+ 126 "\", \""+description+"\", \""+initialPrice+"\", \""+ 127 quantity+"\", \""+reservePrice+"\", \""+buyNow+ 128 "\", 0, 0, \""+startDate+"\", \""+endDate+"\", \""+userId+ 129 "\", "+ categoryId+")"); 130 stmt.executeUpdate(); 131 stmt.close(); 132 } 133 catch (Exception e) 134 { 135 try { stmt.close(); } catch (Exception ignore) {} 136 try { conn.close(); } catch (Exception ignore) {} 137 throw new RemoteException ("Failed to create the item: " +e); 138 } 139 try 141 { 142 stmt = conn.prepareStatement("SELECT id FROM items WHERE name=?"); 143 stmt.setString(1, name); 144 ResultSet irs = stmt.executeQuery(); 145 if (!irs.first()) 146 { 147 try { stmt.close(); } catch (Exception ignore) {} 148 try { conn.close(); } catch (Exception ignore) {} 149 throw new RemoteException ("This item does not exist in the database."); 150 } 151 itemId = irs.getInt("id"); 152 153 html = "<TR><TD>Item id<TD>"+itemId+"\n"; 154 } 155 catch (Exception e) 156 { 157 try { stmt.close(); } catch (Exception ignore) {} 158 try { conn.close(); } catch (Exception ignore) {} 159 throw new RemoteException ("Failed to retrieve the item id: " +e); 160 } 161 if (stmt != null) stmt.close(); 162 if (conn != null) conn.close(); 163 utx.commit(); 164 } 165 catch (Exception e) 166 { 167 try { stmt.close(); } catch (Exception ignore) {} 168 try { conn.close(); } catch (Exception ignore) {} 169 try 170 { 171 utx.rollback(); 172 throw new RemoteException ("Item registration failed (got exception: " +e+")<br>"); 173 } 174 catch (Exception se) 175 { 176 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 177 } 178 } 179 return html; 180 } 181 182 184 195 public void setMessageDrivenContext(MessageDrivenContext ctx) 196 { 197 messageDrivenContext = ctx; 198 if (dataSource == null) 199 { 200 try 202 { 203 initialContext = new InitialContext (); 204 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 205 } 206 catch (Exception e) 207 { 208 throw new EJBException ("Cannot get JNDI InitialContext"); 209 } 210 } 211 } 212 213 217 public void ejbCreate() 218 { 219 220 } 221 222 231 public void ejbRemove() {} 232 233 234 } 235 | Popular Tags |