publicclass JSONContextListener implements ServletContextAttributeListener { privatefinal Log log = LogFactory.getLog(JSONContextListener.class); publicvoid attributeAdded(ServletContextAttributeEvent event) { if (event.getName().equals(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) { // register LookupHelper so we can call methods on it ApplicationContext ctx = (ApplicationContext) event.getValue(); // check for null so we don't have to initialize Spring in tests if (ctx !=null) { log.debug("Registering jsonObjects for XmlHttpRequest to GlobalBridge"); JSONRPCBridge jsonBridge = JSONRPCBridge.getGlobalBridge(); Map jsonObjects = (Map) ctx.getBean("jsonObjects"); for (Object name : jsonObjects.keySet()) { jsonBridge.registerObject(name, jsonObjects.get(name)); } } } } publicvoid attributeReplaced(ServletContextAttributeEvent event) { attributeAdded(event); } }
// Find the JSONRPCBridge for this session or create one // if it doesn't exist. Note the bridge must be named "JSONRPCBridge" // in the HttpSession for the JSONRPCServlet to find it. HttpSession session = request.getSession(); JSONRPCBridge json_bridge =null; json_bridge = (JSONRPCBridge) session.getAttribute("JSONRPCBridge"); if(json_bridge ==null) { json_bridge =new JSONRPCBridge(); session.setAttribute("JSONRPCBridge", json_bridge); }