KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > MDB_Auth


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.MessageDrivenBean JavaDoc;
5 import javax.ejb.MessageDrivenContext JavaDoc;
6 import javax.ejb.EJBException JavaDoc;
7 import javax.jms.*;
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.rmi.PortableRemoteObject JavaDoc;
11 import javax.sql.DataSource JavaDoc;
12 import java.sql.Connection JavaDoc;
13 import java.sql.PreparedStatement JavaDoc;
14 import java.sql.ResultSet JavaDoc;
15 import java.sql.SQLException JavaDoc;
16 import java.io.Serializable JavaDoc;
17
18 /**
19  * This is a message driven bean used to provides user authentication
20  * services to servlets.
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.1
23  */

24
25 public class MDB_Auth implements MessageDrivenBean JavaDoc, MessageListener
26 {
27
28   private DataSource JavaDoc dataSource;
29   private MessageDrivenContext JavaDoc messageDrivenContext;
30   private TopicConnectionFactory connectionFactory;
31   private TopicConnection connection;
32   private Topic topic;
33   private TopicSession session;
34   private TopicPublisher replier;
35   private Context JavaDoc 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 JavaDoc correlationID = request.getJMSCorrelationID();
49       String JavaDoc nickname = request.getStringProperty("nickname");
50       String JavaDoc password = request.getStringProperty("password");
51
52       // check username and password
53
int userId = authenticate(nickname, password);
54
55       // send the reply
56
TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo();
57       if (temporaryTopic != null)
58       {
59         // Retrieve the connection factory
60
connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName);
61         // create a connection
62
connection = connectionFactory.createTopicConnection();
63         // create a session: no transaction, auto ack
64
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); // unidentified publisher
69
connection.start();
70         replier.publish(temporaryTopic, reply);
71         replier.close();
72         session.close();
73         connection.stop();
74         connection.close();
75       }
76     }
77     catch (Exception JavaDoc e)
78     {
79       throw new EJBException JavaDoc("Message traitment failed for MDB_Auth: " +e);
80     }
81   }
82
83   /**
84    * Describe <code>authenticate</code> method here.
85    *
86    * @param name user nick name
87    * @param password user password
88    * @return an <code>int</code> value corresponding to the user id or -1 on error
89    */

90   public int authenticate (String JavaDoc name, String JavaDoc password) throws RemoteException JavaDoc
91   {
92     int userId = -1;
93     ResultSet JavaDoc rs = null;
94     PreparedStatement JavaDoc stmt = null;
95     Connection JavaDoc 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 JavaDoc e)
112     {
113       try
114       {
115         if (stmt != null) stmt.close();
116         if (conn != null) conn.close();
117       }
118       catch (Exception JavaDoc ignore)
119       {
120       }
121       throw new EJBException JavaDoc(" User "+name+" does not exist in the database!<br>(got exception: " +e);
122     }
123     return userId;
124   }
125
126
127   // ======================== EJB related methods ============================
128

129   /**
130    * Set the associated context. The container call this method
131    * after the instance creation.
132    * The enterprise Bean instance should store the reference to the context
133    * object in an instance variable.
134    * This method is called with no transaction context.
135    *
136    * @param MessageDrivenContext A MessageDrivenContext interface for the instance.
137    * @throws EJBException Thrown by the method to indicate a failure caused by
138    * a system-level error.
139    */

140   public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
141   {
142     messageDrivenContext = ctx;
143     if (dataSource == null)
144     {
145       // Finds DataSource from JNDI
146
try
147       {
148         initialContext = new InitialContext JavaDoc();
149         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
150       }
151       catch (Exception JavaDoc e)
152       {
153         throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
154       }
155     }
156   }
157
158   /**
159    * The Message driven bean must define an ejbCreate methods with no args.
160    *
161    */

162   public void ejbCreate()
163   {
164
165   }
166  
167   /**
168    * A container invokes this method before it ends the life of the message-driven object.
169    * This happens when a container decides to terminate the message-driven object.
170    *
171    * This method is called with no transaction context.
172    *
173    * @throws EJBException Thrown by the method to indicate a failure caused by
174    * a system-level error.
175    */

176   public void ejbRemove() {}
177
178 }
179
Popular Tags