Sun Microsystems
Products & Services
 
Support & Training
 
 

Previous Previous     Contents     Index     Next Next

18.2.2 Customized Classes

As was the case for the simple SNMP table example, to be able to tie the code generated by mibgen to its proper instrumentation, you must extend the classes generated by mibgen in a series of custom classes. The customized classes are those provided in the examplesDir/current/Snmp/MBeanTable directory.

The customized extensions to the generated classes add the following functionality:

  • JMX_MBEAN_SERVER_MIB_Impl extends the generated JMX_MBEAN_SERVER_MIB, instantiating JmxMBeanServerImpl instead of the generated JmxMBeanServer skeleton. In addition, JMX_MBEAN_SERVER_MIB_Impl adds a new method, start(), that when called starts mirroring the content of the MBean server into the MIB.

    The start() method instructs the JmxMBeanServerImpl group to populate the jmxMBeanTable and jmxMBeanAttrTable according to the initial content of the MBean server mirrored by this MIB. The start() method also instructs the JmxMBeanServerImpl group to start listening for MBeanServerNotifications coming from the MBean server.

  • JmxMBeanServerImpl extends the JmxMBeanServer skeleton, and instantiates a subclass of TableJmxMBeanTable, in which the MBeans registered in the MBean server are mirrored. The TableJmxMBeanTable object is subclassed to support remote creation and deletion of entries in the jmxMBeanTable through SNMP.

    • An SNMP SET request that creates a new row in the jmxMBeanTable is transformed into a createMBean() call to the MBean server.

    • An SNMP SET request deleting an existing row in the jmxMBeanTable is transformed in an unregisterMBean() call to the MBean server.

    The JmxMBeanServerImpl also listens for notifications of the type MBeanServerNotification to keep the jmxMBeanTable updated, and creates and deletes rows in the jmxMBeanAttrTable accordingly.

  • JmxMBeanAttrEntryImpl extends the generated JmxMBeanAttrEntry, and is used to mirror an attribute of an MBean that is itself mirrored in the jmxMBeanTable. A JmxMBeanAttrEntryImpl object adds instrumentation to a conceptual row in the jmxMBeanAttrTable. When a new row is added to jmxMBeanTable, the corresponding JmxMBeanEntryImpl updates the jmxMBeanAttrTable by adding one row per attribute exposed by the mirrored MBean. When a row is deleted from the jmxMBeanTable, the JmxMBeanEntryImpl deletes the corresponding rows from the jmxMBeanAttrTable. The jmxMBeanAttrTable is thus managed entirely internally by the agent. Its instances cannot be created or deleted by a remote SNMP manager, except as a side effect of the creation or deletion of a row in the jmxMBeanTable.

  • JmxMBeanEntryImpl extends the generated JmxMBeanEntry skeleton. JmxMBeanEntryImpl is used to mirror MBeans that are registered in the MBean server that is made accessible through SNMP by the JMX_MBEAN_SERVER_MIB_Impl MIB. A JmxMBeanEntryImpl object adds instrumentation to a conceptual row in the jmxMBeanTable. JmxMBeanEntryImpl objects can be created or deleted as a result of SNMP SET requests, or as a result of an MBean being locally registered or deregistered from the MBean server mirrored by the MIB. The JmxMBeanServerImpl object keeps the jmxMBeanTable updated, and thus manages the life cycle of the JmxMBeanEntryImpl objects. The JmxMBeanEntryImpl objects keep the jmxMBeanAttrTable updated, and thus manage the life cycle of the JmxMBeanAttrEntryImpl objects.

Aspects of the JmxMBeanServerImpl and JmxMBeanEntryImpl classes are examined in more detail in the following sections.

18.2.2.1 JmxMBeanServerImpl

As stated previously, JmxMBeanServerImpl extends the JmxMBeanServer empty skeleton class. JmxMBeanServerImpl also instantiates a subclass of TableJmxMBeanTable, to mirror the MBeans registered in an MBean server.

Example 18-4 Subclassing TableJmxMBeanTable

