KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcspring > SessionProtocol


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tcspring;
5
6 import com.tc.aspectwerkz.joinpoint.StaticJoinPoint;
7
8 import javax.servlet.http.HttpSession JavaDoc;
9 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
10 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
11
12 /**
13  * Clustering aspects related to http session.
14  *
15  * @author Eugene Kuleshov
16  */

17 public class SessionProtocol {
18
19   private static final String JavaDoc TC_SESSION_SCOPE_CONVERSATION_ID = "_TC_SESSION_SCOPE_CONVERSATION_ID";
20
21   
22   /**
23    * Uses clustered http session to store session id, to protect from the case when session id is
24    * different between nodes.
25    *
26    * cflow(execution(* org.springframework.web.context.request.SessionScope.getConversationId()))
27    * AND withincode(* org.springframework.web.context.request.ServletRequestAttributes.getSessionId())
28    * AND call(* javax.servlet.http.HttpSession+.getId())
29    * AND target(session)
30    *
31    * @see org.springframework.web.context.request.SessionScope#getConversationId()
32    * @see org.springframework.web.context.request.ServletRequestAttributes#getSessionId()
33    */

34   public Object JavaDoc clusterSessionId(StaticJoinPoint jp, HttpSession JavaDoc session) throws Throwable JavaDoc {
35     Object JavaDoc conversationId = session.getAttribute(TC_SESSION_SCOPE_CONVERSATION_ID);
36     if (conversationId == null) {
37       conversationId = jp.proceed();
38       session.setAttribute(TC_SESSION_SCOPE_CONVERSATION_ID, conversationId);
39     }
40     
41     return conversationId;
42   }
43
44
45   private ThreadLocal JavaDoc cflowCallback = new ThreadLocal JavaDoc();
46   
47   /**
48    * @see org.springframework.web.context.request.ServletRequestAttributes.registerSessionDestructionCallback(String name, Runnable callback)
49    */

50   public Object JavaDoc captureDestructionCallback(StaticJoinPoint jp, String JavaDoc name, Runnable JavaDoc callback) throws Throwable JavaDoc {
51     if(callback instanceof ScopedBeanDestructionCallBack) {
52       try {
53         cflowCallback.set(callback);
54         return jp.proceed();
55       } finally {
56         cflowCallback.set(null);
57       }
58     }
59     return jp.proceed();
60   }
61
62   /**
63    * @see org.springframework.web.context.request.ServletRequestAttributes.registerSessionDestructionCallback(String name, Runnable callback)
64    * @see javax.servlet.http.HttpSession#setAttribute(String name, Object value)
65    */

66   public Object JavaDoc virtualizeSessionDestructionListener(StaticJoinPoint jp, String JavaDoc name, HttpSession JavaDoc session) throws Throwable JavaDoc {
67     Object JavaDoc oldAttribute = session.getAttribute(name);
68     ScopedBeanDestructionCallBack callback = (ScopedBeanDestructionCallBack) cflowCallback.get();
69     if(callback!=null) {
70       if(oldAttribute==null) {
71         session.setAttribute(name, new DestructionCallbackBindingListener(callback));
72         return null;
73         
74       } else if(oldAttribute instanceof DestructionCallbackBindingListener) {
75         ((DestructionCallbackBindingListener) oldAttribute).setScopedBeanDestructionCallBack(callback);
76         return null;
77         
78       }
79     }
80     
81     return jp.proceed();
82   }
83
84   
85   /**
86    * Adapter that implements the Servlet 2.3 HttpSessionBindingListener
87    * interface, wrapping a request destruction callback.
88    */

89   private static class DestructionCallbackBindingListener implements HttpSessionBindingListener JavaDoc {
90
91     private transient ScopedBeanDestructionCallBack destructionCallback;
92
93     public DestructionCallbackBindingListener(ScopedBeanDestructionCallBack destructionCallback) {
94       this.destructionCallback = destructionCallback;
95     }
96
97     public void setScopedBeanDestructionCallBack(ScopedBeanDestructionCallBack destructionCallback) {
98       this.destructionCallback = destructionCallback;
99     }
100
101     public ScopedBeanDestructionCallBack getScopedBeanDestructionCallBack() {
102       return destructionCallback;
103     }
104
105     public void valueBound(HttpSessionBindingEvent JavaDoc event) {
106     }
107
108     public void valueUnbound(HttpSessionBindingEvent JavaDoc event) {
109       this.destructionCallback.run();
110     }
111   }
112   
113 }
114
115
Popular Tags