ConfigurationErrorEvent.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.event;

  18. /**
  19.  * <p>
  20.  * An event class that is used for reporting errors that occurred while processing configuration properties.
  21.  * </p>
  22.  * <p>
  23.  * Some configuration implementations (for example {@link org.apache.commons.configuration2.DatabaseConfiguration} or
  24.  * {@link org.apache.commons.configuration2.JNDIConfiguration} use an underlying storage that can throw an exception on
  25.  * each property access. In earlier versions of this library such exceptions were logged and then silently ignored. This
  26.  * makes it impossible for a client to find out that something went wrong.
  27.  * </p>
  28.  * <p>
  29.  * To give clients better control over the handling of errors that might occur while interacting with a configuration
  30.  * object, a specialized error event type is introduced. Clients can register as listeners of this event type at a
  31.  * configuration object and are then notified about all internal errors related to the source configuration object.
  32.  * </p>
  33.  * <p>
  34.  * This class defines similar properties to the {@link ConfigurationEvent} class. This makes it possible to find out
  35.  * which operation was performed on a configuration causing this error event. In addition, a {@code Throwable} object is
  36.  * available representing the occurred error. Note that depending on the event type and the occurred exception not all
  37.  * of the other properties (for example name of the affected property or its value) may be available.
  38.  * </p>
  39.  *
  40.  * @since 1.4
  41.  * @see ConfigurationEvent
  42.  */
  43. public class ConfigurationErrorEvent extends Event {
  44.     /**
  45.      * Constant for the common event type for all error events. Specific types for error events use this type as super type.
  46.      *
  47.      * @since 2.0
  48.      */
  49.     public static final EventType<ConfigurationErrorEvent> ANY = new EventType<>(Event.ANY, "ERROR");

  50.     /**
  51.      * Constant for the event type indicating a read error. Errors of this type are generated if the underlying data store
  52.      * throws an exception when reading a property.
  53.      *
  54.      * @since 2.0
  55.      */
  56.     public static final EventType<ConfigurationErrorEvent> READ = new EventType<>(ANY, "READ_ERROR");

  57.     /**
  58.      * Constant for the event type indicating a write error. Errors of this type are generate if the underlying data store
  59.      * throws an exception when updating data.
  60.      *
  61.      * @since 2.0
  62.      */
  63.     public static final EventType<ConfigurationErrorEvent> WRITE = new EventType<>(ANY, "WRITE_ERROR");

  64.     /**
  65.      * The serial version UID.
  66.      */
  67.     private static final long serialVersionUID = 20140712L;

  68.     /** The event type of the operation which caused this error. */
  69.     private final EventType<?> errorOperationType;

  70.     /** Stores the property name. */
  71.     private final String propertyName;

  72.     /** Stores the property value. */
  73.     private final Object propertyValue;

  74.     /** Stores the exception that caused this event. */
  75.     private final Throwable cause;

  76.     /**
  77.      * Creates a new instance of {@code ConfigurationErrorEvent} and sets all its properties.
  78.      *
  79.      * @param source the event source
  80.      * @param eventType the type of this event
  81.      * @param operationType the event type of the operation causing this error
  82.      * @param propName the name of the affected property
  83.      * @param propValue the value of the affected property
  84.      * @param cause the exception object that caused this event
  85.      */
  86.     public ConfigurationErrorEvent(final Object source, final EventType<? extends ConfigurationErrorEvent> eventType, final EventType<?> operationType,
  87.         final String propName, final Object propValue, final Throwable cause) {
  88.         super(source, eventType);
  89.         errorOperationType = operationType;
  90.         propertyName = propName;
  91.         propertyValue = propValue;
  92.         this.cause = cause;
  93.     }

  94.     /**
  95.      * Gets the cause of this error event. This is the {@code Throwable} object that caused this event to be fired.
  96.      *
  97.      * @return the cause of this error event
  98.      */
  99.     public Throwable getCause() {
  100.         return cause;
  101.     }

  102.     /**
  103.      * Gets the {@code EventType} of the operation which caused this error.
  104.      *
  105.      * @return the event type of the operation causing this error
  106.      */
  107.     public EventType<?> getErrorOperationType() {
  108.         return errorOperationType;
  109.     }

  110.     /**
  111.      * Gets the name of the property that was accessed when this error occurred.
  112.      *
  113.      * @return the property name related to this error (may be <strong>null</strong>)
  114.      */
  115.     public String getPropertyName() {
  116.         return propertyName;
  117.     }

  118.     /**
  119.      * Gets the value of the property that was accessed when this error occurred.
  120.      *
  121.      * @return the property value related this error (may be <strong>null</strong>)
  122.      */
  123.     public Object getPropertyValue() {
  124.         return propertyValue;
  125.     }
  126. }