    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: TurbineJsonRpcService.java 712125 2008-11-07 13:47:28Z seade $
    038     */
    039    public class TurbineJsonRpcService
    040            extends TurbineBaseService
    041            implements JsonRpcService
    042    {
    043        /** Log. */
    044        private static Log log = LogFactory.getLog(TurbineJsonRpcService.class);
    045
    046        /** The key used to store the bridge in the session. */
    047        public static final String JSON_BRIDGE_KEY = "JSONRPCBridge";
    048        /**
    049         * The debug option for the bridge can be enabled by enabling debug level
    050         * logging for this class.
    051         */
    052        private static final boolean DEBUG = log.isDebugEnabled();
    053
    054        public Object processCall(CharArrayWriter cdata,
    055                JSONRPCBridge json_bridge, HttpServletRequest request)
    056        {
    057            return JSONProcessor.processCall(cdata, json_bridge, request);
    058        }
    059
    060        public void registerObjectGlobal(String key, Object value)
    061        {
    062            JSONRPCBridge.getGlobalBridge().setDebug(DEBUG);
    063            JSONRPCBridge.getGlobalBridge().registerObject(key, value);
    064        }
    065
    066        public void registerObject(HttpSession session, String key, Object value)
    067        {
    068            JSONRPCBridge json_bridge = getBridge(session);
    069            json_bridge.setDebug(DEBUG);
    070            json_bridge.registerObject(key, value);
    071        }
    072
    073        public JSONRPCBridge getBridge(HttpSession session)
    074        {
    075            JSONRPCBridge json_bridge = (JSONRPCBridge) session.getAttribute(JSON_BRIDGE_KEY);
    076            if (json_bridge == null)
    077            {
    078                json_bridge = new JSONRPCBridge();
    079                session.setAttribute(JSON_BRIDGE_KEY, json_bridge);
    080            }
    081            return json_bridge;
    082        }
    083
    084        public void clearBridge(HttpSession session)
    085        {
    086            session.removeAttribute(JSON_BRIDGE_KEY);
    087        }
    088
    089    // The following is modeled on XmlRpcSercice.
    090    //    /**
    091    //     * Initialize the JsonRpcService.
    092    //     *
    093    //     * @throws InitializationException Something went wrong in the init stage.
    094    //     */
    095    //    public void init() throws InitializationException
    096    //    {
    097    //        //Configuration conf = getConfiguration();
    098    //        setInit(true);
    099    //    }
    100    //
    101    //    /**
    102    //     * Shuts down this service, stopping running threads.
    103    //     */
    104    //    public void shutdown()
    105    //    {
    106    //        setInit(false);
    107    //    }
    108
    109    }