SignOnEJB.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package day21ex.signon;
- import java.util.*;
- import javax.ejb.*;
- import javax.naming.*;
- import day21ex.user.*;
- import day21ex.student.*;
- public class SignOnEJB implements SessionBean {
- private SessionContext ctx;
- private HashSet cart;
- InitialContext initCtx = null;
- public SignOnEJB() {
- print("The container created this instance.n");
- }
- public void setSessionContext(SessionContext ctx) {
- print("The container called the setSessionContext method ");
- print("to associate session bean instance with its context.n");
- this.ctx = ctx;
- }
- public void ejbCreate() throws CreateException {
- print("The container called the ejbCreate method.n");
- cart = new HashSet();
- }
- public void ejbActivate() {
- print("This instance has just been reactivated.n");
- }
- public void ejbPassivate() {
- print("The container intends to passivate the instance.n");
- }
- public void ejbRemove() {
- print("This instance is in the process of being removed ");
- print("by the container.n");
- }
- public void addUser(String userName, String password){
- try {
- InitialContext ic = new InitialContext();
- UserLocalHome ulh = (UserLocalHome) ic.lookup("day21ex/User");
- UserLocal user = ulh.create(userName, password);
- } catch (Exception e) {
- throw new EJBException("Exception while adding user: "
- + e.getMessage());
- }
- }
- public boolean validateUser(String userName, String password)
- throws InvalidLoginException {
- try {
- InitialContext ic = new InitialContext();
- UserLocalHome ulh = (UserLocalHome) ic.lookup("day21ex/User");
- UserLocal user = ulh.findByPrimaryKey(userName);
- if (user == null) {
- throw new InvalidLoginException("Invalid login");
- }
- return user.matchPassword(password);
- } catch (FinderException fe) {
- throw new InvalidLoginException("Invalid login");
- } catch (NamingException ne) {
- throw new EJBException("Naming exception:" + ne.getMessage());
- }
- }
- void print(String s) {
- System.out.println("SignOnEJB:" + s);
- }
- }