org.apache.sis.util.logging.WarningListeners类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中org.apache.sis.util.logging.WarningListeners类的一些代码示例,展示了WarningListeners类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WarningListeners类的具体详情如下:
包路径:org.apache.sis.util.logging.WarningListeners
类名称:WarningListeners

WarningListeners介绍

[英]Holds a list of WarningListener instances and provides convenience methods for emitting warnings. This is a helper class for org.apache.sis.storage.DataStore implementations or for other services susceptible to emit warnings. Observers can #addWarningListener for being notified about warnings, and processes can invoke one of the warning(…) methods for emitting warnings. All warnings are given to the listeners as LogRecord instances (this allows localizable messages and additional information like LogRecord#getMillis() and LogRecord#getThrown()). This WarningListeners class provides convenience methods like #warning(String,Exception), which builds LogRecord from an exception or from a string, but all those warning(…) methods ultimately delegate to #warning(LogRecord), thus providing a single point that subclasses can override. When a warning is emitted, the default behavior is:

  • If at least one WarningListener is registered, then all listeners are notified and the warning is not logged.
  • Otherwise if the value returned by LogRecord#getLoggerName() is non-null, then the warning will be logged to that named logger.
  • Otherwise the warning is logged to the logger returned by #getLogger().
    Thread safety The same WarningListeners 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.
    [中]保存WarningListener实例的列表,并为发出警告提供方便的方法。这是org的助手类。阿帕奇。姐妹。存储数据存储实现或其他容易发出警告的服务。观察者可以#添加WarningListener以获得有关警告的通知,进程可以调用其中一个警告(…)方法来发出警告。所有警告都作为LogRecord实例提供给侦听器(这允许本地化消息和其他信息,如LogRecord#getMillis()和LogRecord#getThrown())。这个WarningListeners类提供了方便的方法,比如#warning(String,Exception),它从异常或字符串构建日志记录,但所有这些warning(…)方法最终都委托给#warning(LogRecord),从而提供了子类可以覆盖的单一点。发出警告时,默认行为为:
    *如果至少注册了一个WarningListener,则会通知所有侦听器,并且不会记录警告。
    *否则,如果LogRecord#getLoggerName()返回的值非空,则警告将记录到指定的记录器。
    *否则,该警告将记录到由#getLogger()返回的记录器中。
    线程安全同一WarningListeners实例可以被多个线程安全使用,而无需调用方进行同步。子类应该确保任何被重写的方法都可以安全地从多个线程调用。

代码示例

代码示例来源:origin: apache/sis

/**
 * Reports a warning represented by the given message and exception.
 * At least one of {@code message} and {@code exception} shall be non-null.
 *
 * @param message    the message to log, or {@code null} if none.
 * @param exception  the exception to log, or {@code null} if none.
 */
final void warning(final String message, final Exception exception) {
  listeners.warning(message, exception);
}

代码示例来源:origin: apache/sis

/**
 * Logs a warning using the localized error resource bundle for the locale given by
 * {@link WarningListeners#getLocale()}.
 *
 * @param  key  one of {@link Errors.Keys} values.
 */
private void warning(final short key, final Object p1, final Object p2, final Exception e) {
  final WarningListeners<DataStore> listeners = decoder.listeners;
  listeners.warning(Errors.getResources(listeners.getLocale()).getString(key, p1, p2), e);
}

代码示例来源:origin: org.apache.sis.storage/sis-storage

/**
 * Creates a new instance with no provider and initially no listener.
 */
protected DataStore() {
  provider  = null;
  name      = null;
  locale    = Locale.getDefault(Locale.Category.DISPLAY);
  listeners = new WarningListeners<>(this);
}

代码示例来源:origin: org.apache.sis.storage/sis-storage

/**
   * Reports a warning in the {@code "org.apache.sis.io.wkt"} logger. This method pretends that the
   * warning come from {@code getMetadata()} method, which is the public facade for the parsing method.
   *
   * @param  record  the warning to report.
   */
  private void log(final LogRecord record) {
    record.setSourceClassName(listeners.getSource().getClass().getName());
    record.setSourceMethodName("getMetadata");
    record.setLoggerName(Loggers.WKT);
    listeners.warning(record);
  }
}

