This content has been marked as final.
Show 4 replies
-
1. Re: Abstract components, EL and auto-instantiation
pmuir Nov 12, 2006 7:21 AM (in response to jtucker)I would suggest using a manager component:
@Name("currentUser") @Scope(SESSION) @Stateful public class CurrentUserManagerBean implements CurrentUserManager { private User currentUser; @Create public void create() { // Initialise the user e.g. get username from JAAS, load user from Persistence Context currentUser = ...; } @Unwrap public User unwrap() { return currentUser; } }
Alternatively you could make the manager stateful and do all the work in @Unwrap,@Name("currentUser") @Stateless public class CurrentUserManagerBean implements CurrentUserManager { @Unwrap public User unwrap() { // Initialise the user e.g. get username from JAAS, load user from Persistence Context User currentUser = em.find(User.class, username); return currentUser; } }
or implement a @Factory manager pattern.@Name("currentUserManager") @Stateless public class CurrentUserManagerBean implements CurrentUserManager { @Factory("currentUser") public User unwrap() { // Initialise the user e.g. get username from JAAS, load user from Persistence Context User currentUser = em.find(User.class, username); return currentUser; } }
-
2. Re: Abstract components, EL and auto-instantiation
jtucker Nov 12, 2006 9:39 AM (in response to jtucker)Nice suggestion Pete. Looks like it'll work great.
Thanks. -
3. Re: Abstract components, EL and auto-instantiation
jtucker Nov 12, 2006 11:07 AM (in response to jtucker)For anyone else reading this - use the @Factory method.
@Unwrap has problems with ClassCastException's since the unwrapped object is a different class to the component. -
4. Re: Abstract components, EL and auto-instantiation
gavin.king Nov 12, 2006 3:39 PM (in response to jtucker)@Unwrap has problems with ClassCastException's since the unwrapped object is a different class to the component.
What do you mean? Pete's code was perfectly correct.