001: package org.apache.turbine.services.jsonrpc;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.CharArrayWriter;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpSession;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.turbine.services.TurbineBaseService;
030:
031: import com.metaparadigm.jsonrpc.JSONRPCBridge;
032:
033: /**
034: * This is a service that will respond to JSON-RPC calls.
035: *
036: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
037: * @version $Id$
038: */
039: public class TurbineJsonRpcService extends TurbineBaseService implements
040: JsonRpcService {
041: /** Log. */
042: private static Log log = LogFactory
043: .getLog(TurbineJsonRpcService.class);
044:
045: /** The key used to store the bridge in the session. */
046: public static final String JSON_BRIDGE_KEY = "JSONRPCBridge";
047: /**
048: * The debug option for the bridge can be enabled by enabling debug level
049: * logging for this class.
050: */
051: private static final boolean DEBUG = log.isDebugEnabled();
052:
053: public Object processCall(CharArrayWriter cdata,
054: JSONRPCBridge json_bridge, HttpServletRequest request) {
055: return JSONProcessor.processCall(cdata, json_bridge, request);
056: }
057:
058: public void registerObjectGlobal(String key, Object value) {
059: JSONRPCBridge.getGlobalBridge().setDebug(DEBUG);
060: JSONRPCBridge.getGlobalBridge().registerObject(key, value);
061: }
062:
063: public void registerObject(HttpSession session, String key,
064: Object value) {
065: JSONRPCBridge json_bridge = getBridge(session);
066: json_bridge.setDebug(DEBUG);
067: json_bridge.registerObject(key, value);
068: }
069:
070: public JSONRPCBridge getBridge(HttpSession session) {
071: JSONRPCBridge json_bridge = (JSONRPCBridge) session
072: .getAttribute(JSON_BRIDGE_KEY);
073: if (json_bridge == null) {
074: json_bridge = new JSONRPCBridge();
075: session.setAttribute(JSON_BRIDGE_KEY, json_bridge);
076: }
077: return json_bridge;
078: }
079:
080: public void clearBridge(HttpSession session) {
081: session.removeAttribute(JSON_BRIDGE_KEY);
082: }
083:
084: // The following is modeled on XmlRpcSercice.
085: // /**
086: // * Initialize the JsonRpcService.
087: // *
088: // * @throws InitializationException Something went wrong in the init stage.
089: // */
090: // public void init() throws InitializationException
091: // {
092: // //Configuration conf = getConfiguration();
093: // setInit(true);
094: // }
095: //
096: // /**
097: // * Shuts down this service, stopping running threads.
098: // */
099: // public void shutdown()
100: // {
101: // setInit(false);
102: // }
103:
104: }
|