Package org.apache.sling.api.resource.observation

# Interface ResourceChangeListener

@ConsumerType public interface ResourceChangeListener
Listener for resource change events. Event listeners are notified asynchronously, and see events after they occur and the transaction is committed (which means the changes may already be visible to `ResourceResolver` objects before they are being processed by listeners).

`ResourceChangeListener` objects are registered with the Framework service registry and are notified with `ResourceChange` objects when a change occurs.

`ResourceChangeListener` objects can inspect the received `ResourceChange` objects to determine the type of change, location and other properties.

`ResourceChangeListener` objects must be registered with a service property `PATHS` whose value is the list of resource paths for which the listener is interested in getting change events.

By default a resource listener gets only local events which means events caused by changes persisted on the same instance as this listener is registered. If the resource listener is interested in external events, the implementation should implement the `ExternalResourceChangeListener` interface, but still register the service as a `ResourceChangeListener` service.

Since:
  1.0.0 (Sling API Bundle 2.11.0)

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`CHANGE_ADDED`
String constant for `ResourceChange.ChangeType.ADDED`.
`static final String`
`CHANGE_CHANGED`
String constant for `ResourceChange.ChangeType.CHANGED`.
`static final String`
`CHANGE_PROVIDER_ADDED`
String constant for `ResourceChange.ChangeType.PROVIDER_ADDED`.
`static final String`
`CHANGE_PROVIDER_REMOVED`
String constant for `ResourceChange.ChangeType.PROVIDER_REMOVED`.
`static final String`
`CHANGE_REMOVED`
String constant for `ResourceChange.ChangeType.REMOVED`.
`static final String`
`CHANGES`
Array of change types - optional.
`static final String`
`PATHS`
Array of paths or glob patterns - required.
`static final String`
`PROPERTY_NAMES_HINT`
An optional hint indicating to the underlying implementation that for changes regarding properties (added/removed/changed) the listener is only interested in those property names listed inhere.

## Method Summary

Modifier and Type
Method
Description
`void`
`onChange(@NotNull List<ResourceChange> changes)`
Report resource changes based on the filter properties of this listener.

## Field Details

### PATHS

static final String PATHS

Array of paths or glob patterns - required.

A path is either absolute or relative. If it's a relative path, the relative path will be appended to all search paths of the resource resolver.

If the whole tree of all search paths should be observed, the special value `.` should be used.

A glob pattern must start with the `glob:` prefix (e.g. `glob:**/*.html`). The following rules are used to interpret glob patterns:

- The `*` character matches zero or more characters of a name component without crossing directory boundaries.
- The `**` characters match zero or more characters crossing directory boundaries.
- If the pattern is relative (does not start with a slash), the relative path will be appended to all search paths of the resource resolver.

In general, it can't be guaranteed that the underlying implementation of the resources will send a remove/add event for each removed/added resource. For example if the root of a tree, like `/foo` is removed, the underlying implementation might only send a single remove event for `/foo` but not for any child resources. Similarly for renaming a subtree from `/foo` to `/bar` the add event may only be sent for `/bar` but not for any child resources. Therefore a listener might get remove/add events for resources that not directly match the specified paths/path patterns. For example if a listener is registered for path `/foo/bar` and `/foo` is removed, the listener will get a remove event for `/foo`. The same is true if any pattern is used and any parent of a matching resource is removed/added. The listener must handle these events accordingly.

If one of the paths is a sub resource of another specified path, the sub path is ignored.

If this property is missing or invalid, the listener is ignored. The type of the property must either be String, or a String array.

See Also:
  - Constant Field Values

### CHANGES

static final String CHANGES
Array of change types - optional. If this property is missing, added, removed and changed events for resources and added and removed events for resource providers are reported. If this property is invalid, the listener is ignored. The type of the property must either be String, or a String array. Valid values are the constants from this class whose names are starting with `CHANGE_`. They map to one of the values of `ResourceChange.ChangeType`.

See Also:
  - Constant Field Values

### CHANGE_ADDED

static final String CHANGE_ADDED
String constant for `ResourceChange.ChangeType.ADDED`.

Since:
  1.3.0 (Sling API Bundle 2.23.0)
See Also:
  - Constant Field Values

### CHANGE_REMOVED

static final String CHANGE_REMOVED
String constant for `ResourceChange.ChangeType.REMOVED`.

Since:
  1.3.0 (Sling API Bundle 2.23.0)
See Also:
  - Constant Field Values

### CHANGE_CHANGED

static final String CHANGE_CHANGED
String constant for `ResourceChange.ChangeType.CHANGED`.

Since:
  1.3.0 (Sling API Bundle 2.23.0)
See Also:
  - Constant Field Values

### CHANGE_PROVIDER_ADDED

static final String CHANGE_PROVIDER_ADDED
String constant for `ResourceChange.ChangeType.PROVIDER_ADDED`.

Since:
  1.3.0 (Sling API Bundle 2.23.0)
See Also:
  - Constant Field Values

### CHANGE_PROVIDER_REMOVED

static final String CHANGE_PROVIDER_REMOVED
String constant for `ResourceChange.ChangeType.PROVIDER_REMOVED`.

Since:
  1.3.0 (Sling API Bundle 2.23.0)
See Also:
  - Constant Field Values

### PROPERTY_NAMES_HINT

static final String PROPERTY_NAMES_HINT
An optional hint indicating to the underlying implementation that for changes regarding properties (added/removed/changed) the listener is only interested in those property names listed inhere. If the underlying implementation supports this, events for property names that are not enlisted here will not be delivered, however events concerning resources are not affected by this hint. This is only a hint, a change listener registering with this property must be prepared that the underlying implementation is not able to filter based on this property. In this case the listener gets all events as defined with the other properties.

See Also:
  - Constant Field Values

## Method Details

### onChange

void onChange(@NotNull @NotNull List<ResourceChange> changes)
Report resource changes based on the filter properties of this listener.

Note that resource changes for paths which are ancestors of the paths this listener was registered to may be reported through this method. This is due to limitations of certain resource providers to provide events on a more granular level (e.g. for deletion or movement of resources containing (potentially nested) child resources).

Starting with version 1.2 of this API, an instance of `ResoureChangeList` is passed as the parameter to allow passing additional information.

Parameters:
  `changes` - The changes list. This list is immutable.