Implemented Tables

 



 

Overview

 

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. 

 

Instrumentation

 

The file generated for the Table group ShoppingCartTable are,

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;
try {
table = getShoppingCartCollection()
} catch(Exception e1) {
// TODO: logging
return null;
}
if(table == null) {
// TODO: logging
return null;
}
com.adventnet.agent.application.shoppingcart.ShoppingItem shoppingItem = (com.adventnet.agent.application.shoppingcart.ShoppingItem)(table.get(0));
ShoppingCartEntry tableEntry = new ShoppingCartEntry(shoppingItem);
return makeCompositeData(tableEntry);
}

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);
if(shoppingItem == null){
return null;
}
ShoppingCartEntry tableEntry = new ShoppingCartEntry(shoppingItem);
return makeCompositeData(tableEntry);
}

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;
try {
table = getShoppingCartCollection();
} catch(Exception e1) {
// TODO: logging
return null;
}
if(table == null) {
// TODO: logging
return null;
}
int len = table.size();
for(int i=0; i<len; i++) {
com.adventnet.agent.application.shoppingcart.ShoppingItem shoppingItem = (com.adventnet.agent.application.shoppingcart.ShoppingItem)(table.get(i));
if(isIndexEqual(indexObjects, shoppingItem)) {
try {
ShoppingCartEntry tableEntry = new ShoppingCartEntry((com.adventnet.agent.application.shoppingcart.ShoppingItem)table.get(i+1));
return makeCompositeData(tableEntry);
} catch(Exception ee) {
// TODO: logging
return null;
}
}
}
return null;
}
 

To Add a Row to the Table

 

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))
throw new AgentException("Row Already exists, CommonUtils.ROWCREATIONFAILED);
ShoppingCartEntry tableEntry = makeEntry(indexObjects, entry);
String petname = (java.lang.String)Utilities.convertStringToObject("java.lang.String", entry.getDataItem("PetName").toString());
Integer qty = (java.lang.Integer)Utilities.convertStringToObject("java.lang.Integer", entry.getDataItem("Quantity").toString());
addItem(petname, qty);
}

 

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 {
java.util.List table = getShoppingCartCollection();
for(int i=0; i<table.size(); i++) {
com.adventnet.agent.application.shoppingcart.ShoppingItem shoppingItem = (com.adventnet.agent.application.shoppingcart.ShoppingItem)(table.get(i));
if(isIndexEqual(indexObjects, shoppingItem)) {
removeItem(shoppingItem.getPetName());
}
}
}

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))
throw new AgentException("Row doesn't exists", CommonUtils.MODIFYROWNOTEXIST);
com.adventnet.agent.application.shoppingcart.ShoppingItem shoppingItem = getRowObject(indexObjects);
if(shoppingItem == null)
return;
String key = null;
ShoppingCartEntry tableEntry = new ShoppingCartEntry(shoppingItem);
String petname = (java.lang.String)Utilities.convertStringToObject("java.lang.String", entry.getDataItem("PetName").toString());
Integer qty = (java.lang.Integer)Utilities.convertStringToObject("java.lang.Integer", entry.getDataItem("Quantity").toString());
updateItem(petname, qty);
}

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;
try {
table = getShoppingCartCollection();
} catch(Exception e1) {
// TODO: logging
return 0;
}
if(table == null) {
// TODO: logging
return 0;
}
return table.size();
}

 

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) {
java.util.List table = null;
try {
table = getShoppingCartCollection();
} catch(Exception e1) {
// TODO: logging
return new ArrayList();
}
int i = startIndex - 1;
ArrayList arrayList = new ArrayList((endIndex - startIndex)+1);
while (i < endIndex) {
try {
Object[] instances = new Object[indexNames.length];
com.adventnet.agent.application.shoppingcart.ShoppingItem shoppingItem = (com.adventnet.agent.application.shoppingcart.ShoppingItem)(table.get(i));
instances[0] = shoppingItem.getPetName();
if (!arrayList.add(instances)) {
// TODO: logging
return null;
}
i++;
} catch(ArrayIndexOutOfBoundsException aiobe) {
// TODO: logging
return arrayList;
} catch(Exception e) {
// TODO: logging
return arrayList;
}
}
return arrayList;
}

 



Copyright © 1996-2004, AdventNet Inc. All Rights Reserved.