01: package com.mockrunner.example.ejb;
02:
03: import java.rmi.RemoteException;
04:
05: import javax.ejb.CreateException;
06: import javax.ejb.EJBException;
07: import javax.ejb.FinderException;
08: import javax.ejb.SessionBean;
09: import javax.ejb.SessionContext;
10: import javax.naming.InitialContext;
11:
12: import com.mockrunner.example.ejb.interfaces.UserEntity;
13: import com.mockrunner.example.ejb.interfaces.UserEntityHome;
14:
15: /*
16: * @ejb:bean name="UserLoginSession"
17: * display-name="UserLoginSessionBean"
18: * type="Stateless"
19: * transaction-type="Container"
20: * jndi-name="com/mockrunner/example/UserLoginSession"
21: *
22: * @ejb:ejb-ref ejb-name="UserEntity" view-type="remote" ref-name="ejb/UserEntity"
23: **/
24: /**
25: * Facade session bean for {@link UserEntityBean}.
26: */
27: public class UserLoginSessionBean implements SessionBean {
28: private SessionContext sessionContext;
29:
30: /*
31: * @ejb:interface-method
32: * @ejb:transaction type="Required"
33: **/
34: public boolean loginUser(String username, String password) {
35: try {
36: InitialContext context = new InitialContext();
37: UserEntityHome home = (UserEntityHome) context
38: .lookup("java:comp/env/ejb/UserEntity");
39: try {
40: UserEntity user = home.findByPrimaryKey(username);
41: if (user.getPassword().equals(password)) {
42: return true;
43: }
44: return false;
45: } catch (FinderException exc) {
46: return false;
47: }
48: } catch (Exception exc) {
49: sessionContext.setRollbackOnly();
50: throw new EJBException(exc);
51: }
52: }
53:
54: /*
55: * @ejb:interface-method
56: * @ejb:transaction type="Required"
57: **/
58: public boolean createUser(String username, String password) {
59: try {
60: InitialContext context = new InitialContext();
61: UserEntityHome home = (UserEntityHome) context
62: .lookup("java:comp/env/ejb/UserEntity");
63: try {
64: UserEntity user = home.create(username, password);
65: return true;
66: } catch (CreateException exc) {
67: return false;
68: }
69: } catch (Exception exc) {
70: sessionContext.setRollbackOnly();
71: throw new EJBException(exc);
72: }
73: }
74:
75: /*
76: * @ejb:create-method
77: **/
78: public void ejbCreate() throws CreateException {
79:
80: }
81:
82: public void ejbActivate() throws EJBException, RemoteException {
83:
84: }
85:
86: public void ejbPassivate() throws EJBException, RemoteException {
87:
88: }
89:
90: public void ejbRemove() throws EJBException, RemoteException {
91:
92: }
93:
94: public void setSessionContext(SessionContext sessionContext)
95: throws EJBException, RemoteException {
96: this.sessionContext = sessionContext;
97: }
98: }
|