[...]
    private final void inittables() {

       JmxMBeanTable = new TableJmxMBeanTable(myMib,null) {
             public Object createJmxMBeanEntryMBean(SnmpMibSubRequest req,
                                                    SnmpOid rowOid, 
                                                    int depth, 
                                                    ObjectName entryObjName,
                                                    SnmpMibTable meta, 
                                                    Integer  aJmxMBeanIndex)
                 throws SnmpStatusException  {

                 return JmxMBeanServerImpl.this.
                    createJmxMBeanEntryMBean(req,rowOid,depth,entryObjName,
                                                 meta,aJmxMBeanIndex);
                }

                public void removeEntryCb(int pos, SnmpOid row, 
                                          ObjectName name,
                                          Object entry, SnmpMibTable meta)
                    throws SnmpStatusException {
                    super.removeEntryCb(pos,row,name,entry,meta);
                    final JmxMBeanEntryMBean e = 
                        (JmxMBeanEntryMBean) entry;
                    final EnumJmxMBeanRowStatus destroy =
                         new EnumJmxMBeanRowStatus(EnumRowStatus.destroy);
                    e.setJmxMBeanRowStatus(destroy);
                }
            };
	
        JmxMBeanTable.setCreationEnabled(true);
    }

[...]

In Example 18-4, JmxMBeanServerImpl defines a method, inittables, to instantiate and initialize its table objects. The inittables method creates a new instance of TableJmxMBeanTable, and overrides it so that its createJmxMBeanEntryMBean method returns a JmxMBeanEntryImpl object instead of the default JmxMBeanEntry object. This is done by defining a new createJmxMBeanEntryMBean method in the customized JmxMBeanEntryImpl class (see Example 18-5), and calling it on the parent JmxMBeanServerImpl object from the overridden TableJmxMBeanTable object, as shown in Example 18-4.

The removeEntryCb method is overridden, so that table entries representing MBeans also delete their associated entries in the jmxMBeanAtrrTable by calling setJmxMBeanRowStatus(destroy) when they are removed from the jmxMBeanTable.

Example 18-5 createJmxMBeanEntryMBean Method

[...]
    private Object createJmxMBeanEntryMBean(SnmpMibSubRequest req,
					                                SnmpOid rowOid, 
					                                int depth, 
					                                ObjectName entryObjName,
					                                SnmpMibTable meta, 
					                                Integer  aJmxMBeanIndex)
	         throws SnmpStatusException  {

		      SnmpVarBind rowStatusVb = req.getRowStatusVarBind();

	         if (rowStatusVb == null) 
	             throw new SnmpStatusException(
		                    SnmpStatusException.snmpRspNoCreation);

		      if (! isAvailable(aJmxMBeanIndex.intValue())) {
	             if (Boolean.getBoolean("info"))
                 System.out.println("Index is not suitable: " 
                     + aJmxMBeanIndex);
	             throw new SnmpStatusException(
		                    SnmpStatusException.snmpRspInconsistentValue);
	         }

		      JmxMBeanEntryImpl entry = 
	             new JmxMBeanEntryImpl(myMib,this);
	         entry.JmxMBeanIndex = aJmxMBeanIndex;

		      entry.createFromRemote(req,rowStatusVb);
	         return entry;
    }

[...]

As shown in Example 18-4, the createJmxMBeanEntryMBean method is called when a remote SNMP Manager creates a new row in a jmxMBeanTable. Example 18-5 shows how the createJmxMBeanEntryMBean method is overridden by the JmxMBeanServerImpl class, to add new entries in the jmxMBeanAttrTable as the rows are created in the table.

Of the parameters the customized version of createJmxMBeanEntryMBean takes when it is started, the following are the most significant..

  • req, the SnmpMibSubRequest containing the VarBind list pertaining to this new row.

  • rowOid, the index of this new row's OID.

  • meta, the SnmpMibTable metadata object of the jmxMBeanTable.

  • aJmxMBeanIndex, the value of the new entry's JmxMBeanIndex index variable.

Previous Previous     Contents     Index     Next Next