Class StoreListeners
- Object
-
- WarningListeners
-
- StoreListeners
-
- All Implemented Interfaces:
Localized
public class StoreListeners extends WarningListeners implements Localized
Holds a list ofStoreListenerinstances and provides convenience methods for sending events. This is a helper class forDataStoreandResourceimplementations.Observers can add listeners for being notified about events, and producers can invoke one of the
warning(…)and other methods for emitting events.Warning eventsAll warnings are given to the listeners asLogRecordinstances (this allows localizable messages and additional information like stack trace, timestamp, etc.). ThisStoreListenersclass provides convenience methods likewarning(String, Exception), which buildLogRecordfrom an exception or from a string. But all thosewarning(…)methods ultimately delegate towarning(LogRecord), thus providing a single point that subclasses can override. When a warning is emitted, the default behavior is:- Notify all listeners registered for
WarningEventtype in thisStoreListenersand in the parent managers. - If previous step found no listener registered for
WarningEvent, then log the warning in the first logger found in following choices:- The logger specified by
LogRecord.getLoggerName()if non-null. - Otherwise the logger specified by
DataStoreProvider.getLogger()if the provider can be found. - Otherwise a logger whose name is the source
DataStorepackage name.
- The logger specified by
Thread safetyThe sameStoreListenersinstance 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
Defined in the
sis-storagemodule
-
-
Constructor Summary
Constructors Constructor Description StoreListeners(StoreListeners parent, Resource source)Creates a new instance with the given parent and initially no listener.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T extends StoreEvent>
voidaddListener(Class<T> eventType, StoreListener<? super T> listener)Registers a listener to notify when the specified kind of event occurs.voidaddWarningListener(WarningListener listener)Deprecated.Replaced byaddListener(listener, WarningEvent.class).<T extends StoreEvent>
booleanfire(T event, Class<T> eventType)Sends the given event to all listeners registered for the given type or for a super-type.LocalegetLocale()Returns the locale used by this manager, ornullif unspecified.ResourcegetSource()Returns the source of events.StringgetSourceName()Returns a short name or label for the source.booleanhasListeners()Deprecated.booleanhasListeners(Class<? extends StoreEvent> eventType)Returnstrueif this object or its parent contains at least one listener for the given type of event.<T extends StoreEvent>
voidremoveListener(Class<T> eventType, StoreListener<? super T> listener)Unregisters a listener previously added for the given type of events.voidremoveWarningListener(WarningListener listener)Deprecated.Replaced byremoveListener(listener, WarningEvent.class).voidwarning(Exception exception)Reports a warning described by the given exception.voidwarning(String message)Reports a warning described by the given message.voidwarning(String message, Exception exception)Reports a warning described by the given message and exception.voidwarning(Level level, String message, Exception exception)Reports a warning at the given level represented by the given message and exception.voidwarning(LogRecord description)Reports a warning described by the given log record.-
Methods inherited from class WarningListeners
getListeners, getLogger
-
-
-
-
Constructor Detail
-
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 theDataStorethat created a resource.- Parameters:
parent- the manager to notify in addition to this manager, ornullif none.source- the source of events. Can not be null.
-
-
Method Detail
-
getSource
public Resource getSource()
Returns the source of events. This value is specified at construction time.- Overrides:
getSourcein classWarningListeners- Returns:
- the source of events.
-
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 that name from the data store, or returns an arbitrary name if it can get it otherwise.
- Returns:
- a short name of label for the source (never
null). - See Also:
DataStore.getDisplayName()
-
getLocale
public Locale getLocale()
Returns the locale used by this manager, ornullif unspecified. That locale is typically inherited from theDataStorelocale and can be used for formatting messages.- Specified by:
getLocalein interfaceLocalized- Overrides:
getLocalein classWarningListeners- Returns:
- the locale for messages (typically specified by the data store), or
nullif unknown. - See Also:
DataStore.getLocale(),StoreEvent.getLocale()
-
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 ofmessageandexceptionarguments 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).- Overrides:
warningin classWarningListeners- Parameters:
message- the warning message to report, ornullif none.exception- the exception to report, ornullif 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 ofmessageandexceptionarguments shall be non-null. If both are non-null, then the exception message will be concatenated after the given message.Stack trace omissionIf there is no registered listener for theWarningEventtype, then thewarning(LogRecord)method will send the record 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 ofStoreListeners(on the assumption that the logging message provides sufficient information). If the stack trace is desired, then users can either:- invoke
warning(LogRecord)directly, or - override
warning(LogRecord)and invokeLogRecord.setThrown(Throwable)explicitly, or - register a listener which will log the record itself.
- Overrides:
warningin classWarningListeners- Parameters:
level- the warning level.message- the message to log, ornullif none.exception- the exception to log, ornullif none.
- invoke
-
warning
public void warning(LogRecord description)
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:StoreListener.eventOccured(new WarningEvent(source, record))on all listeners registered for this kind of event.- Only if above step found no listener, then
Logging.getLogger(record.loggerName).log(record)whereloggerNameis one of the following:record.getLoggerName()if that value is non-null.- Otherwise the value of
DataStoreProvider.getLogger()if the provider is found. - Otherwise the source
DataStorepackage name.
- Overrides:
warningin classWarningListeners- Parameters:
description- warning details provided as a log record.
-
fire
public <T extends StoreEvent> boolean fire(T event, Class<T> eventType)
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 thisStoreListeners, then notifies listeners registered in parentStoreListenerss. Each listener will be notified only once even if it has been registered many times.- Type Parameters:
T- compile-time value of theeventTypeargument.- Parameters:
event- the event to fire.eventType- the type of events to be fired.- Returns:
trueif the event has been sent to at least one listener.
-
addListener
public <T extends StoreEvent> void addListener(Class<T> eventType, StoreListener<? super T> listener)
Registers a listener to notify when the specified kind of event occurs. Registering a listener for a giveneventTypealso register the listener for all event sub-types. The same listener can be registered many times, but itsStoreListener.eventOccured(StoreEvent)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 eventsIfeventTypeis assignable fromWarningEvent.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:
T- compile-time value of theeventTypeargument.- Parameters:
eventType- type ofStoreEventto listen (can not benull).listener- listener to notify about events.- See Also:
Resource.addListener(Class, StoreListener)
-
removeListener
public <T extends StoreEvent> void removeListener(Class<T> eventType, StoreListener<? super T> listener)
Unregisters a listener previously added for the given type of events. TheeventTypemust be the exact same class than the one given to theaddListener(…)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
addListener(type, ls)has been invoked twice, thenremoveListener(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 eventsIfeventTypeisWarningEvent.classand if, after this method invocation, there is no remaining listener for warning events, then thisStoreListenerswill send future warnings to the loggers.- Type Parameters:
T- compile-time value of theeventTypeargument.- Parameters:
eventType- type ofStoreEventwhich were listened (can not benull).listener- listener to stop notifying about events.- See Also:
Resource.removeListener(Class, StoreListener)
-
hasListeners
public boolean hasListeners(Class<? extends StoreEvent> eventType)
Returnstrueif this object or its parent contains at least one listener for the given type of event.- Parameters:
eventType- the type of event for which to check listener presence.- Returns:
trueif this object contains at least one listener for given event type,falseotherwise.
-
hasListeners
@Deprecated public boolean hasListeners()
Deprecated.Returnstrueif this object contains at least one listener.- Overrides:
hasListenersin classWarningListeners- Returns:
trueif this object contains at least one listener,falseotherwise.
-
addWarningListener
@Deprecated public void addWarningListener(WarningListener listener)
Deprecated.Replaced byaddListener(listener, WarningEvent.class).Description copied from class:WarningListenersAdds a listener to be notified when a warning occurred. When a warning occurs, there is a choice:- If this object has no warning listener, then the warning is logged at
Level.WARNING. - If this object has at least one warning listener, then all listeners are notified and the warning is not logged by this object.
- Overrides:
addWarningListenerin classWarningListeners- Parameters:
listener- the listener to add.
- If this object has no warning listener, then the warning is logged at
-
removeWarningListener
@Deprecated public void removeWarningListener(WarningListener listener)
Deprecated.Replaced byremoveListener(listener, WarningEvent.class).Description copied from class:WarningListenersRemoves a previously registered listener.- Overrides:
removeWarningListenerin classWarningListeners- Parameters:
listener- the listener to remove.
-
-