Java Dynamic Management Kit 3.2 Programming Guide
[ Previous ][ Fast Back ]Chapter 19. Developing SNMP Agents With the Java Dynamic Management Kit[ Fast Forward ][ Next ]

Implementing Access Methods

M-beans generated from SNMP MIBs require you to add access methods to be able to read and write m-bean properties. The following subsections show how to implement access methods for:

Implementing Access Methods for Simple Groups

Each SNMP group is represented by an m-bean named after the group name. In the case of MIB II, the system group is represented by an m-bean called System. The metadata of the System object are contained in an object called SystemMeta. The code generated by mibgen does not impose an inheritance scheme on the generated object. This is because the Java language supports only a single inheritance scheme. Therefore, you are free to use your own inheritance scheme when designing the implementation of your MIB. Example 19-1 shows the code for the System group. In this case there is no predefined inheritance.

Example 19-1. Code Generated by mibgen to Implement the System Group
//
// Generated by mibgen version 3.0 when compiling RFC1213-MIB.
//

import java.io.*;
import java.util.*;
import com.sun.jaw.impl.adaptor.snmp.*;

/**
 * The class is used for implementing the "System" group.
 * The group is defined with the following oid: 1.3.6.1.2.1.1.
 */
public class System implements  Serializable {

For each variable of a group, mibgen generates:

In each of the generated methods, you need to add code for accessing or setting the variable. For example, in the System group, the sysLocation variable is defined as a read-write variable. The mibgen compiler generates the code shown in Example 19-2.

Example 19-2. Code Generated by mibgen to Implement sysLocation
/**
 * Variable for storing the value of "SysLocation".
 * The variable is identified by: "1.3.6.1.2.1.1.6".
 */
 protected String SysLocation= new String("Sample implementation
                                            of system group");
...
/**
 * Getter for the "SysLocation" variable.
 */
 public String getSysLocation() throws SnmpStatusException {
 return SysLocation;
 }

/**
 * Setter for the "SysLocation" variable.
 */
 public  void setSysLocation(String x) throws
                            SnmpStatusException {
    SysLocation = x;
 }

/**
 * Checker for the "SysLocation" variable.
 */
 public  void checkSysLocation(String x) throws
                              SnmpStatusException {
    //
    // Add your own checking policy.
    //
 }


Note - The checker method must throw an SnmpStatusException if the requested operation cannot be performed.


Implementing Access Methods for Tables and Entries

Tables are not represented as specific m-beans but as a set of entries. For each table, mibgen generates a Java class containing the SNMP view of the table. In the case of MIB II, the TCP group is represented by an m-bean called tcp. The mibgen compiler generates a Java class called TabletcpConnTable to represent the SNMP view of tcpConnTable. An instance of TabletcpConnTable is created in the constructor of the group. The code generated by mibgen to implement the TCP group is shown in Example 19-3.

Example 19-3. Code Generated by mibgen to Implement the TCP Group
/**
 * Constructor for the "tcp" group.
 */
 public tcp(SnmpMib myMib) {
    tcpConnTable = new TabletcpConnTable (myMib);
 }

Entries are represented in exactly the same way as groups, that is, as m-beans. A metadata object is generated for each entry type. The tcpConnTable table contains a tcpConnEntry object. Therefore, mibgen generates a tcpConnEntry class to represent an entry in tcpConnTable. The code generated by mibgen to implement tcpConnEntry is shown in Example 19-4.

Example 19-4. Code Generated by mibgen to Implement tcpConnEntry
//
// Generated by mibgen version 3.0 when compiling RFC1213-MIB.
//

import java.io.*;
import java.util.*;
import com.sun.jaw.impl.adaptor.snmp.*;

/**
 * The class is used for implementing the "tcpConnEntry" group.
 * The group is defined with the following oid: 1.3.6.1.2.1.6.13.1.
 */
public class tcpConnEntry implements  Serializable {
...
}

Table objects provide methods for adding or removing entries from the table. Example 19-5 shows how to add a new tcpConnEntry to tcpConnTable.

Example 19-5. Adding a New Entry to a Table
/**
 * Constructor for the "tcp" group.
 */
public tcp(SnmpMib myMib) {
  tcpConnTable = new TabletcpConnTable (myMib);

  try {
    // Create the entry object
    //
    tcpConnEntry entry= new tcpConnEntry(myMib);

    // Initialize some fields
    //
    entry.tcpConnLocalAddress= "1.2.3.4";

    // Add the entry into the table
    //
    tcpConnTable.addEntry(entry);

  } catch(Exception e) {
    e.printStackTrace();
  }
} 

If you want to be notified each time a table entry is added or removed, instantiate a class that implements the SnmpTableEntryListener interface. This interface defines two callback methods for classes that require such notification. Code for a class implementing the SnmpTableEntryListener interface is shown in Example 19-6. An SnmpTableEntryEvent is sent to the class implementing the SnmpTableEntryListener interface.

Example 19-6. Implementing the SnmpTableEntryListener interface
public class SnmpTableEntryListenerImpl implements
   SnmpTableEntryListener {
   // Invoked when an entry is added to a SNMP table
   public void entryAdded(SnmpTableEntryEvent event) {
   ...
   }

   //Invoked when an entry is removed from the SNMP table.
   public void entryRemoved(SnmpTableEntryEvent event) {
   ...
   }
}

The class implementing the SnmpTableEntryListener interface must be added as a listener to the table to receive SnmpTableEntryEvent objects. This is shown in Example 19-7.

Example 19-7. Adding an SnmpTableEntryListener
//add a listener to the table
tcpConnTable.addSnmpTableEntryListener(new TableEntryListenerImpl());

When dealing with tables, SNMP indexes are handled automatically. The table object transparently maps an SNMP index to a Java table entry.

When a set request is received on a table object for a variable that does not exist, the table object rejects the operation. You can, however, customize this behavior so that a new entry is created when a set operation does not specify an existing entry. Do this by:

Example 19-8 shows the tcpConnTable class as it appears before customization.

Example 19-8. TabletcpConnTable Class Before Customization
public class TabletcpConnTable extends SnmpMibTable implements

Serializable {
...
}

Example 19-9 shows how to customize the TabletcpConnTable class to allow the addition of new entries.

Example 19-9. Customizing the TabletcpConnTable Class
public class TabletcpConnTable extends SnmpMibTableRemCreate

implements Serializable {
...
}


Note - By default, the generated code does not register table entries with the framework. Table entries are indexed properties within a group.



[ Previous ][ Home ][ Next ]
Overview of the Agent Development Process[ Up ]Loading MIBs Into an SNMP Adaptor