org.snmp4j.agent
Class DefaultMOServer

java.lang.Object
  extended by org.snmp4j.agent.DefaultMOServer
All Implemented Interfaces:
MOServer

public class DefaultMOServer
extends java.lang.Object
implements MOServer

The default MO server implementation uses a sorted map for the managed object registry.

Version:
1.4
Author:
Frank Fock

Constructor Summary
DefaultMOServer()
           
 
Method Summary
 void addContext(org.snmp4j.smi.OctetString context)
          Adds the supplied context to the server.
 void addContextListener(ContextListener l)
          Adds a context listener to the server.
 void addLookupListener(MOServerLookupListener listener, ManagedObject mo)
          Adds a managed object lookup listener for the supplied managed object to this managed object server.
protected  void checkForUpdate(UpdatableManagedObject mo, MOQuery query)
          Checks updateStrategy whether the queried managed object needs to be updated.
protected  void fireContextChanged(ContextEvent event)
           
protected  void fireLookupEvent(ManagedObject mo, MOQuery query)
           
protected  void fireQueryEvent(ManagedObject mo, MOQuery query)
           
 org.snmp4j.smi.OctetString[] getContexts()
          Returns the contexts known by the server.
 ManagedObject getManagedObject(org.snmp4j.smi.OID key, org.snmp4j.smi.OctetString context)
          Returns the ManagedObject with the specified OID and registered in the supplied context.
 org.snmp4j.smi.OctetString[] getRegisteredContexts(ManagedObject managedObject)
          Returns the contexts for which the supplied ManagedObject has been registered.
 java.util.SortedMap<MOScope,ManagedObject> getRegistry()
           
 UpdateStrategy getUpdateStrategy()
          Gets the update strategy for UpdatableManagedObjects.
static org.snmp4j.smi.Variable getValue(MOServer server, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID key)
          Returns the value of a particular MIB object instance using the ManagedObjectValueAccess interface.
 boolean isContextSupported(org.snmp4j.smi.OctetString context)
          Checks whether the supplied context is supported (registered) by this server.
 java.util.Iterator<java.util.Map.Entry<MOScope,ManagedObject>> iterator()
          Returns a read-only Iterator over the content of this server.
 boolean lock(java.lang.Object owner, ManagedObject managedObject)
          Locks a ManagedObject by the supplied owner.
 boolean lock(java.lang.Object owner, ManagedObject managedObject, long timeoutMillis)
          Locks a ManagedObject by the supplied owner.
 ManagedObject lookup(MOQuery query)
          Lookups the first (lexicographically ordered) managed object that matches the supplied query.
 void register(ManagedObject mo, org.snmp4j.smi.OctetString context)
          Registers a managed object for the specified context.
static void registerTableRowListener(MOServer server, MOTableRowListener listener)
          Register a single MOTableRowListener with all tables in the specified MOServer.
 void removeContext(org.snmp4j.smi.OctetString context)
          Removes a context from the server.
 void removeContextListener(ContextListener l)
          Removes a previously added context listener.
 boolean removeLookupListener(MOServerLookupListener listener, ManagedObject mo)
          Removes a managed object lookup listener for the specified managed object.
 void setUpdateStrategy(UpdateStrategy updateStrategy)
          Sets the update strategy for UpdatableManagedObjects.
static boolean setValue(MOServer server, org.snmp4j.smi.OctetString context, org.snmp4j.smi.VariableBinding newValueAndKey)
          Sets the value of a particular MIB object instance using the ManagedObjectValueAccess interface.
 java.lang.String toString()
           
 void unlock(java.lang.Object owner, ManagedObject managedObject)
          Unlocks a ManagedObject that has been locked by the specified owner.
 void unregister(ManagedObject mo, org.snmp4j.smi.OctetString context)
          Removes the registration of the supplied managed object for the specified context.
static void unregisterTableRowListener(MOServer server, MOTableRowListener listener)
          Unregister a single MOTableRowListener with all tables in the specified MOServer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultMOServer

public DefaultMOServer()
Method Detail

lookup

public ManagedObject lookup(MOQuery query)
Description copied from interface: MOServer
Lookups the first (lexicographically ordered) managed object that matches the supplied query.

Specified by:
lookup in interface MOServer
Parameters:
query - a MOQuery instance.
Returns:
the ManagedObject that matches the query and null if no such object exists.

checkForUpdate

protected void checkForUpdate(UpdatableManagedObject mo,
                              MOQuery query)
Checks updateStrategy whether the queried managed object needs to be updated. This method is called on behalf of lookup(MOQuery query) after fireQueryEvent(org.snmp4j.agent.ManagedObject, org.snmp4j.agent.MOQuery) and before fireLookupEvent(org.snmp4j.agent.ManagedObject, org.snmp4j.agent.MOQuery) is being called.

Parameters:
mo - an UpdatableManagedObject instance.
query - the query that is interested in content of mo.
Since:
1.2

getManagedObject

public ManagedObject getManagedObject(org.snmp4j.smi.OID key,
                                      org.snmp4j.smi.OctetString context)
