Class AbstractResource

java.lang.Object
org.apache.sis.storage.AbstractResource
All Implemented Interfaces:
Resource
Direct Known Subclasses:
AbstractFeatureSet, AbstractGridCoverageResource, BandGroup, GroupAggregate

public abstract class AbstractResource extends Object implements Resource
Default implementations of several methods for classes that want to implement the Resource interface. This class provides a getMetadata() method which extracts information from other methods. Subclasses should override the following methods:

Thread safety

Default methods of this abstract class are thread-safe. Synchronization, when needed, uses getSynchronizationLock().
Since:
1.2
Version:
1.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final StoreListeners
    The set of registered StoreListeners for this resources.
    private org.opengis.metadata.Metadata
    A description of this resource as an unmodifiable metadata, or null if not yet computed.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractResource(StoreListeners parentListeners, boolean hidden)
    Creates a new resource which can send notifications to the given set of listeners.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends StoreEvent>
    void
    addListener(Class<T> eventType, StoreListener<? super T> listener)
    Registers a listener to notify when the specified kind of event occurs in this resource or in children.
    protected void
    Clears any cache in this resource, forcing the data to be recomputed when needed again.
    (package private) final String
    createExceptionMessage(String filename, org.opengis.geometry.Envelope request)
    Creates an exception message for a resource that cannot be read.
    protected org.opengis.metadata.Metadata
    Invoked in a synchronized block the first time that getMetadata() is invoked.
    Optional<org.opengis.geometry.Envelope>
    Returns the spatiotemporal envelope of this resource.
    Optional<org.opengis.util.GenericName>
    Returns the resource persistent identifier if available.
    final org.opengis.metadata.Metadata
    Returns a description of this resource.
    protected Object
    Returns the object on which to perform synchronizations for thread-safety.
    <T extends StoreEvent>
    void
    removeListener(Class<T> eventType, StoreListener<? super T> listener)
    Unregisters a listener previously added to this resource for the given type of events.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • listeners

      protected final StoreListeners listeners
      The set of registered StoreListeners for this resources. This StoreListeners has DataStore.listeners has a parent.
    • metadata

      private volatile org.opengis.metadata.Metadata metadata
      A description of this resource as an unmodifiable metadata, or null if not yet computed. If non-null, this metadata should contain at least the resource identifier. Those metadata are created by createMetadata() when first needed.
      See Also:
  • Constructor Details

    • AbstractResource

      protected AbstractResource(StoreListeners parentListeners, boolean hidden)
      Creates a new resource which can send notifications to the given set of listeners. If hidden is false (the recommended value), then this resource will have its own set of listeners with this resource declared as the source of events. It will be possible to add and remove listeners independently from the set of parent listeners. Conversely if hidden is true, then the given listeners will be used directly and this resource will not appear as the source of any event.

      In any cases, the listeners of all parents (ultimately the data store that created this resource) will always be notified, either directly if hidden is true or indirectly if hidden is false.

      Parameters:
      parentListeners - listeners of the parent resource, or null if none. This is usually the listeners of the DataStore that created this resource.
      hidden - false if this resource shall use its own StoreListeners with the specified parent, or true for using parentListeners directly.
  • Method Details

    • getIdentifier

      public Optional<org.opengis.util.GenericName> getIdentifier() throws DataStoreException
      Returns the resource persistent identifier if available. The default implementation returns an empty value. Subclasses are strongly encouraged to override if they can provide a value.
      Implementation note: the default implementation of createMetadata() uses this identifier for initializing the metadata/identificationInfo/citation/title property.
      Specified by:
      getIdentifier in interface Resource
      Returns:
      a persistent identifier unique within the data store, or absent if this resource has no such identifier.
      Throws:
      DataStoreException - if an error occurred while fetching the identifier.
      See Also:
    • getEnvelope

      public Optional<org.opengis.geometry.Envelope> getEnvelope() throws DataStoreException
      Returns the spatiotemporal envelope of this resource. This information is part of API only in some kinds of resource such as FeatureSet. But the method is provided in this base class for convenience. The default implementation gives an empty value.
      Implementation note: the default implementation of createMetadata() uses this identifier for initializing the metadata/identificationInfo/extent/geographicElement property.
      Returns:
      the spatiotemporal resource extent.
      Throws:
      DataStoreException - if an error occurred while reading or computing the envelope.
    • getMetadata

      public final org.opengis.metadata.Metadata getMetadata() throws DataStoreException
      Returns a description of this resource. This method invokes createMetadata() in a synchronized block when first needed, then caches the result.
      Specified by:
      getMetadata in interface Resource
      Returns:
      information about this resource (never null).
      Throws:
      DataStoreException - if an error occurred while reading or computing the metadata.
      See Also:
    • createMetadata

      protected org.opengis.metadata.Metadata createMetadata() throws DataStoreException
      Invoked in a synchronized block the first time that getMetadata() is invoked. The default implementation populates metadata based on information provided by getIdentifier() and getEnvelope(). Subclasses should override if they can provide more information. The default value can be completed by casting to DefaultMetadata.
      Returns:
      the newly created metadata, or null if unknown.
      Throws:
      DataStoreException - if an error occurred while reading metadata from this resource.
    • clearCache

      protected void clearCache()
      Clears any cache in this resource, forcing the data to be recomputed when needed again. The default implementation clears the cached metadata object, which will cause createMetadata() to be invoked again when first needed.
    • getSynchronizationLock

      protected Object getSynchronizationLock()
      Returns the object on which to perform synchronizations for thread-safety. The default implementation returns this. Subclasses can override for example for synchronizing everything on an enclosing DataStore.
      Returns:
      the synchronization lock.
    • 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 in this resource or in children. The default implementation forwards to listeners.addListener(eventType, listener).
      Specified by:
      addListener in interface Resource
      Type Parameters:
      T - compile-time value of the eventType argument.
      Parameters:
      eventType - type of StoreEvents to listen (cannot be null).
      listener - listener to notify about events.
    • removeListener

      public <T extends StoreEvent> void removeListener(Class<T> eventType, StoreListener<? super T> listener)
      Unregisters a listener previously added to this resource for the given type of events. The default implementation forwards to listeners.removeListener(eventType, listener)
      Specified by:
      removeListener in interface Resource
      Type Parameters:
      T - compile-time value of the eventType argument.
      Parameters:
      eventType - type of StoreEvents which were listened (cannot be null).
      listener - listener to stop notifying about events.
    • createExceptionMessage

      final String createExceptionMessage(String filename, org.opengis.geometry.Envelope request)
      Creates an exception message for a resource that cannot be read. If the error is potentially created by a request out of bounds, this method tries to build a message with the problematic coordinates.
      Parameters:
      filename - some identification (typically a file name) of the data that cannot be read.
      request - the requested domain, or null if the problem is not a request out of bounds.
      Returns:
      the message to provide in an exception.