1 4 package com.tcspring; 5 6 import com.tc.aspectwerkz.joinpoint.StaticJoinPoint; 7 8 import javax.servlet.http.HttpSession ; 9 import javax.servlet.http.HttpSessionBindingEvent ; 10 import javax.servlet.http.HttpSessionBindingListener ; 11 12 17 public class SessionProtocol { 18 19 private static final String TC_SESSION_SCOPE_CONVERSATION_ID = "_TC_SESSION_SCOPE_CONVERSATION_ID"; 20 21 22 34 public Object clusterSessionId(StaticJoinPoint jp, HttpSession session) throws Throwable { 35 Object 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 cflowCallback = new ThreadLocal (); 46 47 50 public Object captureDestructionCallback(StaticJoinPoint jp, String name, Runnable callback) throws Throwable { 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 66 public Object virtualizeSessionDestructionListener(StaticJoinPoint jp, String name, HttpSession session) throws Throwable { 67 Object 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 89 private static class DestructionCallbackBindingListener implements HttpSessionBindingListener { 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 event) { 106 } 107 108 public void valueUnbound(HttpSessionBindingEvent event) { 109 this.destructionCallback.run(); 110 } 111 } 112 113 } 114 115 | Popular Tags |