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  import org.jabsorb.JSONRPCBridge;
31  
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: TurbineJsonRpcService.java 1662679 2015-02-27 13:22:23Z gk $
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().registerObject(key, value);
63      }
64  
65      public void registerObject(HttpSession session, String key, Object value)
66      {
67          JSONRPCBridge json_bridge = getBridge(session);
68          json_bridge.registerObject(key, value);
69      }
70  
71      public JSONRPCBridge getBridge(HttpSession session)
72      {
73          JSONRPCBridge json_bridge = (JSONRPCBridge) session.getAttribute(JSON_BRIDGE_KEY);
74          if (json_bridge == null)
75          {
76              json_bridge = new JSONRPCBridge();
77              session.setAttribute(JSON_BRIDGE_KEY, json_bridge);
78          }
79          return json_bridge;
80      }
81  
82      public void clearBridge(HttpSession session)
83      {
84          session.removeAttribute(JSON_BRIDGE_KEY);
85      }
86  
87  // The following is modeled on XmlRpcSercice. 
88  //    /**
89  //     * Initialize the JsonRpcService.
90  //     *
91  //     * @throws InitializationException Something went wrong in the init stage.
92  //     */
93  //    public void init() throws InitializationException
94  //    {
95  //        //Configuration conf = getConfiguration();
96  //        setInit(true);
97  //    }
98  //
99  //    /**
100 //     * Shuts down this service, stopping running threads.
101 //     */
102 //    public void shutdown()
103 //    {
104 //        setInit(false);
105 //    }
106 
107 }