View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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){// do-nothing
61  //			System.out.println("session already invalidated : on JsonSessionListener " + session);			
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  }