In this chapter we are going to see how instrumentation is done for the tables using the User Storage Model. The AdventNet Multi-Protocol agent supports storage of tabular data in text files, XML files, databases, and RAM. In some cases, tabular data may be held in some other form or stored externally in a different place. The JmxTableModelListener Interface is implemented to handle this type of user storage models .
To use the JmxTableModelListener, the XXXTable.java has to implement the com.adventnet.utils.jmx.JmxTableModelListener interface. This interface is used for handling tables in JMX when user needs the choice of handling the table entries rather than the Multi-Protocol agent holding them in memory.
The file generated for the Table group ShoppingCartTable are,
ShoppingCartTable.java
This file contains the methods for updating or retrieving the whole
Table, getting the first entry , getting the entry by passing the
instance etc.
ShoppingCartEntry.java
This file contains the getter and setter methods for petName , unitCost,
quantity , listPrice . The composite Data or a row in the table
refers to a single table entry.
As specified earlier in the code generation section, the storage option was specified as User Defined for the ShoppingCartTable of the shoppingCartMibModule. Hence, the following code is generated in ShoppingCartTable.java
|
public class ShoppingCartTable implements JmxTableModelListener |
With this, the ShoppingCartAgent gets initialized with User Storage Model and thus any request from the manager for that particular table is forwarded to the Interface implemented for this purpose. Data is retrieved from the table only when the request reaches the Agent The interface, when initialized, calls the respective methods present in the interface. These methods will be exposed to the MBeanServer through the ShoppingCartTable.xml file. In this tutorial, we have instrumented these methods , so that it returns the required information from the Shopping Cart application. Now, let us see how the methods present in the JmxTableModelListener interface are instrumented for this purpose.
To Get the First Entry in the Table
The getFirstEntry() method is used to get the first entry in the table. This method will be called when a walk is done on a table. After calling the getFirstEntry(), the getNextEntry() will be called while doing a walk on a Table. The return type of this method is a CompositeData entry object.
The following instrumentation is done inside this method:
|
public CompositeData getFirstEntry() { java.util.List table = null; |
The getShoppingCartCollection() which is defined in the ShoppingCartTable returns the list of shopping items from the ShoppingCartApplication
In order to retrieve the first shopping item from this list, we use the table.get(0) method.
The method makeCompositeData() of the ShoppingCartTable takes care of sending the corresponding entry for this shopping item.
To Get the Entry for the Given Row (Identified by the Instance)
The getEntry() method is used to get the entry for the given row, identified by the instance. This method will be called when a GET is done on a Table specifying the instance. It takes an Object array with index objects, which identify the row, as the parameter. It returns the CompositeData object of that instance. Each row in the table is a CompositeData object.
The following instrumentation is done inside this method:
|
public CompositeData getEntry(Object[] indexObjects) { com.adventnet.agent.application.shoppingcart.ShoppingItem
shoppingItem = getRowObject(indexObjects); |
In the above instrumentation, inorder to retrieve the shopping item for the given instance(indexObjects) we use the getRowObject() method.
The method makeCompositeData() of the ShoppingCartTable takes care of sending the corresponding entry for this shopping item.
To Get the Next Entry for the Given Row (Identified by the Instance)
The getNextEntry() method is used to get the next entry of the given row, identified by the instance. This method will be called while doing a walk on the Table, after calling the getFirstEntry(). It takes an Object array with index objects, which identify the row , as the parameter. It returns the CompositeData object of that instance.
The following instrumentation is done inside this method:
|
public CompositeData getNextEntry(Object[] indexObjects) { java.util.List table = null; |
In the above instrumentation, the getShoppingCartCollection() which is defined in the ShoppingCartTable returns the list of shopping items from the ShoppingCartApplication
Using the for loop we are finding out the shopping item for the given index. We are getting the next shopping item by incrementing 1.
The >addRow() method is used to add a row to the table. This method will be called when the manager is trying to add a row in the table. This method takes the following parameters: The Object array with index objects, which identify the row, and the Composite data entry.
The following instrumentation is done inside this method:
|
public void addRow(Object[] indexObjects, CompositeData entry) throws Exception { if(isRowExists(indexObjects))
|
If the entry does not already exist in the application a new entry is created using the makeEntry() method passing the index objects and the entry as parameters.
By calling the addItem()of the ShoppingCartTable, the shopping item for the corresponding entry is created and added to the ShoppingCart Application.
To Delete a Row from the Table
The deleteRow() method is used a delete a row from the table. This method will be called when the manager is trying to delete a row from the table. This method takes the object array with index objects, which identifies the row, as its parameter.
The following instrumentation is done inside this method:
|
public void deleteRow(Object[] indexObjects) throws Exception
{ |
In the above instrumentation to delete an existing row, we first get the list of shopping items by calling getShoppingCartCollection()method.
From the list, we find out the shopping item for the given instance and the same is removed using the removeItem()from the ShoppingCartApplicationTable. (ie) corresponding entry of the given instance is removed.
To Modify an Existing Row in the Table
The modifyRow() method is used to modify the values of a row in the table.This method will be called when the manager is trying to modify a row in the table. This method takes the following parameters : The Object array with index objects, which identify the row, and the Composite data entry.
The following instrumentation is done inside this method:
|
public void modifyRow(Object[] indexObjects, CompositeData entry) throws Exception { if(!isRowExists(indexObjects)) |
In the above instrumentation to modify an an existing row, a verification is made if the entry exists in the application using the isRowExists() method by passing the index object.
The shopping item for the given instance(indexObjects) is obtained using the getRowObject() method.
The values to be modified are modified in the composite data using the methods available in the Utlities object.
The updateItem() updates the values of the pet name and quantity in the ShoppingCartApplication.
To Return the Total Number of Rows in the Table
The totalRows() method is used to give the total number of rows in the table as an integer. The following instrumentation is done inside this method:
|
public int totalRows() { java.util.List table = null;
|
The getShoppingCartCollection() which is defined in the ShoppingCartTable returns the list of shopping items from the ShoppingCartApplication
The total rows in the table (i.e) the size of the list is obtained and returned.
To Get Instances in the Table between given two index values
The getEntries() method is used to get all the instances in the table between given two index values. It takes the startIndex and endIndex (both as int datatype) as the parameters and returns an ArrayList of instances in the table. The HTML adaptor calls this method while viewing the entries in the table. The TL1Adaptor calls this method while doing a GETALL on the Table.
The following instrumentation is done inside this method:
|
public java.util.List getEntries(int startIndex, int endIndex)
{
|
In the above instrumentation to get the table instances between the given range, we first get the list of shopping items by calling getShoppingCartCollection() method.
An array list is created to store the instances.
Using the while loop we get the shopping item and its instance (pet name). This instance is added to the array list. This continues until it reaches the end index.
The created array list now containing the instances is returned