1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.util.ajax;
17
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpSession;
23 import javax.servlet.http.HttpSessionAttributeListener;
24 import javax.servlet.http.HttpSessionBindingEvent;
25
26 import org.seasar.tuigwaa.system.Constants;
27
28 import com.metaparadigm.jsonrpc.JSONRPCBridge;
29
30 /***
31 * @author nishioka
32 */
33 public class JsonSessionListener implements HttpSessionAttributeListener {
34
35 private Map sharedObjectsMap = Collections.synchronizedMap(new HashMap());
36
37 public void attributeAdded(HttpSessionBindingEvent event) {
38 attributeReplaced(event);
39 }
40
41 public void attributeRemoved(HttpSessionBindingEvent event) {
42 String name = event.getName();
43 HttpSession session = event.getSession();
44
45 try{
46 Object obj = session.getAttribute(name);
47
48 if (obj instanceof Ajaxlizable && ((Ajaxlizable) obj).isSharable()) {
49 Ajaxlizable ajaxObj = (Ajaxlizable) obj;
50
51 String key = ajaxObj.getSharedKey();
52 synchronized (sharedObjectsMap) {
53 SharedObjects objs = (SharedObjects) sharedObjectsMap.get(key);
54 objs.removeObject(ajaxObj);
55 if (objs.size() == 0) {
56 sharedObjectsMap.remove(key);
57 }
58 }
59 }
60 }catch(IllegalStateException e){
61
62 }
63 }
64
65 public void attributeReplaced(HttpSessionBindingEvent event) {
66 String name = event.getName();
67 HttpSession session = event.getSession();
68 Object obj = session.getAttribute(name);
69 if (!(obj instanceof Ajaxlizable)) {
70 return;
71 }
72
73 JSONRPCBridge json_bridge = null;
74 json_bridge = (JSONRPCBridge) session
75 .getAttribute(Constants.SATTR_JSONRPCBRIDGE);
76 if (json_bridge == null) {
77 json_bridge = new JSONRPCBridge();
78 session.setAttribute(Constants.SATTR_JSONRPCBRIDGE, json_bridge);
79 }
80 json_bridge.registerObject(name, obj);
81
82 Ajaxlizable ajaxObj = (Ajaxlizable) obj;
83
84 if (ajaxObj.isSharable()) {
85 String key = ajaxObj.getSharedKey();
86 synchronized (sharedObjectsMap) {
87 SharedObjects objs = (SharedObjects) sharedObjectsMap.get(key);
88 if (objs == null) {
89 objs = new SharedObjects();
90 sharedObjectsMap.put(key, objs);
91 }
92 objs.addObject(ajaxObj);
93 }
94 }
95 }
96 }