Returns the ManagedObject with the specified OID and registered in the supplied context.

Note: The query used to lookup the managed object will indicate an intended read-only access for the MOServerLookupEvents fired on behalf of this method.

Parameters:
key - the OID identifying the key (lower bound) of the ManagedObject.
context - the optional context to look in. A null value searches in all contexts.
Returns:
the ManagedObject instance or null if such an instance does not exists.
Since:
1.1

getValue

public static org.snmp4j.smi.Variable getValue(MOServer server,
                                               org.snmp4j.smi.OctetString context,
                                               org.snmp4j.smi.OID key)
Returns the value of a particular MIB object instance using the ManagedObjectValueAccess interface. If a ManagedObject does not support this interface, its value cannot be returned and null will be returned instead.

Parameters:
server - the MOServer where to lookup the value.
context - the optional context to look in. A null value searches in all contexts.
key - the OID identifying the variable instance to return.
Returns:
the Variable associated with OID and context in server or null if no such variable exists.
Since:
1.4

setValue

public static boolean setValue(MOServer server,
                               org.snmp4j.smi.OctetString context,
                               org.snmp4j.smi.VariableBinding newValueAndKey)
Sets the value of a particular MIB object instance using the ManagedObjectValueAccess interface. If a ManagedObject does not support this interface, its value cannot be set and false will be returned.

Parameters:
server - the MOServer where to lookup the value.
context - the optional context to look in. A null value searches in all contexts.
newValueAndKey - the OID identifying the variable instance to set and its new value.
Returns:
the true if the value has been set successfully, false otherwise.
Since:
1.4

fireLookupEvent

protected void fireLookupEvent(ManagedObject mo,
                               MOQuery query)

fireQueryEvent

protected void fireQueryEvent(ManagedObject mo,
                              MOQuery query)

getContexts

public org.snmp4j.smi.OctetString[] getContexts()
Description copied from interface: MOServer
Returns the contexts known by the server.

Specified by:
getContexts in interface MOServer
Returns:
an array of context names.

isContextSupported

public boolean isContextSupported(org.snmp4j.smi.OctetString context)
Description copied from interface: MOServer
Checks whether the supplied context is supported (registered) by this server.

Specified by:
isContextSupported in interface MOServer
Parameters:
context - a context name.
Returns:
true if the context is support (thus has previously added by MOServer.addContext(org.snmp4j.smi.OctetString)) and false otherwise.

getRegistry

public java.util.SortedMap<MOScope,ManagedObject> getRegistry()

getUpdateStrategy

public UpdateStrategy getUpdateStrategy()
Gets the update strategy for UpdatableManagedObjects. If the strategy is null no updates will be performed on behalf of calls to lookup(org.snmp4j.agent.MOQuery).

Returns:
the current UpdateStrategy instance or null if no strategy is active.
Since:
1.2
See Also:
lookup(org.snmp4j.agent.MOQuery)

setUpdateStrategy

public void setUpdateStrategy(UpdateStrategy updateStrategy)
Sets the update strategy for UpdatableManagedObjects. If the strategy is null no updates will be performed on behalf of calls to lookup(MOQuery).

Parameters:
updateStrategy - the new UpdateStrategy instance or null if no updates should be performed.
Since:
1.2
See Also:
lookup(MOQuery)

register

public void register(ManagedObject mo,
                     org.snmp4j.smi.OctetString context)
              throws DuplicateRegistrationException
Description copied from interface: MOServer
Registers a managed object for the specified context. A managed object can be registered for more than one context.

Specified by:
register in interface MOServer
Parameters:
mo - a ManagedObject instance.
context - the context name for which to register the mo or null if the managed oject is to be registered for all contexts (including the default context).
Throws:
DuplicateRegistrationException - if the registration conflicts (i.e. overlaps) with an already existing registration.

unregister

public void unregister(ManagedObject mo,
                       org.snmp4j.smi.OctetString context)
Description copied from interface: MOServer
Removes the registration of the supplied managed object for the specified context.

Specified by:
unregister in interface MOServer
Parameters:
mo - a ManagedObject instance.
context - the context name for which to unregister the mo or null if the managed oject is to be unregistered for all contexts (including the default context). In the latter case however, explicit registrations for a particular context will not be removed!

addContext

public void addContext(org.snmp4j.smi.OctetString context)
Description copied from interface: MOServer
Adds the supplied context to the server. The server however may silently ignore the request if local constraints do not allow to add the context (although this should be an exception case only).

Specified by:
addContext in interface MOServer
Parameters:
context - an OctetString representing the context name to add.

removeContext

public void removeContext(org.snmp4j.smi.OctetString context)
Description copied from interface: MOServer
Removes a context from the server. Removing a context does not remove any managed objects from the server's registry.

Specified by:
removeContext in interface MOServer
Parameters:
context - n OctetString representing the context name to remove.

lock

public boolean lock(java.lang.Object owner,
                    ManagedObject managedObject)
