When writing an m-bean, you must:
Create an m-let text file.
The information on each m-bean is specified in a single instance of a tag, called the MLET tag. When an m-let text file is loaded, an instance of each m-bean specified in the file is created. For example, the m-let text file for the QDatabaseAccounting m-bean is contained in the file QDatabaseAccounting.html and looks like this:
<MLET CODE=com.sun.ba.beans.QDatabaseAccounting ARCHIVE="qdatabaseaccounting.jar, dbaccess.jar > <PARAM NAME=jdbcDriverName VALUE=com.sun.jdbc.SimpleText.SimpleTextDriver> <PARAM NAME=jdbcURLName VALUE=jdbc:SimpleText:/var/opt/SUNWconn/ba> </MLET>The attributes used in this example are:
The full Java class name of the m-bean
The files containing m-beans and other resources used by the QDataBaseAccounting m-bean. In this case the file qdatabaseaccounting.jar contains the required classes and dbaccess.jar contains the database driver.
A list of the parameters required by the QDataBaseAccounting m-bean. These will be passed as a modification list to the initCmf() method.
Implement the initCmf() method. The prototype of the initCmf() method must look like this:
. . . public void initCmf(Framework cmf, ObjectName name, boolean db, ModificationList list) . . .The ModificationList parameter is required in order to contain the list of parameters defined in the MLET tag.
You can also provide the bean and class names of the m-bean(s) to initialize:
. . . ObjectName statsName = new ObjectName(cmf.getDomain(), beanName); stats = (className) cmf.retrieveObject(statsName); . . .
The initCmf() method in the QDatabaseAccounting.java file looks like this:
. . . public void initCmf(Framework cmf, ObjectName name, boolean db, ModificationList list) throws InstanceNotFoundException, InstanceAlreadyExistException, ServiceNotFoundException { this.cmf = cmf; if (name == null) { name = new ObjectName(cmf.getDomain(), getClass().getName()); } if (db) { cmf.addDBObject(this, name); } else { cmf.addObject(this, name); } this.name = name; ObjectName statsName = new ObjectName(cmf.getDomain(), statsMOName); stats = (QStatsInterface) cmf.retrieveObject(statsName); String jdbcDriverName = null; Enumeration enum = list.elements(); while (enum.hasMoreElements()) { Modification modif = (Modification) enum.nextElement(); String property = modif.getProperty(); String value = (String) modif.getValue(); if (property.equalsIgnoreCase("jdbcDriverName")) { jdbcDriverName = value; } else if (property.equalsIgnoreCase("jdbcUrlName")) { jdbcUrlName = value; } else if (property.equalsIgnoreCase("jdbcUserName")) { jdbcUserName = value; } else if (property.equalsIgnoreCase("jdbcPassword")) { jdbcPassword = value; } else if (property.equalsIgnoreCase("jdbcTableName")) { jdbcTableName = value; } } try { Class.forName(jdbcDriverName); } catch (ClassNotFoundException e) { throw new ServiceNotFoundException("cannot load JDBC driver " + jdbcDriverName); } } . . .
Make sure that the m-bean declaration implements the ActivatableIf interface:
. . . public class QDatabaseAccounting implements QConstants, QFlowAccountingListene r, ActivatableIf { . . .This makes the performStart() and performStop() methods available.
Implement the performStart() method. In the case of the QDatabaseAccounting m-bean the performStart() method calls a method to start an event listener for flow accounting:
. . . public void performStart() { if (!isActive()) { try { jdbcConnection = DriverManager.getConnection(jdbcUrlName, jdbcUserName, jdbcPassword); flowTable = new QFlowTable(jdbcConnection, jdbcTableName); stats.addQFlowAccountingListener(this); } catch (SQLException e) { e.printStackTrace(); } } } . . .
Implement the performStop() method. For the QDatabaseAccounting m-bean, the performStop() method looks like this:
. . . public void performStop() { if (isActive()) { stats.removeQFlowAccountingListener(this); try { jdbcConnection.commit(); jdbcConnection.close(); jdbcConnection = null; flowTable = null; } catch (SQLException e) { e.printStackTrace(); } } } . . .
Implement the isActive() method:
. . . public boolean isActive() { return (jdbcConnection != null && flowTable != null); } . . .