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

ChannelManagerEventListener 

Available for HST 2.24.03 and higher

During the creation or modification of a channel, as a developer, you can plug in a callback handler with the ChannelManagerEventListener(s) and register them. Through a ChannelManagerEventListener, a developers can register two callback methods that will be executed when a ChannelMananager event occurs.

The channel change events are:

  • creating a channel
  • updating a channel

The ChannelManagerEventListener is as follows:

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 in your META-INF/hst-assembly/overrides the following spring configuration:

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

An example

Example implementation of the ChannelManagerEventListener

/**
 * DemoChannelManagerEventListener
 * <P>
 * Example <CODE>ChannelManagerEventListener</CODE> implementation
 * which simply logs all the triggered event information.
 * </P>
 *
 * @version $Id$
 */
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();

        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();

        if (channel != null) {
            sb.append("channel: [ ");
            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>