代码示例来源:origin: apache/sis

/**
   * Tests {@link WarningListeners#warning(String, Exception)} with a registered listener.
   */
  @Test
  @DependsOnMethod("testAddAndRemoveWarningListener")
  public void testWarning() {
    listeners.addWarningListener(this);
    listeners.warning("The message", null);
    listeners.removeWarningListener(this);
    assertNotNull("Listener has not been notified.", warning);
    assertEquals(getClass().getName(), warning.getSourceClassName());
    assertEquals("testWarning", warning.getSourceMethodName());
    assertEquals("The message", warning.getMessage());
  }
}

代码示例来源:origin: org.apache.sis.core/sis-utility

if (exception != null) {
  trace = exception.getStackTrace();
  message = Exceptions.formatChainedMessages(getLocale(), message, exception);
  if (message == null) {
    message = exception.toString();
  if (isPublic(e)) {
    record.setSourceClassName(e.getClassName());
    record.setSourceMethodName(e.getMethodName());
warning(record);

代码示例来源:origin: org.apache.sis.storage/sis-storage

/**
 * Returns the locale for error messages or warnings.
 * Returns {@code null} if no locale is explicitly defined.
 *
 * @return the locale, or {@code null} if not explicitly defined.
 */
@Override
public final Locale getLocale() {
  return (listeners != null) ? listeners.getLocale() : null;
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link WarningListeners#addWarningListener(WarningListener)} followed by
 * {@link WarningListeners#removeWarningListener(WarningListener)}
 */
@Test
public void testAddAndRemoveWarningListener() {
  listeners.addWarningListener(this);
  try {
    listeners.addWarningListener(this);
  } catch (IllegalArgumentException e) {
    // This is the expected exception.
    assertTrue(e.getMessage().contains("TestListener"));
  }
  listeners.removeWarningListener(this);
  try {
    listeners.removeWarningListener(this);
  } catch (NoSuchElementException e) {
    // This is the expected exception.
    assertTrue(e.getMessage().contains("TestListener"));
  }
}

代码示例来源:origin: org.apache.sis.storage/sis-storage

/**
 * Removes a previously registered listener.
 *
 * @param  listener  the listener to remove.
 * @throws NoSuchElementException if the given listener is not registered in this data store.
 */
public void removeWarningListener(final WarningListener<? super DataStore> listener)
    throws NoSuchElementException
{
  listeners.removeWarningListener(listener);
}

代码示例来源:origin: org.apache.sis.core/sis-metadata

/**
 * Adds a listener to be notified when a warning occurred while reading from or writing metadata.
 * When a warning occurs, there is a choice:
 *
 * <ul>
 *   <li>If this metadata source has no warning listener, then the warning is logged at {@link Level#WARNING}.</li>
 *   <li>If this metadata source has at least one warning listener, then all listeners are notified
 *       and the warning is <strong>not</strong> logged by this metadata source instance.</li>
 * </ul>
 *
 * Consider invoking this method in a {@code try} … {@code finally} block if the {@code MetadataSource}
 * lifetime is longer than the listener lifetime, as below:
 *
 * {@preformat java
 *     source.addWarningListener(listener);
 *     try {
 *         // Do some work...
 *     } finally {
 *         source.removeWarningListener(listener);
 *     }
 * }
 *
 * @param  listener  the listener to add.
 * @throws IllegalArgumentException if the given listener is already registered in this metadata source.
 */
public void addWarningListener(final WarningListener<? super MetadataSource> listener)
    throws IllegalArgumentException
{
  listeners.addWarningListener(listener);
}

代码示例来源:origin: apache/sis

/**
   * Reports a warning in the {@code "org.apache.sis.io.wkt"} logger. This method pretends that the
   * warning come from {@code getMetadata()} method, which is the public facade for the parsing method.
   *
   * @param  record  the warning to report.
   */
  private void log(final LogRecord record) {
    record.setSourceClassName(listeners.getSource().getClass().getName());
    record.setSourceMethodName("getMetadata");
    record.setLoggerName(Loggers.WKT);
    listeners.warning(record);
  }
}

代码示例来源:origin: apache/sis

if (exception != null) {
  trace = exception.getStackTrace();
  message = Exceptions.formatChainedMessages(getLocale(), message, exception);
  if (message == null) {
    message = exception.toString();
  if (isPublic(e)) {
    record.setSourceClassName(e.getClassName());
    record.setSourceMethodName(e.getMethodName());
warning(record);

代码示例来源:origin: apache/sis

/**
 * Returns the locale to use for warnings and error messages.
 */
final Locale getLocale() {
  return listeners.getLocale();
}

代码示例来源:origin: org.apache.sis.core/sis-metadata

/**
 * Removes a previously registered listener.
 *
 * @param  listener  the listener to remove.
 * @throws NoSuchElementException if the given listener is not registered in this metadata source.
 */
public void removeWarningListener(final WarningListener<? super MetadataSource> listener)
    throws NoSuchElementException
{
  listeners.removeWarningListener(listener);
}

代码示例来源:origin: org.apache.sis.storage/sis-storage

throws IllegalArgumentException
listeners.addWarningListener(listener);

代码示例来源:origin: apache/sis

/**
 * Invoked by the UCAR netCDF library when an error occurred.
 *
 * @param  message  the error message.
 */
@Override
public void setError(final String message) {
  listeners.warning(message, null);
}

代码示例来源:origin: org.apache.sis.storage/sis-storage

/**
 * Reports a warning for a WKT that can not be read. This method should be invoked only when the CRS
 * can not be created at all; it should not be invoked if the CRS has been created with some warnings.
 */
final void log(final Exception e) {
  final DataStore store = listeners.getSource();
  listeners.warning(Resources.forLocale(store.getLocale())
      .getString(Resources.Keys.CanNotReadCRS_WKT_1, store.getDisplayName()), e);
}

代码示例来源:origin: apache/sis

/**
 * Reports a warning to the specified listeners.
 *
 * @param  listeners  the listeners where to report the warning.
 * @param  caller     the caller class to report, preferably a public class.
 * @param  method     the caller method to report, preferable a public method.
 * @param  exception  the exception that occurred, or {@code null} if none.
 * @param  resources  the resources bundle for {@code key} and {@code arguments}, or {@code null} for {@link Resources}.
 * @param  key        one or {@link Resources.Keys} constants.
 * @param  arguments  values to be formatted in the {@link java.text.MessageFormat} pattern.
 */
static void warning(final WarningListeners<?> listeners, final Class<?> caller, final String method,
    final Exception exception, IndexedResourceBundle resources, final short key, final Object... arguments)
{
  if (resources == null) {
    resources = Resources.forLocale(listeners.getLocale());
  }
  final LogRecord record = resources.getLogRecord(Level.WARNING, key, arguments);
  record.setLoggerName(Modules.NETCDF);
  record.setSourceClassName(caller.getCanonicalName());
  record.setSourceMethodName(method);
  if (exception != null) {
    // TODO: avoid reporting the full exception stack trace (maybe leverage QuietLogRecord).
    record.setThrown(exception);
  }
  listeners.warning(record);
}

代码示例来源:origin: apache/sis

/**
 * Returns the locale for error messages or warnings.
 * Returns {@code null} if no locale is explicitly defined.
 *
 * @return the locale, or {@code null} if not explicitly defined.
 */
@Override
public final Locale getLocale() {
  return (listeners != null) ? listeners.getLocale() : null;
}

代码示例来源:origin: apache/sis

/**
 * Creates a new instance with no provider and initially no listener.
 */
protected DataStore() {
  provider  = null;
  name      = null;
  locale    = Locale.getDefault(Locale.Category.DISPLAY);
  listeners = new WarningListeners<>(this);
}

相关文章