Thursday, December 25, 2008

Guicing JSF with Guice

Recently I read some of presentation of the WebBeans. This encourage me to learn JSF. When learning the JSF there are Managed Bean and I think I need some injection, Guice cross on my mind, so I search on the internet about integrating Guice and  JSF. Some of article and one project found, but none of fits on what I need and I want.

So I'm trying to learn how this JSF works so I can use Guice on it.

The case of login on a web page. User need to login to a web page using username and password. Below is the login bean

public class LoginBean {
    private String username;
    private String password;
    
    public String login() {
        return "success";
    }
    
    public void reset(ActionEvent event) {
        username = null;
        password = null;
    }
}

we need some logic when login button execute. So I add some logic.

public class LoginBean {
    private String username;
    private String password;
    
    @Inject
    private UserDao userDao;
    
    public String login() {
        User user = userDao.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            // we found the user on our d   atabase
            return "success";
        } else {
            // there is no user with specified username and password
            return "failed";
        }
    }
    
    public void reset(ActionEvent event) {
        username = null;
        password = null;
    }
}

The @Inject marked will be injected by the Guice injector. So we are going to create the Guice injection works. Mojarra, the JSF Resource Implementation, we can find com.sun.faces.spi.InjectionProvider, here are the injection will be held.

public class InjectionProvider {
    void inject(Object managedBean) throws InjectionProviderException;
    void invokePostConstruct(Object managedBean) throws InjectionProviderException;
    void invokePreDestroy(Object managedBean) throws InjectionProviderException;
}

We are iterested only on "inject" method, this are executed everytime the Managed Beans are about to use.

public class GuiceInjectionProvider implements InjectionProvider {
    private final Injector injector;
    
    public GuiceInjectionProvider() {
        // create injector
        injector = Guice.createInjector(new InMemoryModule());
    }
    
    public void inject(Object managedBean) throws InjectionProviderException {
        // inject managed beans
        injector.injectMembers(managedBean);
    }
    
    public void invokePostConstruct(Object managedBean)
            throws InjectionProviderException {
    }
    
    public void invokePreDestroy(Object managedBean) throws InjectionProviderException {
    }
}

Above are simple implementation for the injection, actually for now on we need the simple one. The creation of injection is on the constructor.

We already have the java class, now is the configuration part. Add context-param on web.xml




<context-param>
lt;param-name>com.sun.faces.injectionProvider</param-name>
<param-value>uudashr.guicejsf.guice.GuiceInjectionProvider</param-value>
</context-param>



It tells the mojarra to use custom injection provider.

No comments: