shorne in japan

blog archive about resume
package net.shornepla.auth;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
 * Provides an UserDesailsService implementation based on Hibernate.
 * This ties hibernate and acegi together.
 *
 * @author shorne
 * @since Feb 13, 2009
 */
public class UserDetailProvider extends HibernateDaoSupport implements UserDetailsService {
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DataAccessException {
        Object object = null;
        Session session = this.getSession();
        session.beginTransaction();
        object = session.createQuery("from User o where o.username=:username")
                 .setString("username", username).uniqueResult();
        session.getTransaction().commit();
        if (object == null) {
            throw new UsernameNotFoundException("User " + username + "was not found");
        } else {
            return (UserDetails) object;
        }
    }
}