1 package org.gnomekr.potron.service; 2 3 import net.sf.acegisecurity.Authentication; 4 import net.sf.acegisecurity.UserDetails; 5 import net.sf.acegisecurity.context.security.SecureContextUtils; 6 7 import org.gnomekr.potron.data.User; 8 import org.hibernate.Session; 9 import org.springframework.orm.hibernate3.SessionFactoryUtils; 10 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 11 12 /*** 13 * This class provides common methods to implement specific services. 14 * 15 * @author iolo 16 * @version @Revision@ @Date@ 17 */ 18 public abstract class BasePotronService extends HibernateDaoSupport { 19 20 /*** 21 * Get currently logined user. 22 * 23 * TODO: need more efficient way 24 * 25 * @return the User object 26 */ 27 User getContextUser() { 28 Authentication auth = SecureContextUtils.getSecureContext() 29 .getAuthentication(); 30 UserDetails principal = (UserDetails) auth.getPrincipal(); 31 Session session = SessionFactoryUtils.getSession( 32 getSessionFactory(), 33 false); 34 User user = (User) session.load(User.class, principal.getUsername()); 35 return user; 36 } 37 38 /*** 39 * Get a hibernate session. 40 * 41 * @return the hibernate Session object 42 */ 43 Session getHibernateSession() { 44 return SessionFactoryUtils.getSession(getSessionFactory(), false); 45 } 46 47 }