KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > session > UserServiceBean


1 package hero.session;
2
3 import hero.interfaces.BnUserLocal;
4 import hero.interfaces.BnUserLocalHome;
5 import hero.interfaces.BnUserUtil;
6 import hero.interfaces.UserRegistrationLocal;
7 import hero.interfaces.UserRegistrationLocalHome;
8 import hero.interfaces.UserRegistrationUtil;
9 import hero.util.HeroException;
10 import hero.user.UserBase;
11 import hero.user.UserBaseException;
12 import hero.user.UserBaseService;
13
14 import java.rmi.RemoteException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.ejb.CreateException JavaDoc;
21 import javax.ejb.FinderException JavaDoc;
22 import javax.ejb.SessionBean JavaDoc;
23 import javax.ejb.SessionContext JavaDoc;
24
25
26 /**
27  * @ejb:bean name="UserService"
28  * display-name="UserService Bean"
29  * type="Stateless"
30  * transaction-type="Container"
31  * jndi-name="ejb/hero/UserService"
32  * local-jndi-name="ejb/hero/UserService_L"
33  *
34  * @ejb:ejb-ref ejb-name="UserService"
35  * ref-name="myhero/UserService"
36  * @ejb:transaction-type type="Container"
37  * @ejb.permission role-name="BONITAUSER,user,SuperAdmin"
38  *
39  * @jonas.bean
40  * ejb-name="UserService"
41  * jndi-name="ejb/hero/UserService"
42  *
43  **/

44
45 public class UserServiceBean implements SessionBean JavaDoc {
46     SessionContext JavaDoc mContext;
47     
48     /**
49      * Create the User Session Bean. This method is the first one to invoke in order to
50      * creates a new user account.
51      *
52      * @throws CreateException
53      *
54      * @ejb:create-method view-type="both"
55      **/

56     public void ejbCreate() throws CreateException JavaDoc {
57     }
58     
59     /**
60      * @ejb.interface-method
61      * @ejb.transaction type="Supports"
62      * @param name
63      */

64     public BnUserLocal findUserLocal(String JavaDoc name) throws HeroException {
65         BnUserLocalHome uHome;
66         try {
67             uHome=BnUserUtil.getLocalHome();
68         } catch (javax.naming.NamingException JavaDoc ne) {
69             throw new HeroException(ne.getMessage());
70         }
71
72         try {
73             return(uHome.findByName(name));
74         } catch (FinderException JavaDoc f){
75             return(registerUser(name));
76         }
77     }
78     
79     /**
80      * @ejb.interface-method
81      * @ejb.transaction type="Supports"
82      * @param name
83      */

84     public BnUserLocal findUser(String JavaDoc name) throws HeroException {
85         BnUserLocalHome uHome;
86         try {
87             uHome=BnUserUtil.getLocalHome();
88         } catch (javax.naming.NamingException JavaDoc ne) {
89             throw new HeroException(ne.getMessage());
90         }
91         try {
92             findUserInBase(name);
93             return(uHome.findByName(name));
94         } catch (Exception JavaDoc f){
95             return(registerUser(name));
96         }
97     }
98     
99     /**
100      * @ejb.interface-method
101      * @ejb.transaction type="Supports"
102      * @param name
103      */

104     public Collection JavaDoc findUsers() throws HeroException {
105         BnUserLocalHome uHome;
106         try {
107             uHome=BnUserUtil.getLocalHome();
108         } catch (javax.naming.NamingException JavaDoc ne) {
109             throw new HeroException(ne.getMessage());
110         }
111         try {
112             return(findUsersInBase());
113         } catch (Exception JavaDoc f){
114             throw new HeroException(f.getMessage());
115         }
116     }
117     
118     /**
119      * @ejb.interface-method
120      * @ejb.transaction type="Supports"
121      * @param name
122      */

123     public Map JavaDoc getUserInfos(String JavaDoc name) throws HeroException {
124         return(findUserInBase(name));
125     }
126         
127     private Map JavaDoc findUserInBase(String JavaDoc id) throws HeroException{
128         Collection JavaDoc ucol=UserBaseService.getInstance().getUserBases();
129         Iterator JavaDoc i=ucol.iterator();
130         Map JavaDoc uinfos=null;
131         while (i.hasNext()) {
132             UserBase ub=(UserBase)i.next();
133             try {
134                 uinfos=ub.getUserInfos(id);
135                 break;
136             } catch (UserBaseException u) {
137                 throw new HeroException(u.getMessage());
138             }
139         }
140         return uinfos;
141     }
142
143     private Collection JavaDoc findUsersInBase() throws HeroException{
144         Collection JavaDoc ucol=UserBaseService.getInstance().getUserBases();
145         Iterator JavaDoc i=ucol.iterator();
146         Collection JavaDoc uinfos = new ArrayList JavaDoc();
147         while (i.hasNext()) {
148             UserBase ub=(UserBase)i.next();
149             try {
150                 uinfos=ub.getUsers();
151                 break;
152             } catch (UserBaseException u) {
153                 throw new HeroException(u.getMessage());
154             }
155         }
156         return uinfos;
157     }
158     
159     private BnUserLocal registerUser(String JavaDoc name) throws HeroException {
160         try{
161             UserRegistrationLocalHome urh=UserRegistrationUtil.getLocalHome();
162             UserRegistrationLocal ur=urh.create();
163             return(ur.userCreate(name));
164         }catch(Exception JavaDoc e){throw new HeroException(e.getMessage());}
165         
166     }
167         
168     /**
169      * Internal Enterprise Java Beans method.
170      */

171     public void setSessionContext(javax.ejb.SessionContext JavaDoc context)
172             throws RemoteException JavaDoc {
173         this.mContext = context;
174     }
175     /**
176      * Internal Enterprise Java Beans method.
177      */

178     public void unsetSessionContext() throws RemoteException JavaDoc {
179     }
180     /**
181      * Internal Enterprise Java Beans method.
182      */

183     public void ejbRemove() throws java.rmi.RemoteException JavaDoc {
184     }
185     /**
186      * Internal Enterprise Java Beans method.
187      */

188     public void ejbActivate() throws java.rmi.RemoteException JavaDoc {
189     }
190     /**
191      * Internal Enterprise Java Beans method.
192      */

193     public void ejbPassivate() throws java.rmi.RemoteException JavaDoc {
194     }
195 }
Popular Tags