View Javadoc

1   /***
2    * 
3    */
4   package hu.elte.tribus.sf;
5   
6   import hu.elte.tribus.interfaces.ConversationDao;
7   import hu.elte.tribus.interfaces.UserTracker;
8   import hu.elte.tribus.model.Conversation;
9   
10  import java.util.Date;
11  
12  import javax.servlet.http.HttpSessionEvent;
13  import javax.servlet.http.HttpSessionListener;
14  
15  import org.hibernate.Session;
16  import org.hibernate.SessionFactory;
17  import org.springframework.context.ApplicationContext;
18  import org.springframework.transaction.PlatformTransactionManager;
19  import org.springframework.transaction.TransactionStatus;
20  import org.springframework.transaction.support.TransactionCallback;
21  import org.springframework.transaction.support.TransactionTemplate;
22  import org.springframework.web.context.support.WebApplicationContextUtils;
23  
24  /***
25   * This is the piece of code that could kill clustering
26   * environment, but it wont. At least I hope so :)
27   * 
28   * Depends on our application context, locates the UserTracker bean
29   * and performs login/logout operations on it.
30   * 
31   * It would be nice to move the system to acegi, so all this would not be neccessary.
32   * Which is beyond scope for a 48-hours hacking contest.
33   * 
34   * @author kocka
35   */
36  public class UserSessionListener implements HttpSessionListener {
37  
38  	/* (non-Javadoc)
39  	 * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
40  	 */
41  	public void sessionCreated(HttpSessionEvent arg0) {
42  		// TODO Auto-generated method stub
43  
44  	}
45  
46  	/* (non-Javadoc)
47  	 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
48  	 */
49  	public void sessionDestroyed(final HttpSessionEvent event) {
50  		final ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getSession().getServletContext());
51  		PlatformTransactionManager transactionManager = (PlatformTransactionManager) context.getBean("txManager");
52  		new TransactionTemplate(transactionManager).execute(new TransactionCallback(){
53  
54  			public Object doInTransaction(TransactionStatus arg0) {
55  				Integer conversationId = (Integer) event.getSession().getAttribute("conversation-id");
56  				if(conversationId != null) {
57  					ConversationDao conversationDao = (ConversationDao) context.getBean("conversationDao");
58  					Conversation conversation = conversationDao.getConversationbyId(conversationId);
59  					if(conversation != null && conversation.getEnddate() != null) {
60  						conversation.setEnddate(new Date());
61  						conversationDao.saveConversation(conversation);
62  					}
63  				}
64  				if(event.getSession().getAttribute("remoteuser") != null) {
65  					ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getSession().getServletContext());
66  					UserTracker tracker = (UserTracker) applicationContext.getBean("userTracker");
67  					tracker.userLoggedOut((String)event.getSession().getAttribute("remoteuser"));
68  				}
69  				return null;
70  			}});
71  
72  	}
73  
74  }