1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.SessionBean ; 5 import javax.ejb.SessionContext ; 6 import javax.ejb.FinderException ; 7 import javax.ejb.ObjectNotFoundException ; 8 import javax.ejb.CreateException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import javax.rmi.PortableRemoteObject ; 14 import javax.sql.DataSource ; 15 import java.io.Serializable ; 16 17 23 24 public class SB_AuthBean implements SessionBean 25 { 26 protected SessionContext sessionContext; 27 protected Context initialContext = null; 28 protected DataSource dataSource = null; 29 30 37 public int authenticate (String name, String password) throws RemoteException 38 { 39 int userId = -1; 40 41 UserHome userHome = null; 43 try 44 { 45 userHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), UserHome.class); 46 } 47 catch (Exception e) 48 { 49 throw new RemoteException ("Cannot lookup User: " +e); 50 } 51 try 53 { 54 User user = userHome.findByNickName(name); 55 String pwd = user.getPassword(); 56 if (pwd.compareTo(password) == 0) 57 { 58 userId = user.getId().intValue(); 59 } 60 } 61 catch (Exception e) 62 { 63 throw new RemoteException (" User "+name+" does not exist in the database!<br>(got exception: " +e); 64 } 65 return userId; 66 } 67 68 69 71 74 public void ejbCreate() throws CreateException , RemoteException 75 { 76 } 77 78 79 public void ejbActivate() throws RemoteException {} 80 81 public void ejbPassivate() throws RemoteException {} 82 83 public void ejbRemove() throws RemoteException {} 84 85 86 95 public void setSessionContext(SessionContext sessionContext) throws RemoteException 96 { 97 this.sessionContext = sessionContext; 98 if (dataSource == null) 99 { 100 102 try 103 { 104 initialContext = new InitialContext (); 105 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 106 } 107 catch (Exception e) 108 { 109 throw new RemoteException ("Cannot get JNDI InitialContext"); 110 } 111 } 112 } 113 114 } 115 | Popular Tags |