This article covers a Bloomreach Experience Manager version 12. There's an updated version available that covers our most recent release.

Respond to Create Channel and Update Channel Settings Events

Introduction

Goal

Implement custom logic which gets triggered when a user creates a channel or updates a channel's settings in the Channel Manager.

Background

Users can create channels (from blueprints) and update a channel's settings in the Channel Manager. A delivery tier implementation may respond to these events by implementing custom logic in a Channel Manager Event Listener.

ChannelManagerEventListener Interface

The HST API provides an interface org.hippoecm.hst.configuration.channel.ChannelManagerEventListener which defines two callback methods:

  • void channelCreated(ChannelManagerEvent event)
    Called immediately after a channel has been created (from a blueprint).
  • void channelUpdated(ChannelManagerEvent event)
    Called immediately after a channel's settings have been updated.

In your project, you can register any number of ChannelManagerEventListener implementations. Whenever one of the above events occurs, the corresponding callback method of each registered listener will be executed.

A ChannelManagerEventListener implementation must be registered as a Spring bean (see Spring Configuration example below).

Example ChannelManagerEventListener Implementation

The following example is part of the Hippo Test Suite.

Implementation Class

hippo-testsuite/components/src/main/java/org/hippoecm/hst/demo/channel/DemoChannelManagerEventListener.java

/**
 * DemoChannelManagerEventListener
 * <P>
 * Example <CODE>ChannelManagerEventListener</CODE> implementation
 * which simply logs all the triggered event information.
 * </P>
 */
public class DemoChannelManagerEventListener
            implements ChannelManagerEventListener {

    private static Logger log =
            LoggerFactory.getLogger(DemoChannelManagerEventListener.class);

    public void channelCreated(ChannelManagerEvent event) {
        log.info("A channel has been created. {}",
                 channelManagerEventToString(event));
    }

    public void channelUpdated(ChannelManagerEvent event) {
        log.info("A channel has been updated. {}",
                 channelManagerEventToString(event));
    }

    private String channelManagerEventToString(ChannelManagerEvent event) {
        StringBuilder sb = new StringBuilder(100);
        sb.append("{ ");

        Blueprint blueprint = event.getBlueprint();
        // blueprint can be null
        if (blueprint != null) {
            sb.append("blueprint: [ ");
            sb.append(blueprint.getId()).append(", ");
            sb.append(blueprint.getName()).append(", ");
            sb.append(blueprint.getDescription());
            sb.append(" ], ");
        }

        Channel channel = event.getChannel();
        // channel will never be null
        sb.append("channel: [ ");
        // note in 10.2.0 event.getChannelId() has been deprecated and 
        // must be replaced by event.getChannel().getId()
        sb.append(event.getChannelId()).append(", ");
        sb.append(channel.getName()).append(", ");
        sb.append(channel.getContentRoot());
        sb.append(" ], ");

        Node configRootNode = event.getConfigRootNode();

        try {
            if (configRootNode != null) {
                sb.append("configRootNode: ");
                sb.append(configRootNode.getPath());
            }
        } catch (RepositoryException e) {
            log.error("Failed to read channel node path", e);
        }

        sb.append(" }");
        return sb.toString();
    }
}

Spring Configuration

hippo-testsuite/components/src/main/resources/META-INF/hst-assembly/overrides/channelManagerListenerExample.xml

  <!-- Custom channel manager event listeners in the following may be
       overriden in their project specific assembly. -->
  <bean id="customChannelManagerEventListeners"
        class="org.springframework.beans.factory.config.ListFactoryBean">
    <property name="sourceList">
      <list>
        <bean class=
             "org.hippoecm.hst.demo.channel.DemoChannelManagerEventListener">
        </bean>
      </list>
    </property>
  </bean>
Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?