    001package 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
    022import java.io.CharArrayWriter;
    023
    024import javax.servlet.http.HttpServletRequest;
    025import javax.servlet.http.HttpSession;
    026
    027import org.apache.turbine.services.TurbineServices;
    028import org.jabsorb.JSONRPCBridge;
    029
    030
    031/**
    032 * This is a static accessor class for {@link JsonRpcService}.
    033 *
    034 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
    035 * @version $Id: TurbineJsonRpc.java 1706239 2015-10-01 13:18:35Z tv $
    036 */
    037public abstract class TurbineJsonRpc
    038{
    039    /**
    040     * Returns system's configured implementation of {@link JsonRpcService}.
    041     *
    042     * @return an implementation of <code>JsonRpcService</code>
    043     */
    044    public static JsonRpcService getService()
    045    {
    046        return (JsonRpcService) TurbineServices.getInstance()
    047                .getService(JsonRpcService.SERVICE_NAME);
    048    }
    049
    050    /**
    051     * Process a JSON RPC call
    052     * @param cdata the JSON data
    053     * @param json_bridge the {@link JSONRPCBridge} object
    054     * @param request the request
    055     * @return the return object of the JSON RPC call
    056     */
    057    public static Object processCall(CharArrayWriter cdata,
    058            JSONRPCBridge json_bridge, HttpServletRequest request)
    059    {
    060        return getService().processCall(cdata, json_bridge, request);
    061    }
    062
    063    /**
    064     * Register an object with the {@link JSONRPCBridge} in a given session
    065     *
    066     * @param session the session
    067     * @param key the name of the object in the session
    068     * @param value the object to register
    069     */
    070    public static void registerObject(HttpSession session, String key, Object value)
    071    {
    072        getService().registerObject(session, key, value);
    073    }
    074
    075    /**
    076     * Register an object with the {@link JSONRPCBridge} globally
    077     *
    078     * @param key the name of the object in the session
    079     * @param value the object to register
    080     */
    081    public static void registerObjectGlobal(String key, Object value)
    082    {
    083        getService().registerObjectGlobal(key, value);
    084    }
    085
    086    /**
    087     * Get the {@link JSONRPCBridge} from the session
    088     *
    089     * @param session the session
    090     * @return the {@link JSONRPCBridge} instance
    091     */
    092    public static JSONRPCBridge getBridge(HttpSession session)
    093    {
    094        return getService().getBridge(session);
    095    }
    096
    097    /**
    098     * Remove the {@link JSONRPCBridge} from the session
    099     *
    100     * @param session the session
    101     */
    102    public static void clearBridge(HttpSession session)
    103    {
    104        getService().clearBridge(session);
    105    }
    106}