KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 public class SB_AuthBean implements SessionBean JavaDoc
25 {
26   protected SessionContext JavaDoc sessionContext;
27   protected Context JavaDoc initialContext = null;
28   protected DataSource JavaDoc dataSource = null;
29
30   /**
31    * Describe <code>authenticate</code> method here.
32    *
33    * @param name user nick name
34    * @param password user password
35    * @return an <code>int</code> value corresponding to the user id or -1 on error
36    */

37   public int authenticate (String JavaDoc name, String JavaDoc password) throws RemoteException JavaDoc
38   {
39     int userId = -1;
40
41     // Connecting to user Home interface thru JNDI
42
UserHome userHome = null;
43     try
44     {
45       userHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), UserHome.class);
46     }
47     catch (Exception JavaDoc e)
48     {
49       throw new RemoteException JavaDoc("Cannot lookup User: " +e);
50     }
51     // get the User ID
52
try
53     {
54       User user = userHome.findByNickName(name);
55       String JavaDoc pwd = user.getPassword();
56       if (pwd.compareTo(password) == 0)
57       {
58         userId = user.getId().intValue();
59       }
60     }
61     catch (Exception JavaDoc e)
62     {
63       throw new RemoteException JavaDoc(" User "+name+" does not exist in the database!<br>(got exception: " +e);
64     }
65     return userId;
66   }
67
68
69   // ======================== EJB related methods ============================
70

71   /**
72    * This method is empty for a stateless session bean
73    */

74   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
75   {
76   }
77
78   /** This method is empty for a stateless session bean */
79   public void ejbActivate() throws RemoteException JavaDoc {}
80   /** This method is empty for a stateless session bean */
81   public void ejbPassivate() throws RemoteException JavaDoc {}
82   /** This method is empty for a stateless session bean */
83   public void ejbRemove() throws RemoteException JavaDoc {}
84
85
86   /**
87    * Sets the associated session context. The container calls this method
88    * after the instance creation. This method is called with no transaction context.
89    * We also retrieve the Home interfaces of all RUBiS's beans.
90    *
91    * @param sessionContext - A SessionContext interface for the instance.
92    * @exception RemoteException - Thrown if the instance could not perform the function
93    * requested by the container because of a system-level error.
94    */

95   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
96   {
97     this.sessionContext = sessionContext;
98     if (dataSource == null)
99     {
100       // Finds DataSource from JNDI
101

102       try
103       {
104         initialContext = new InitialContext JavaDoc();
105         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
106       }
107       catch (Exception JavaDoc e)
108       {
109         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
110       }
111     }
112   }
113
114 }
115
Popular Tags