📄 usermanagerbean.java
字号:
package examples;import javax.ejb.*;import java.rmi.RemoteException;import java.util.*;import javax.naming.*;import javax.rmi.PortableRemoteObject;/** * UserManager is Stateless session bean resposible for creating and retrieving a customer record. * It is also authenticate the user. */public class UserManagerBean implements SessionBean { /* * Although this is a stateless session bean, we * do have state - the session context. Remember * that stateless session beans can store state, they * just can't store state on behalf of particular * clients. */ private SessionContext ctx; //---------------------------------------------------- // Begin EJB-required methods. The methods below are // called by the Container, and never called by client // code. //---------------------------------------------------- /** * Returns an user object for the given customer id. * It uses customer entity bean to retrieve the user record. */ public User getUser(String customerId) { try{ Context ctx = new InitialContext(); CustomerHome customerHome = (CustomerHome) PortableRemoteObject.narrow( ctx.lookup("CustomerHome"), CustomerHome.class); Customer customer=customerHome.findByPrimaryKey(customerId); return new User(customer.getCustomerID(),customer.getName(),customer.getPassword(),customer.getAddress()); }catch (Exception ex) { throw new EJBException(ex); } } /** * It uses the customer entity bean to create a record in the databse */ public void createUser(User user){ try{ Context ctx = new InitialContext(); CustomerHome customerHome = (CustomerHome) PortableRemoteObject.narrow( ctx.lookup("CustomerHome"), CustomerHome.class); Customer customer=customerHome.create(user.getCustomerID(),user.getName(),user.getPassword(),user.getAddress()); }catch (Exception e) { throw new EJBException(e); } } /** * This method Authenticate the user, if password is incorrect throws an InvalidPasswordException. * If password is correct, returns true. */ public boolean validateUser(String login, String password)throws InvalidPasswordException { try{ User user=getUser(login); if(password.equals(user.getPassword())){ return true; }else { throw new InvalidPasswordException("Invalid Password:"+password); } }catch(Exception ex){ throw new EJBException(ex); } } //---------------------------------------------------- // Begin EJB-required methods. The methods below are // called by the Container, and never called by client // code. //---------------------------------------------------- public void ejbCreate() throws RemoteException { System.out.println("ejbCreate() called."); } public void ejbRemove() { System.out.println("ejbRemove() called."); } public void ejbActivate() { System.out.println("ejbActivate() called."); } public void ejbPassivate() { System.out.println("ejbPassivate() called."); } public void setSessionContext(SessionContext ctx) { System.out.println("setSessionContext() called"); this.ctx = ctx; } //---------------------------------------------------- // End EJB-required methods //----------------------------------------------------}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -