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_Auth implements MessageDrivenBean , MessageListener 26 { 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_Auth() 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 nickname = request.getStringProperty("nickname"); 50 String password = request.getStringProperty("password"); 51 52 int userId = authenticate(nickname, password); 54 55 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 57 if (temporaryTopic != null) 58 { 59 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 61 connection = connectionFactory.createTopicConnection(); 63 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 65 MapMessage reply = session.createMapMessage(); 66 reply.setJMSCorrelationID(correlationID); 67 reply.setInt("userId", userId); 68 replier = session.createPublisher(null); connection.start(); 70 replier.publish(temporaryTopic, reply); 71 replier.close(); 72 session.close(); 73 connection.stop(); 74 connection.close(); 75 } 76 } 77 catch (Exception e) 78 { 79 throw new EJBException ("Message traitment failed for MDB_Auth: " +e); 80 } 81 } 82 83 90 public int authenticate (String name, String password) throws RemoteException 91 { 92 int userId = -1; 93 ResultSet rs = null; 94 PreparedStatement stmt = null; 95 Connection conn = null; 96 97 try 98 { 99 conn = dataSource.getConnection(); 100 stmt = conn.prepareStatement("SELECT users.id FROM users WHERE nickname=? AND password=?"); 101 stmt.setString(1, name); 102 stmt.setString(2, password); 103 rs = stmt.executeQuery(); 104 if (rs.first()) 105 { 106 userId = rs.getInt("id"); 107 } 108 if (stmt != null) stmt.close(); 109 if (conn != null) conn.close(); 110 } 111 catch (SQLException e) 112 { 113 try 114 { 115 if (stmt != null) stmt.close(); 116 if (conn != null) conn.close(); 117 } 118 catch (Exception ignore) 119 { 120 } 121 throw new EJBException (" User "+name+" does not exist in the database!<br>(got exception: " +e); 122 } 123 return userId; 124 } 125 126 127 129 140 public void setMessageDrivenContext(MessageDrivenContext ctx) 141 { 142 messageDrivenContext = ctx; 143 if (dataSource == null) 144 { 145 try 147 { 148 initialContext = new InitialContext (); 149 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 150 } 151 catch (Exception e) 152 { 153 throw new EJBException ("Cannot get JNDI InitialContext"); 154 } 155 } 156 } 157 158 162 public void ejbCreate() 163 { 164 165 } 166 167 176 public void ejbRemove() {} 177 178 } 179 | Popular Tags |