Channel Manager Event Listener - BloomReach Experience - Open Source CMS

This article covers a Hippo CMS version 10. There's an updated version available that covers our most recent release.

30-12-2015

Channel Manager Event Listener

During the creation or modification of a channel, as a developer, you can register one or more  ChannelManagerEventListener(s) to be called back. A ChannelManagerEventListener implements two callback methods, that will be executed when a ChannelMananager event occurs.

The channel change events are:

  • creating a channel (from a blueprint)
  • updating a channel (by modifying the channel settings in the Channel Manager)
public interface ChannelManagerEventListener {    /**
     * Called immediately after a channel has been created
     * through {@link ChannelManager#persist(String, Channel)}.
     * @param event the {@link ChannelManagerEvent}
     */
    void channelCreated(ChannelManagerEvent event);

    /**
     * Called immediately after a channel has been updated
     * through {@link ChannelManager#save(Channel)}.
     * @param event the {@link ChannelManagerEvent}
     */
    void channelUpdated(ChannelManagerEvent event);
}

All your custom ChannelManagerEventListener(s) must be registered through a Spring bean. This can be done by adding the following spring configuration in your META-INF/hst-assembly/overrides:

<!-- 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>
        <!-- You can register channel manager event listener beans here !! -->
      </list>
    </property>
</bean> 

Example implementation of a ChannelManagerEventListener

/**
 * 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();
    }
}

Example Spring assembly bean registration:

  <!-- 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?