0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jmx.snmp.agent;
0N/A
0N/A
0N/A
0N/A// java imports
0N/A//
0N/Aimport java.io.Serializable;
0N/Aimport java.util.Date;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/A
0N/A// jmx imports
0N/A//
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.NotificationFilter;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.NotificationBroadcaster;
0N/Aimport javax.management.MBeanNotificationInfo;
0N/Aimport javax.management.ListenerNotFoundException;
0N/Aimport com.sun.jmx.snmp.SnmpOid;
0N/Aimport com.sun.jmx.snmp.SnmpValue;
0N/Aimport com.sun.jmx.snmp.SnmpVarBind;
0N/Aimport com.sun.jmx.snmp.SnmpStatusException;
0N/A
0N/A/**
0N/A * This class is an abstraction for an SNMP table.
0N/A * It is the base class for implementing SNMP tables in the
0N/A * MBean world.
0N/A *
0N/A * <p>
0N/A * Its responsibility is to synchronize the MBean view of the table
0N/A * (Table of entries) with the MIB view (array of OID indexes). Each
0N/A * object of this class will be bound to the Metadata object which
0N/A * manages the same SNMP Table within the MIB.
0N/A * </p>
0N/A *
0N/A * <p>
0N/A * For each table defined in a MIB, mibgen will generate a specific
0N/A * class called Table<i>TableName</i> that will subclass this class, and
0N/A * a corresponding <i>TableName</i>Meta class extending SnmpMibTable
0N/A * and corresponding to the MIB view of the same table.
0N/A * </p>
0N/A *
0N/A * <p>
0N/A * Objects of this class are instantiated by MBeans representing
0N/A * the SNMP Group to which the table belong.
0N/A * </p>
0N/A *
0N/A * <p><b>This API is a Sun Microsystems internal API and is subject
0N/A * to change without notice.</b></p>
0N/A * @see com.sun.jmx.snmp.agent.SnmpTableEntryFactory
0N/A * @see com.sun.jmx.snmp.agent.SnmpMibTable
0N/A *
0N/A */
0N/Apublic abstract class SnmpTableSupport implements SnmpTableEntryFactory,
0N/A// NPCTE fix for bugId 4499265, esc 0, MR 04 sept 2001
0N/A// SnmpTableCallbackHandler {
0N/A SnmpTableCallbackHandler, Serializable {
0N/A// end of NPCTE fix for bugId 4499265
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Protected Variables
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * The list of entries
0N/A **/
0N/A protected List<Object> entries;
0N/A
0N/A /**
0N/A * The associated metadata object
0N/A **/
0N/A protected SnmpMibTable meta;
0N/A
0N/A /**
0N/A * The MIB to which this table belongs
0N/A **/
0N/A protected SnmpMib theMib;
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Private Variables
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * This variable is initialized while binding this object to its
0N/A * corresponding meta object.
0N/A **/
0N/A private boolean registrationRequired = false;
0N/A
0N/A
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Constructor
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * Initializes the table.
0N/A * The steps are these:
0N/A * <ul><li> allocate an array for storing entry object,</li>
0N/A * <li> retrieve the corresponding metadata object
0N/A * from the MIB,
0N/A * <li> bind this object to the corresponding metadata object
0N/A * from the MIB.</li>
0N/A * </ul>
0N/A *
0N/A * @param mib The MIB to which this table belong.
0N/A *
0N/A **/
0N/A protected SnmpTableSupport(SnmpMib mib) {
0N/A theMib = mib;
0N/A meta = getRegisteredTableMeta(mib);
0N/A bindWithTableMeta();
0N/A entries = allocateTable();
0N/A }
0N/A
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Implementation of the SnmpTableEntryFactory interface
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * Creates a new entry in the table.
0N/A *
0N/A * This factory method is generated by mibgen and used internally.
0N/A * It is part of the
0N/A * {@link com.sun.jmx.snmp.agent.SnmpTableEntryFactory} interface.
0N/A * You may subclass this method to implement any specific behaviour
0N/A * your application requires.
0N/A *
0N/A * @exception SnmpStatusException if the entry cannot be created.
0N/A **/
0N/A public abstract void createNewEntry(SnmpMibSubRequest request,
0N/A SnmpOid rowOid, int depth,
0N/A SnmpMibTable meta)
0N/A throws SnmpStatusException;
0N/A
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Public methods
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * Returns the entry located at the given position in the table.
0N/A *
0N/A * @return The entry located at the given position, <code>null</code>
0N/A * if no entry can be found at this position.
0N/A **/
0N/A // XXXX xxxx zzz ZZZZ => public? or protected?
0N/A public Object getEntry(int pos) {
0N/A if (entries == null) return null;
0N/A return entries.get(pos);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of entries registered in the table.
0N/A *
0N/A * @return The number of entries registered in the table.
0N/A **/
0N/A public int getSize() {
0N/A return meta.getSize();
0N/A }
0N/A
0N/A /**
0N/A * This method lets you dynamically switch the creation policy.
0N/A *
0N/A * <CODE>setCreationEnabled()</CODE> will switch the policy of
0N/A * remote entry creation via SET operations, by calling
0N/A * <code>setCreationEnabled()</code> on the metadata object
0N/A * associated with this table.
0N/A * <BR> By default remote entry creation via SET operation is disabled.
0N/A *
0N/A * @param remoteCreationFlag Tells whether remote entry creation must
0N/A * be enabled or disabled.
0N/A * <li>
0N/A * <CODE>setCreationEnabled(true)</CODE> will enable remote entry
0N/A * creation via SET operations.</li>
0N/A * <li>
0N/A * <CODE>setCreationEnabled(false)</CODE> will disable remote entry
0N/A * creation via SET operations.</li>
0N/A * <p> By default remote entry creation via SET operation is disabled.
0N/A * </p>
0N/A *
0N/A * @see com.sun.jmx.snmp.agent.SnmpMibTable
0N/A *
0N/A **/
0N/A public void setCreationEnabled(boolean remoteCreationFlag) {
0N/A meta.setCreationEnabled(remoteCreationFlag);
0N/A }
0N/A
0N/A /**
0N/A * Tells whether a new entry should be created when a SET operation
0N/A * is received for an entry that does not exist yet.
0N/A * This method calls <code>isCreationEnabled()</code> on the metadata
0N/A * object associated with this table.
0N/A *
0N/A * @return true if a new entry must be created, false otherwise.<br>
0N/A * [default: returns <CODE>false</CODE>]
0N/A *
0N/A * @see com.sun.jmx.snmp.agent.SnmpMibTable
0N/A **/
0N/A public boolean isCreationEnabled() {
0N/A return meta.isCreationEnabled();
0N/A }
0N/A
0N/A /**
0N/A * Tells whether the metadata object to which this table is linked
0N/A * requires entries to be registered. In this case passing an
0N/A * ObjectName when registering entries will be mandatory.
0N/A *
0N/A * @return <code>true</code> if the associated metadata requires entries
0N/A * to be registered (mibgen generated generic metadata).
0N/A **/
0N/A public boolean isRegistrationRequired() {
0N/A return registrationRequired;
0N/A }
0N/A
0N/A /**
0N/A * Builds an entry SnmpIndex from its row OID.
0N/A *
0N/A * This method is generated by mibgen and used internally.
0N/A *
0N/A * @param rowOid The SnmpOid object identifying a table entry.
0N/A *
0N/A * @return The SnmpIndex of the entry identified by <code>rowOid</code>.
0N/A *
0N/A * @exception SnmpStatusException if the index cannot be built from the
0N/A * given OID.
0N/A **/
0N/A public SnmpIndex buildSnmpIndex(SnmpOid rowOid)
0N/A throws SnmpStatusException {
0N/A return buildSnmpIndex(rowOid.longValue(false), 0);
0N/A }
0N/A
0N/A /**
0N/A * Builds an SnmpOid from an SnmpIndex object.
0N/A *
0N/A * This method is generated by mibgen and used internally.
0N/A *
0N/A * @param index An SnmpIndex object identifying a table entry.
0N/A *
0N/A * @return The SnmpOid form of the given entry index.
0N/A *
0N/A * @exception SnmpStatusException if the given index is not valid.
0N/A **/
0N/A public abstract SnmpOid buildOidFromIndex(SnmpIndex index)
0N/A throws SnmpStatusException;
0N/A
0N/A /**
0N/A * Builds the default ObjectName of an entry from the SnmpIndex
0N/A * identifying this entry. No access is made on the entry itself.
0N/A *
0N/A * This method is generated by mibgen and used internally.
0N/A * You can subclass this method if you want to change the default
0N/A * ObjectName policy. This is only meaningfull when entries
0N/A * are registered MBeans.
0N/A *
0N/A * @param index The SnmpIndex identifying the entry from which we
0N/A * want to build the default ObjectName.
0N/A *
0N/A * @return The default ObjectName for the entry identified by
0N/A * the given index.
0N/A *
0N/A * @exception SnmpStatusException if the given index is not valid.
0N/A **/
0N/A public abstract ObjectName buildNameFromIndex(SnmpIndex index)
0N/A throws SnmpStatusException;
0N/A
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Implementation of the SnmpTableEntryFactory interface
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * This callback is called by the associated metadata object
0N/A * when a new table entry has been registered in the
0N/A * table metadata.
0N/A *
0N/A * This method will update the <code>entries</code> list.
0N/A *
0N/A * @param pos The position at which the new entry was inserted
0N/A * in the table.
0N/A * @param row The row OID of the new entry
0N/A * @param name The ObjectName of the new entry (as specified by the
0N/A * factory)
0N/A * @param entry The new entry (as returned by the factory)
0N/A * @param meta The table metadata object.
0N/A *
0N/A **/
0N/A public void addEntryCb(int pos, SnmpOid row, ObjectName name,
0N/A Object entry, SnmpMibTable meta)
0N/A throws SnmpStatusException {
0N/A try {
0N/A if (entries != null) entries.add(pos,entry);
0N/A } catch (Exception e) {
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchName);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This callback is called by the associated metadata object
0N/A * when a new table entry has been removed from the
0N/A * table metadata.
0N/A *
0N/A * This method will update the <code>entries</code> list.
0N/A *
0N/A * @param pos The position from which the entry was deleted
0N/A * @param row The row OID of the deleted entry
0N/A * @param name The ObjectName of the deleted entry (may be null if
0N/A * ObjectName's were not required)
0N/A * @param entry The deleted entry (may be null if only ObjectName's
0N/A * were required)
0N/A * @param meta The table metadata object.
0N/A *
0N/A **/
0N/A public void removeEntryCb(int pos, SnmpOid row, ObjectName name,
0N/A Object entry, SnmpMibTable meta)
0N/A throws SnmpStatusException {
0N/A try {
0N/A if (entries != null) entries.remove(pos);
0N/A } catch (Exception e) {
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Enables to add an SNMP entry listener to this
0N/A * <CODE>SnmpMibTable</CODE>.
0N/A *
0N/A * @param listener The listener object which will handle the
0N/A * notifications emitted by the registered MBean.
0N/A *
0N/A * @param filter The filter object. If filter is null, no filtering
0N/A * will be performed before handling notifications.
0N/A *
0N/A * @param handback The context to be sent to the listener when a
0N/A * notification is emitted.
0N/A *
0N/A * @exception IllegalArgumentException Listener parameter is null.
0N/A */
0N/A public void
0N/A addNotificationListener(NotificationListener listener,
0N/A NotificationFilter filter, Object handback) {
0N/A meta.addNotificationListener(listener,filter,handback);
0N/A }
0N/A
0N/A /**
0N/A * Enables to remove an SNMP entry listener from this
0N/A * <CODE>SnmpMibTable</CODE>.
0N/A *
0N/A * @param listener The listener object which will handle the
0N/A * notifications emitted by the registered MBean.
0N/A * This method will remove all the information related to this
0N/A * listener.
0N/A *
0N/A * @exception ListenerNotFoundException The listener is not registered
0N/A * in the MBean.
0N/A */
0N/A public synchronized void
0N/A removeNotificationListener(NotificationListener listener)
0N/A throws ListenerNotFoundException {
0N/A meta.removeNotificationListener(listener);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <CODE>NotificationInfo</CODE> object containing the
0N/A * notification class and the notification type sent by the
0N/A * <CODE>SnmpMibTable</CODE>.
0N/A */
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A return meta.getNotificationInfo();
0N/A }
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Protected Abstract methods
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * Builds an SnmpIndex object from the index part of an OID.
0N/A *
0N/A * This method is generated by mibgen and used internally.
0N/A *
0N/A * @param oid The OID from which to build the index, represented
0N/A * as an array of long.
0N/A * @param start The position where to start from in the OID array.
0N/A *
0N/A * @return The SnmpOid form of the given entry index.
0N/A *
0N/A * @exception SnmpStatusException if the given index is not valid.
0N/A **/
0N/A protected abstract SnmpIndex buildSnmpIndex(long oid[], int start )
0N/A throws SnmpStatusException;
0N/A
0N/A /**
0N/A * Returns the metadata object associated with this table.
0N/A *
0N/A * This method is generated by mibgen and used internally.
0N/A *
0N/A * @param mib The SnmpMib object holding the Metadata corresponding
0N/A * to this table.
0N/A *
0N/A * @return The metadata object associated with this table.
0N/A * Returns <code>null</code> if this implementation of the
0N/A * MIB doesn't support this table.
0N/A **/
0N/A protected abstract SnmpMibTable getRegisteredTableMeta(SnmpMib mib);
0N/A
0N/A
0N/A //-----------------------------------------------------------------
0N/A //
0N/A // Protected methods
0N/A //
0N/A //-----------------------------------------------------------------
0N/A
0N/A /**
0N/A * Allocates an ArrayList for storing table entries.
0N/A *
0N/A * This method is called within the constructor at object creation.
0N/A * Any object implementing the {@link java.util.List} interface can
0N/A * be used.
0N/A *
0N/A * @return A new list in which to store entries. If <code>null</code>
0N/A * is returned then no entry will be stored in the list
0N/A * and getEntry() will always return null.
0N/A **/
0N/A protected List<Object> allocateTable() {
0N/A return new ArrayList<Object>();
0N/A }
0N/A
0N/A /**
0N/A * Add an entry in this table.
0N/A *
0N/A * This method registers an entry in the table and perform
0N/A * synchronization with the associated table metadata object.
0N/A *
0N/A * This method assumes that the given entry will not be registered,
0N/A * or will be registered with its default ObjectName built from the
0N/A * associated SnmpIndex.
0N/A * <p>
0N/A * If the entry is going to be registered, then
0N/A * {@link com.sun.jmx.snmp.agent.SnmpTableSupport#addEntry(SnmpIndex, ObjectName, Object)} should be prefered.
0N/A * <br> This function is mainly provided for backward compatibility.
0N/A *
0N/A * @param index The SnmpIndex built from the given entry.
0N/A * @param entry The entry that should be added in the table.
0N/A *
0N/A * @exception SnmpStatusException if the entry cannot be registered with
0N/A * the given index.
0N/A **/
0N/A protected void addEntry(SnmpIndex index, Object entry)
0N/A throws SnmpStatusException {
0N/A SnmpOid oid = buildOidFromIndex(index);
0N/A ObjectName name = null;
0N/A if (isRegistrationRequired()) {
0N/A name = buildNameFromIndex(index);
0N/A }
0N/A meta.addEntry(oid,name,entry);
0N/A }
0N/A
0N/A /**
0N/A * Add an entry in this table.
0N/A *
0N/A * This method registers an entry in the table and performs
0N/A * synchronization with the associated table metadata object.
0N/A *
0N/A * @param index The SnmpIndex built from the given entry.
0N/A * @param name The ObjectName with which this entry will be registered.
0N/A * @param entry The entry that should be added in the table.
0N/A *
0N/A * @exception SnmpStatusException if the entry cannot be registered with
0N/A * the given index.
0N/A **/
0N/A protected void addEntry(SnmpIndex index, ObjectName name, Object entry)
0N/A throws SnmpStatusException {
0N/A SnmpOid oid = buildOidFromIndex(index);
0N/A meta.addEntry(oid,name,entry);
0N/A }
0N/A
0N/A /**
0N/A * Remove an entry from this table.
0N/A *
0N/A * This method unregisters an entry from the table and performs
0N/A * synchronization with the associated table metadata object.
0N/A *
0N/A * @param index The SnmpIndex identifying the entry.
0N/A * @param entry The entry that should be removed in the table. This
0N/A * parameter is optional and can be omitted if it doesn't
0N/A * need to be passed along to the
0N/A * <code>removeEntryCb()</code> callback defined in the
0N/A * {@link com.sun.jmx.snmp.agent.SnmpTableCallbackHandler}
0N/A * interface.
0N/A *
0N/A * @exception SnmpStatusException if the entry cannot be unregistered.
0N/A **/
0N/A protected void removeEntry(SnmpIndex index, Object entry)
0N/A throws SnmpStatusException {
0N/A SnmpOid oid = buildOidFromIndex(index);
0N/A meta.removeEntry(oid,entry);
0N/A }
0N/A
0N/A // protected void removeEntry(ObjectName name, Object entry)
0N/A // throws SnmpStatusException {
0N/A // meta.removeEntry(name,entry);
0N/A // }
0N/A
0N/A /**
0N/A * Returns the entries in the table.
0N/A *
0N/A * @return An Object[] array containing the entries registered in the
0N/A * table.
0N/A **/
0N/A protected Object[] getBasicEntries() {
0N/A if (entries == null) return null;
0N/A Object[] array= new Object[entries.size()];
0N/A entries.toArray(array);
0N/A return array;
0N/A }
0N/A
0N/A /**
0N/A * Binds this table with its associated metadata, registering itself
0N/A * as an SnmpTableEntryFactory.
0N/A **/
0N/A protected void bindWithTableMeta() {
0N/A if (meta == null) return;
0N/A registrationRequired = meta.isRegistrationRequired();
0N/A meta.registerEntryFactory(this);
0N/A }
0N/A
0N/A}