Description copied from interface: MOServer
Locks a ManagedObject by the supplied owner. Once a ManagedObject is locked, a lookup attempt will block until it is unlocked or a predefined timeout occurs.

Specified by:
lock in interface MOServer
Parameters:
owner - an Object.
managedObject - the ManagedObject to lock.
Returns:
true if the lock could be acquired, false otherwise, i.e. if an InterruptedException has occurred.

lock

public boolean lock(java.lang.Object owner,
                    ManagedObject managedObject,
                    long timeoutMillis)
Description copied from interface: MOServer
Locks a ManagedObject by the supplied owner. Once a ManagedObject is locked, a lookup attempt will block until it is unlocked or a predefined timeout occurs.

Specified by:
lock in interface MOServer
Parameters:
owner - an Object.
managedObject - the ManagedObject to lock.
timeoutMillis - the number of 1/1000 seconds to wait for the lock.
Returns:
true if the lock could be acquired, false otherwise, i.e. if an InterruptedException or timeout has occurred.

unlock

public void unlock(java.lang.Object owner,
                   ManagedObject managedObject)
Description copied from interface: MOServer
Unlocks a ManagedObject that has been locked by the specified owner. If the ManagedObject is currently locked by another owner this method returns silently.

Note: In debug log mode a message is locked if the lock owner does not match the current lock owner.

Specified by:
unlock in interface MOServer
Parameters:
owner - an Object.
managedObject - the ManagedObject to unlock.

iterator

public java.util.Iterator<java.util.Map.Entry<MOScope,ManagedObject>> iterator()
Description copied from interface: MOServer
Returns a read-only Iterator over the content of this server. The iterator is thread safe and can be used while the server is being modified. The remove operation of the iterator is not supported.

Specified by:
iterator in interface MOServer
Returns:
the Iterator on the Map.Entry instances managed by this server. Each Entry consists of an MOScope key instance and a corresponding ManagedObject value instance. If the ManagedObject has been registered for a specific context, then a MOContextScope is returned as key, otherwise the managed objects own MOScope is returned.

addLookupListener

public void addLookupListener(MOServerLookupListener listener,
                              ManagedObject mo)
Description copied from interface: MOServer
Adds a managed object lookup listener for the supplied managed object to this managed object server. A MOServerLookupListener is called before the managed object is returned by MOServer.lookup(MOQuery query).

Specified by:
addLookupListener in interface MOServer
Parameters:
listener - a MOServerLookupListener instance, for example a managed object that needs to update its state whenever it has been looked up
mo - the ManagedObject that triggers the MOServerLookupEvent to be fired when it has been looked up.

removeLookupListener

public boolean removeLookupListener(MOServerLookupListener listener,
                                    ManagedObject mo)
Description copied from interface: MOServer
Removes a managed object lookup listener for the specified managed object.

Specified by:
removeLookupListener in interface MOServer
Parameters:
listener - a MOServerLookupListener instance.
mo - the ManagedObject that triggered the MOServerLookupEvent to be fired when it has been looked up.
Returns:
true if the listener could be removed or false if such a listener is not registered.

addContextListener

public void addContextListener(ContextListener l)
Description copied from interface: MOServer
Adds a context listener to the server. The listener will be informed about context insertion and removal.

Specified by:
addContextListener in interface MOServer
Parameters:
l - a ContextListener instance to be informed about context changes.

removeContextListener

public void removeContextListener(ContextListener l)
Description copied from interface: MOServer
Removes a previously added context listener.

Specified by:
removeContextListener in interface MOServer
Parameters:
l - a ContextListener instance.

fireContextChanged

protected void fireContextChanged(ContextEvent event)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getRegisteredContexts

public org.snmp4j.smi.OctetString[] getRegisteredContexts(ManagedObject managedObject)
Description copied from interface: MOServer
Returns the contexts for which the supplied ManagedObject has been registered.

Specified by:
getRegisteredContexts in interface MOServer
Parameters:
managedObject - a ManagedObject instance.
Returns:
an array of context strings, for which managedObject has been registered. If the managedObject has bee registered for all contexts, a null element is included in the array.

registerTableRowListener

public static void registerTableRowListener(MOServer server,
                                            MOTableRowListener listener)
Register a single MOTableRowListener with all tables in the specified MOServer. This overall registration can be used, for example, to apply table size limits to all tables in an agent. See MOTableSizeLimit for details.

Note: The server must not change its registration content while this method is being called, otherwise a ConcurrentModificationException might be thrown.

Parameters:
server - a MOServer instance.
listener - the MOTableRowListener instance to register.
Since:
1.4

unregisterTableRowListener

public static void unregisterTableRowListener(MOServer server,
                                              MOTableRowListener listener)
Unregister a single MOTableRowListener with all tables in the specified MOServer. This overall unregistration can be used, for example, to remove table size limits from all tables in an agent. See MOTableSizeLimit for details.

Note: The server must not change its registration content while this method is being called, otherwise a ConcurrentModificationException might be thrown.

Parameters:
server - a MOServer instance.
listener - the MOTableRowListener instance to unregister.
Since:
1.4

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.