Class TabularDataAccessor

java.lang.Object
org.jolokia.service.serializer.json.TabularDataAccessor
All Implemented Interfaces:
ObjectAccessor

public class TabularDataAccessor extends Object implements ObjectAccessor
Since:
Apr 19, 2009
Author:
roland
  • Constructor Details

  • Method Details

    • getType

      public Class<?> getType()
      Description copied from interface: ObjectAccessor
      Gets a class of supported objects.
      Specified by:
      getType in interface ObjectAccessor
      Returns:
      supported type of the objects handled by this accessor
    • extractObject

      public Object extractObject(ObjectToJsonConverter pConverter, Object pTabularData, Deque<String> pPathParts, boolean pJsonify) throws AttributeNotFoundException

      Convert a TabularData to a JSONObject. There are 3 possible representations of the returned JSON object depending on TabularType. These are described also in TabularDataConverter.

      #1. If the TabularType describes Map representation defined in MXBean specification, the resulting map is simple JSONObject with key-value pairs, where each pair represents single row of TabularData.
      The requirements are:

      #2. If any of the TabularType.getIndexNames() point to CompositeType fields of type different than SimpleType, the converted TabularData is said to use complex keys. This means that the complex values can't be used as keys of a JSONObject.
      In this case, each row of the TabularData is converted to single JSONObject with keys being String item names of CompositeData and values being complex item values of CompositeData.
      In order to recreate such representation, index keys are stored under indexNames field of the returned JSONObject and rows are returned in JSONArray under values field of the returned JSONObject.

      #3. If all of the index items are SimpleType, we can convert the index values to Strings and use them as keys of the returned JSONObject. We can avoid then the extra indexNames and values top level fields and serialize the TabularData as multi-layered JSONObject where leafs are map representations of rows - with keys being item names of CompositeData. The more index names the more nested the leafs are stored in the returned JSONObject.

      Let's consider a CompositeType with 3 items:

      A JSON representation of this CompositeType could look like this:
       
       {
           "id": 123,
           "name": "some value",
           "attributes": {
               "a1": "v1",
               "a2": "v2"
           }
       }
       
       
      We can define two different tabular types based on this CompositeType:
      • One with index keys: id and name
      • One with index keys: id and attributes - this would be a valid TabularType with complex keys.

      Now, when the TabularType.getIndexNames() are all simple types we can get this nested map representation #3 (notice the duplication of data):

       
       {
           "123": {
               "some value": {
                   "id": 123,
                   "name": "some value",
                   "attributes": {
                       "a1": "v1",
                       "a2": "v2"
                   }
               }
           }
       }
       
       

      When the TabularType.getIndexNames() use also complex types, we'll get this representation #2:

       
       {
           "indexNames": [ "id", "attributes" ],
           "values": [
               {
                   "id": 123,
                   "name": "some value",
                   "attributes": {
                       "a1": "v1",
                       "a2": "v2"
                   }
               }
           ]
       }
       
       

      Accessing TabularData with a path is only supported for index keys of SimpleType, i.e. each index name must point to a String-convertible value. As many path elements must be provided as index names for the tabular type exists (i.e. pPathParts.size() >= pTabularData.getTabularType().getIndexNames().size())

      For TabularData of type #1, conforming to MXBean specification, a path access with the single key value will return the content of the value item. For non-MXBean tabular types, w need proper path specification.

      Specified by:
      extractObject in interface ObjectAccessor
      Parameters:
      pConverter - the global converter in order to be able do dispatch for serializing inner data types
      pTabularData - the value to convert
      pPathParts - extra arguments which contain e.g. a path
      pJsonify - whether to convert to a JSON object/list or whether the plain object should be returned. The later is required for writing an inner value
      Returns:
      the extracted object
      Throws:
      AttributeNotFoundException
    • canSetValue

      public boolean canSetValue()
      Description copied from interface: ObjectAccessor
      Whether this accessor can be used to set a value inside (directly or nested) a value of given class.
      Specified by:
      canSetValue in interface ObjectAccessor
      Returns:
      true if this extractor can be used to set inner values.
    • setObjectValue

      public Object setObjectValue(Converter<String> pConverter, Object pObject, String pAttribute, Object pValue) throws IllegalAccessException, InvocationTargetException
      Description copied from interface: ObjectAccessor
      If this accessor can be used to set inner values, this method sets the value inside the passed outer object.
      Specified by:
      setObjectValue in interface ObjectAccessor
      Parameters:
      pConverter - Converter used to convert the value being set to a class of the accessed attribute
      pObject - object on which to set the value
      pAttribute - attribute of the object to set. (For arrays or lists it should be an index.)
      pValue - the new value to set after conversion
      Returns:
      the old value of the changed attribute
      Throws:
      IllegalAccessException - if the attribute to set to is not accessible
      InvocationTargetException - reflection error