Class StoreListeners

Object
StoreListeners
All Implemented Interfaces:
Localized

public class StoreListeners extends Object implements Localized
Holds a list of Store­Listener instances and provides convenience methods for sending events. This is a helper class for Data­Store and Resource implementations.

Observers can add listeners for being notified about events, and producers can invoke one of the warning(…) and other methods for emitting events.

Warning events

All warnings are given to the listeners as Log­Record instances (this allows localizable messages and additional information like stack trace, timestamp, etc.). This Store­Listeners class provides convenience methods like warning(String, Exception), which build Log­Record from an exception or from a string. But all those warning(…) methods ultimately delegate to warning(Log­Record, Filter), thus providing a single point that subclasses can override. When a warning is emitted, the default behavior is:
  • Notify all listeners that are registered for a given Warning­Event type in this Store­Listeners and in the parent resource or data store. Each listener will be notified only once, even if the same listener is registered in two or more places.
  • If previous step found no listener registered for Warning­Event, then log the warning in the first logger found in following choices:
    1. The logger specified by Log­Record​.get­Logger­Name() if non-null.
    2. Otherwise the logger specified by Data­Store­Provider​.get­Logger() if the provider can be found.
    3. Otherwise a logger whose name is the source Data­Store package name.

Thread safety

The same Store­Listeners instance can be safely used by many threads without synchronization on the part of the caller. Subclasses should make sure that any overridden methods remain safe to call from multiple threads.
Since:
1.0
  • Constructor Details

    • StoreListeners

      public StoreListeners(StoreListeners parent, Resource source)
      Creates a new instance with the given parent and initially no listener. The parent is typically the listeners of the Data­Store that created a resource. When an event is fired, listeners registered in the parent will be notified as well as listeners registered in this Store­Listeners. Each listener will be notified only once even if it has been registered in two places.

      Permitted even types

      If the parent restricts the usable event types to a subset of Store­Event subtypes, then this Store­Listeners inherits those restrictions. The list of usable types can be rectricted more but cannot be relaxed.
      Parameters:
      parent - the manager to notify in addition to this manager, or null if none.
      source - the source of events. Cannot be null.
  • Method Details

    • getParent

      public Optional<StoreListeners> getParent()
      Returns the parent set of listeners that are notified in addition to this set of listeners. This is the value of the parent argument given to the constructor.
      Returns:
      parent set of listeners that are notified in addition to this set of listeners.
      Since:
      1.3
    • getSource

      public Resource getSource()
      Returns the source of events. This value is specified at construction time.
      Returns:
      the source of events (never null).
    • getSourceName

      public String getSourceName()
      Returns a short name or label for the source. It may be the name of the file opened by a data store. The returned name can be useful in warning messages for identifying the problematic source.

      The default implementation fetches a name from the data store, or returns an arbitrary name if no better name is found.

      Returns:
      a short name of label for the source (never null).
      See Also:
    • getLocale

      public Locale getLocale()
      Returns the locale used by this manager, or null if unspecified. That locale is typically inherited from the Data­Store locale and can be used for formatting messages.
      Specified by:
      get­Locale in interface Localized
      Returns:
      the locale for messages (typically specified by the data store), or null if unknown.
      See Also:
    • getLogger

      public Logger getLogger()
      Returns the logger where to send warnings when no other destination is specified. This method tries to get the logger from Data­Store­Provider​.get­Logger(). If that logger cannot be found, then this method infers a logger name from the package name of the source data store. The returned logger is used when:
      Returns:
      the logger where to send the warnings when there is no other destination.
      Since:
      1.1
      See Also:
    • warning

      public void warning(String message)
      Reports a warning described by the given message.

      This method is a shortcut for warning(Level​.WARNING, message, null).

      Parameters:
      message - the warning message to report.
    • warning

      public void warning(Exception exception)
      Reports a warning described by the given exception. The exception stack trace will be omitted at logging time for avoiding to pollute console output (keeping in mind that this method should be invoked only for non-fatal warnings). See below for more explanation.

      This method is a shortcut for warning(Level​.WARNING, null, exception).

      Parameters:
      exception - the exception to report.
    • warning

      public void warning(String message, Exception exception)
      Reports a warning described by the given message and exception. At least one of message and exception arguments shall be non-null. If both are non-null, then the exception message will be concatenated after the given message. If the exception is non-null, its stack trace will be omitted at logging time for avoiding to pollute console output (keeping in mind that this method should be invoked only for non-fatal warnings). See below for more explanation.

      This method is a shortcut for warning(Level​.WARNING, message, exception).

      Parameters:
      message - the warning message to report, or null if none.
      exception - the exception to report, or null if none.
    • warning

      public void warning(Level level, String message, Exception exception)
      Reports a warning at the given level represented by the given message and exception. At least one of message and exception arguments shall be non-null. If both are non-null, then the exception message will be concatenated after the given message.

      Stack trace omission

      If there are no registered listeners for the Warning­Event type, then the Log­Record will be sent to a Logger but without the stack trace. This is done that way because stack traces consume lot of space in the logging files, while being considered implementation details in the context of Store­Listeners (on the assumption that the logging message provides sufficient information).
      Parameters:
      level - the warning level.
      message - the message to log, or null if none.
      exception - the exception to log, or null if none.
    • warning

      public void warning(LogRecord description)
      Reports a warning described by the given log record. Invoking this method is equivalent to invoking warning(Log­Record, Filter) with a null filter.
      Parameters:
      description - warning details provided as a log record.
    • warning

      public void warning(LogRecord description, Filter onUnhandled)
      Reports a warning described by the given log record. The default implementation forwards the given record to one of the following destinations, in preference order:
      1. Store­Listener​.event­Occured(new Warning­Event(source, record)) on all listeners registered for this kind of event.
      2. on­Unhandled​.is­Loggable(description) if above step found no listener and the on­Unhandled filter is non-null.
      3. Logger​.get­Logger(record​.logger­Name).log(record) if the filter in above step returned true (or if the filter is null). In that case, logger­Name is one of the following:
      Parameters:
      description - warning details provided as a log record.
      on­Unhandled - filter invoked if the record has not been handled by a Store­Listener, or null if none. This filter determines whether the record should be sent to the logger returned by get­Logger().
      Since:
      1.2
    • fire

      public <E extends StoreEvent> boolean fire(Class<E> eventType, E event)
      Sends the given event to all listeners registered for the given type or for a super-type. This method first notifies the listeners registered in this Store­Listeners, then notifies listeners registered in parent Store­Listenerss. Each listener will be notified only once even if it has been registered many times.

      If one or many Store­Listener​.event­Occured(Store­Event) implemetations throw a Runtime­Exception, those exceptions will be collected and reported in a single log record. Runtime exceptions in listeners do not cause this method to fail.

      Type Parameters:
      E - compile-time value of the event­Type argument.
      Parameters:
      event­Type - the type of the event to be fired.
      event - the event to fire.
      Returns:
      true if the event has been sent to at least one listener.
      Throws:
      Illegal­Argument­Exception - if the given event type is not one of the types of events that this Store­Listeners can fire.
      Since:
      1.3
      See Also:
    • addListener

      public <E extends StoreEvent> void addListener(Class<E> eventType, StoreListener<? super E> listener)
      Registers a listener to notify when the specified kind of event occurs. Registering a listener for a given event­Type also register the listener for all event sub-types. The same listener can be registered many times, but its Store­Listener​.event­Occured(Store­Event) method will be invoked only once per event. This filtering applies even if the listener is registered on different resources in the same tree, for example a parent and its children.

      Warning events

      If event­Type is assignable from Warning­Event.class, then registering that listener turns off logging of warning messages for this manager. This side-effect is applied on the assumption that the registered listener will handle warnings in its own way, for example by showing warnings in a widget.
      Type Parameters:
      E - compile-time value of the event­Type argument.
      Parameters:
      event­Type - type of Store­Event to listen (cannot be null).
      listener - listener to notify about events.
      See Also:
    • removeListener

      public <E extends StoreEvent> void removeListener(Class<E> eventType, StoreListener<? super E> listener)
      Unregisters a listener previously added for the given type of events. The event­Type must be the exact same class than the one given to the add­Listener(…) method; this method does not remove listeners registered for subclasses and does not remove listeners registered in parent manager.

      If the same listener has been registered many times for the same even type, then this method removes only the most recent registration. In other words if add­Listener(type, ls) has been invoked twice, then remove­Listener(type, ls) needs to be invoked twice in order to remove all instances of that listener. If the given listener is not found, then this method does nothing (no exception is thrown).

      Warning events

      If event­Type is Warning­Event.class and if, after this method invocation, there are no remaining listeners for warning events, then this Store­Listeners will send future warnings to the loggers.
      Type Parameters:
      E - compile-time value of the event­Type argument.
      Parameters:
      event­Type - type of Store­Event which were listened (cannot be null).
      listener - listener to stop notifying about events.
      See Also:
    • hasListener

      public <E extends StoreEvent> boolean hasListener(Class<E> eventType, StoreListener<? super E> listener)
      Returns true if the given listener is registered for the given type or a super-type. This method may unconditionally return false if the given type of event is never fired by this Store­Listeners, because calls to add­Listener(event­Type, …) are free to ignore the listeners for those types.
      Type Parameters:
      E - compile-time value of the event­Type argument.
      Parameters:
      event­Type - type of Store­Event to check (cannot be null).
      listener - listener to check for registration.
      Returns:
      true if this object contains the specified listener for given event type, false otherwise.
      Since:
      1.3
    • hasListeners

      public boolean hasListeners(Class<? extends StoreEvent> eventType)
      Returns true if at least one listener is registered for the given type or a super-type. This method may unconditionally return false if the given type of event is never fired by this Store­Listeners, because calls to add­Listener(event­Type, …) are free to ignore the listeners for those types.
      Parameters:
      event­Type - the type of event for which to check listener presence.
      Returns:
      true if this object contains at least one listener for given event type, false otherwise.
    • setUsableEventTypes

      public void setUsableEventTypes(Class<?>... permitted)
      Notifies this Store­Listeners that only events of the specified types will be fired. With this knowledge, Store­Listeners will not retain any reference to listeners that are not listening to events of those types or to events of a parent type. This restriction allows the garbage collector to dispose unnecessary listeners.

      The argument shall enumerate all permitted types, including sub-types (they are not automatically accepted). All types given in argument must be types that were accepted before the invocation of this method. In other words, this method can be invoked for reducing the set of permitted types but not for expanding it.

      Example

      an application may unconditionally register listeners for being notified about additions of new data. If a Data­Store implementation is read-only, then such listeners would never receive any notification. As a slight optimization, the Data­Store constructor can invoke this method for example as below:
      listeners.setUsableEventTypes(WarningEvent.class);
      
      With this configuration, calls to add­Listener(Data­Added­Event​.class, foo) will be ignored, thus avoiding this instance to retain a never-used reference to the foo listener.
      Parameters:
      permitted - type of events that are permitted. Permitted sub-types shall be explicitly enumerated as well.
      Throws:
      Illegal­Argument­Exception - if one of the given types was not permitted before invocation of this method.
      Since:
      1.2
      See Also:
    • useReadOnlyEvents

      public void useReadOnlyEvents()
      Notifies this Store­Listeners that it will fire only Warning­Events and Close­Event. This method is a shortcut for set­Usable­Event­Types(Warning­Event​.class, Close­Event​.class)}, provided because frequently used by read-only data store implementations.

      Declaring a root resource (typically a Data­Store) as read-only implies that all children (e.g. components of an aggregate) are also read-only.

      Since:
      1.3
      See Also:
    • close

      public void close()
      Sends a Close­Event to all listeners registered for that kind of event, then discards listeners in this instance (but not in parents). Because listeners are discarded, invoking this method many times on the same instance has no effect after the first invocation.

      If one or many Store­Listener​.event­Occured(Store­Event) implementations throw a Runtime­Exception, those exceptions will be collected and reported in a single log record. Runtime exceptions in listeners do not cause this method to fail.

      Since:
      1.3
      See Also:
    • toString

      public String toString()
      Returns a string representation for debugging purposes.
      Overrides:
      to­String in class Object
      Returns:
      a debug string.