Class DefaultMOServer

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

public class DefaultMOServer extends Object implements MOServer
The default MO server implementation uses a sorted map for the managed object registry.
Version:
3.1.0
Author:
Frank Fock
  • Constructor Details

    • DefaultMOServer

      public DefaultMOServer()
  • Method Details

    • lookup

      public ManagedObject lookup(MOQuery query)
      Description copied from interface: MOServer
      Lookup the first (lexicographically ordered) managed object that matches the supplied query. No locking will be performed, regardless of the set MOLockStrategy.
      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.
    • lookup

      public <MO extends ManagedObject<?>> MO lookup(MOQuery query, LockRequest lockRequest, MOServerLookupEvent lookupEvent, Class<MO> managedObjectType)
      Lookup the first (lexicographically ordered) managed object that matches the supplied query and implements the given ManagedObject class. Locking will be performed according to the set MOLockStrategy before the lookup listener is fired. CAUTION: To make sure that the acquired lock is released after the using of the managed object has been finished, the unlock(Object, ManagedObject) method must be called then.
      Specified by:
      lookup in interface MOServer
      Type Parameters:
      MO - the ManagedObject type to lookup.
      Parameters:
      query - a MOQuery instance.
      lockRequest - the LockRequest that holds the lock owner and the timeout for acquiring a lock and returns whether a lock has been acquired or not on behalf of this lookup operation.
      lookupEvent - provides additional information about the intended use and optionally a callback to be informed about the completion of the use, including a reference to its result.
      managedObjectType - the ManagedObject type filter. Only objects of this type will be looked up and returned.
      Returns:
      the ManagedObject that matches the query and null if no such object exists.
      Since:
      3.1
    • getLockStrategy

      public MOLockStrategy getLockStrategy()
      Return the locking strategy for this server. The strategy defines if a lock is acquired before a looked up ManagedObject is returned (and before the corresponding lookup event is being fired) or not.
      Returns:
      the managed object locking strategy instance used by this server. If null, no locking is performed at all (which is only recommended for static content servers).
      Since:
      2.4.0
    • setLockStrategy

      public void setLockStrategy(MOLockStrategy lockStrategy)
      Sets the lock strategy for this server. The strategy defines if a lock is acquired before a looked up ManagedObject is returned (and before the corresponding lookup event is being fired) or not. By default, only write access needs a lock.
      Parameters:
      lockStrategy - a MOLockStrategy instance or null to suppress any locking.
      Since:
      2.4.0
    • 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.MOServerLookupEvent) and before fireLookupEvent(org.snmp4j.agent.ManagedObject<?>, org.snmp4j.agent.MOServerLookupEvent) 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, boolean fireLookupEvents)
      Returns the ManagedObject with the specified OID as ID returned by RegisteredManagedObject.getID() or the lower bound (regardless whether the ManagedObject's scope includes it or not) when registered in the supplied context.

      Note: The query used to look up the managed object will indicate 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.
      fireLookupEvents - if true lookup and query events will be fired as if the managed objects has been looked up by lookup(MOQuery). In addition, if the looked up managed object is an UpdatableManagedObject it will be locked if the lock strategy of this server requires it. The lock is active until the look-up events have been fired completely. The lock operation waits without timeout for the lock to become available. Otherwise, no events will be fired at all.
      Returns:
      the ManagedObject instance or null if such an instance does not exist.
      Since:
      2.3
    • getManagedObject

      public ManagedObject<?> getManagedObject(org.snmp4j.smi.OID key, org.snmp4j.smi.OctetString context)
      Returns the ManagedObject with the specified OID as ID returned by RegisteredManagedObject.getID() or the lower bound (regardless whether the ManagedObject's scope includes it or not) when registered in the supplied context.

      Note: The query used to look up the managed object will indicate 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 exist.
      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. Note: This method does not perform any locking based on the MOLockStrategy.
      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. Note: This method does not perform any locking based on the MOLockStrategy.
      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, MOServerLookupEvent event)
    • fireQueryEvent

      protected void fireQueryEvent(ManagedObject<?> mo, MOServerLookupEvent event)
    • 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 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:
    • setUpdateStrategy

      public DefaultMOServer 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.
      Returns:
      this object for fluently connecting config settings.
      Since:
      1.2
      See Also:
    • 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 ManagedObject<?> 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 object 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!
      Returns:
      the removed ManagedObjectinstance or null if the removal failed.
    • 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(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(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 from another owner will block until it is unlocked or a predefined timeout occurs. The same owner can lock again recursively but has to unlock with the same number of calls to MOServer.unlock(Object, ManagedObject) to actually release the lock. To release a lock for an owner immediately without unlocking recursively, call MOServer.unlockNow(Object, ManagedObject).
      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. 0 or less disables the timeout and waits forever until the lock is released by the current owner.
      Returns:
      true if the lock could be acquired, false otherwise, i.e. if an InterruptedException or timeout has occurred.
    • resetLocks

      public void resetLocks()
      Resets the lock list and all active and waiting locks. This might cause threads / LockRequest (= transactions) waiting for locks to continue without still having locked the locks they already acquired. To cleanly reset the locks, first set the setLockStrategy(MOLockStrategy) to (managedObjectLookedUp, query) -> false and then wait for getActiveLockRequests() become zero.
    • waitForUnlockedState

      public boolean waitForUnlockedState(long waitTimeoutMillis)
      Wait until this server has not active and pending locks anymore.
      Specified by:
      waitForUnlockedState in interface MOServer
      Parameters:
      waitTimeoutMillis - the maximum time in milliseconds to wait for this server to become lock free.
      Returns:
      true if server has no active and waiting locks anymore, false otherwise.
      Since:
      3.7.0
    • getWaitingLockRequests

      public Map<LockRequest,Integer> getWaitingLockRequests()
      Returns a readonly map of waiting LockRequests and their locks or an empty set if isDeadlockPreventionEnabled() is false.
      Returns:
      the lock requests (i.e., transactions) that wait for a lock.
      Since:
      3.7.0
    • getActiveLockRequests

      public Map<LockRequest,Integer> getActiveLockRequests()
      Returns a readonly set of active LockRequests or an empty set if isDeadlockPreventionEnabled() is false.
      Returns:
      the lock requests (i.e., transactions) that hold at least one lock.
      Since:
      3.7.0
    • lock

      public boolean lock(Object owner, ManagedObject<?> managedObject, long timeoutMillis, LockRequest lockRequest)
    • unlockNow

      public boolean unlockNow(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. If the specified owner is identical with the current lock holder for the specified managed object, then the lock is removed regardless how many recursive lock.

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

      Specified by:
      unlockNow in interface MOServer
      Parameters:
      owner - an Object that is owner of a lock and whose locks should be removed completely now.
      managedObject - the ManagedObject to forcibly unlock. If managedObject is null then this call has no effect.
      Returns:
      true if the lock has been found and completely released successfully, false otherwise.
    • unlock

      public boolean unlock(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. If managedObject is null then this call has no effect.
      Returns:
      true if the lock has been found and released successfully, false otherwise.
    • unlock

      protected boolean unlock(Object owner, ManagedObject<?> managedObject, boolean forceUnlock)
    • iterator

      public Iterator<Map.Entry<MOScope,ManagedObject<?>>> iterator()
      Description copied from interface: MOServer
      Return 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.
    • iterator

      public Iterator<Map.Entry<MOScope,ManagedObject<?>>> iterator(Comparator<MOScope> comparator, MOFilter moFilter)
      Description copied from interface: MOServer
      Return an Iterator on the registered ManagedObjects of this server in order defined by the given comparator and filtered.
      Specified by:
      iterator in interface MOServer
      Parameters:
      comparator - the comparator that defines the order of the managed objects to be returned.
      moFilter - if not null, only those managed objects are returned by this iterator that pass the specified filter.
      Returns:
      the iterator a copy of the registry.
    • 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 String toString()
      Overrides:
      toString in class 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 been registered for all contexts, a null element is included in the array.
    • getRegisteredScopes

      public Map<org.snmp4j.smi.OctetString,MOScope> getRegisteredScopes(ManagedObject<?> managedObject)
      Description copied from interface: MOServer
      Returns the scopes MOScope or MOContextScope for which the supplied ManagedObject has been registered in a map indexed by context.
      Specified by:
      getRegisteredScopes in interface MOServer
      Parameters:
      managedObject - a ManagedObject instance.
      Returns:
      a possibly empty map of context to MOScope entries. A registration to the default ("one-registration-for-all-contexts") null context is returned with a null context key value.
    • registerTableRowListener

      public static <R extends MOTableRow, T extends MOTable<R, ? extends MOColumn, ? extends MOTableModel<R>>> void registerTableRowListener(MOServer server, MOTableRowListener<R> 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.

      Type Parameters:
      R - the MOTableRow type supported by the table row listener to register.
      T - the MOTable type supported by the table row listener to register.
      Parameters:
      server - a MOServer instance.
      listener - the MOTableRowListener instance to register.
      Since:
      1.4
    • unregisterTableRowListener

      public static <R extends MOTableRow, T extends MOTable<R, ? extends MOColumn, ? extends MOTableModel<R>>> void unregisterTableRowListener(MOServer server, MOTableRowListener<R> 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.

      Type Parameters:
      R - the MOTableRow type supported by the table row listener to register.
      T - the MOTable type supported by the table row listener to register.
      Parameters:
      server - a MOServer instance.
      listener - the MOTableRowListener instance to unregister.
      Since:
      1.4
    • registerChangeListener

      public static void registerChangeListener(MOServer server, MOChangeListener listener, MOFilter moFilter)
      Register a single MOChangeListener with all objects matching the given filter in the specified MOServer. This overall registration can be used, for example, to listen for object changes.

      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.
      moFilter - an optional filter to select objects which should register the provided listener.
      Since:
      3.0
    • unregisterChangeListener

      public static void unregisterChangeListener(MOServer server, MOChangeListener listener, MOFilter moFilter)
      Unregister a single MOChangeListener from all objects matching the given filter in the specified MOServer.

      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.
      moFilter - an optional filter to select objects which should no longer register the provided listener.
      Since:
      3.0
    • isDeadlockPreventionEnabled

      public boolean isDeadlockPreventionEnabled()
      Returns true if the lookup(MOQuery, LockRequest, MOServerLookupEvent, Class) method uses deadlock prevention using a super thread, if a deadlock situation is detected (i.e., waiting LockRequests equal active LockRequests. Using the super thread could reduce parallelism unnecessarily to one and detecting (possible) deadlock situations creates additional overhead. For most use cases, setting CommandProcessor.setLockNonNextRequestsSortedByVbOid(boolean) to true is sufficient to prevent deadlocks, if API calls to lookup(MOQuery, LockRequest, MOServerLookupEvent, Class) are not locking more than one ManagedObject at once for a single LockRequest.
      Returns:
      true if super-thread based deadlock prevention is enabled or false otherwise (default).
      Since:
      3.7.0
    • setDeadlockPreventionEnabled

      public DefaultMOServer setDeadlockPreventionEnabled(boolean deadlockPreventionEnabled)
      Enable or disable the super-thread based deadlock prevention for the lookup(MOQuery, LockRequest, MOServerLookupEvent, Class) method. If enabled and a deadlock situation is detected (i.e., the number of waiting LockRequests equals active LockRequests, a super-thread will be used to run the latest lookup while all other lookups are blocked until this one is unlocked (finished). Using the super thread could reduce parallelism unnecessarily to one and detecting (possible) deadlock situations creates additional overhead. For most use cases, setting CommandProcessor.setLockNonNextRequestsSortedByVbOid(boolean) to true is sufficient to prevent deadlocks, if API calls to lookup(MOQuery, LockRequest, MOServerLookupEvent, Class) are not locking more than one ManagedObject at once for a single LockRequest.
      Parameters:
      deadlockPreventionEnabled - true to enable super-thread based deadlock prevention. Deadlock prevention is disabled by default.
      Returns:
      this object instance for fluently connecting config settings.
      Since:
      3.7.0