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
/** * 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: [ "); sb.append(event.getChannel().getId()).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
<!-- 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>