View Javadoc

1   package org.apache.turbine.services.jsonrpc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.CharArrayWriter;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.turbine.services.TurbineBaseService;
30  
31  import com.metaparadigm.jsonrpc.JSONRPCBridge;
32  
33  /***
34   * This is a service that will respond to JSON-RPC calls.
35   *
36   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
37   * @version $Id$
38   */
39  public class TurbineJsonRpcService
40          extends TurbineBaseService
41          implements JsonRpcService
42  {
43      /*** Log. */
44      private static Log log = LogFactory.getLog(TurbineJsonRpcService.class);
45  
46      /*** The key used to store the bridge in the session. */
47      public static final String JSON_BRIDGE_KEY = "JSONRPCBridge";
48      /***
49       * The debug option for the bridge can be enabled by enabling debug level
50       * logging for this class.
51       */
52      private static final boolean DEBUG = log.isDebugEnabled();
53  
54      public Object processCall(CharArrayWriter cdata,
55              JSONRPCBridge json_bridge, HttpServletRequest request)
56      {
57          return JSONProcessor.processCall(cdata, json_bridge, request);
58      }
59  
60      public void registerObjectGlobal(String key, Object value)
61      {
62          JSONRPCBridge.getGlobalBridge().setDebug(DEBUG);
63          JSONRPCBridge.getGlobalBridge().registerObject(key, value);
64      }
65  
66      public void registerObject(HttpSession session, String key, Object value)
67      {
68          JSONRPCBridge json_bridge = getBridge(session);
69          json_bridge.setDebug(DEBUG);
70          json_bridge.registerObject(key, value);
71      }
72  
73      public JSONRPCBridge getBridge(HttpSession session)
74      {
75          JSONRPCBridge json_bridge = (JSONRPCBridge) session.getAttribute(JSON_BRIDGE_KEY);
76          if (json_bridge == null)
77          {
78              json_bridge = new JSONRPCBridge();
79              session.setAttribute(JSON_BRIDGE_KEY, json_bridge);
80          }
81          return json_bridge;
82      }
83  
84      public void clearBridge(HttpSession session)
85      {
86          session.removeAttribute(JSON_BRIDGE_KEY);
87      }
88  
89  // The following is modeled on XmlRpcSercice. 
90  //    /***
91  //     * Initialize the JsonRpcService.
92  //     *
93  //     * @throws InitializationException Something went wrong in the init stage.
94  //     */
95  //    public void init() throws InitializationException
96  //    {
97  //        //Configuration conf = getConfiguration();
98  //        setInit(true);
99  //    }
100 //
101 //    /***
102 //     * Shuts down this service, stopping running threads.
103 //     */
104 //    public void shutdown()
105 //    {
106 //        setInit(false);
107 //    }
108 
109 }