Sunday, 11 January 2015

Customizing DeploymentEventListener

To confgiure a customized DeploymentEventListener, Here are the steps.
  1. Define a listener service class that implements the interface DeploymentEventListener and implement the method deploymentEvent(DeploymentEvent pEvent).
  2. Configure an instance in Nucleus on the appropriate server and register it with the DeploymentServer or DeploymentAgent as appropriate.
  • Configure the listener on the asset management server (BCC) and register it with the /atg/epub/DeploymentServer, so it listens for deployment events fired when the deployment changes its state.
  • Configure the listener on the target (production/stagging) server and register it with the local /atg/epub/DeploymentAgent, so it listens for deployment events fired when an agent changes its state.
Here is sample code to print the assets changed/modified in BCC project. Along with configuration. You can customize code as per your need.

Note : Custom is the custom module created for this customization.

1.   Custom listener class.
Path : /Custom/src/com/test/service/CustomDeploymentEventListener.java
File Content : 


package com.test.service;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.ejb.EJBException;
import javax.ejb.FinderException;

import atg.deployment.common.event.DeploymentEvent;
import atg.deployment.common.event.DeploymentEventListener;
import atg.epub.project.Project;
import atg.epub.project.ProjectConstants;
import atg.epub.project.ProjectHome;
import atg.nucleus.GenericService;
import atg.repository.RepositoryException;
import atg.repository.RepositoryItem;
import atg.versionmanager.AssetVersion;
import atg.versionmanager.exceptions.VersionException;

public class CustomDeploymentEventListener extends GenericService implements DeploymentEventListener {
/**
* boolean to denote whether to listen the event or ignore it
*/
private boolean enabled;
@SuppressWarnings("unchecked")
@Override
public void deploymentEvent(DeploymentEvent pDeploymentevent) {
vlogDebug("Enter deploymentEvent method of CustomDeploymentEventListener");
if (isEnabled() && pDeploymentevent != null &&
                 DeploymentEvent.DEPLOYMENT_COMPLETE == pDeploymentevent.getNewState()) {

 String[] projectIds = pDeploymentevent.getDeploymentProjectIDs();
 Project project = null;
 Set workSpaceAssets = null;
 Set<RepositoryItem> repositoryItemsUpdated = new LinkedHashSet<RepositoryItem>();
 Iterator<AssetVersion> workspaceAssetsIterator = null;
 RepositoryItem repositoryItem = null;
 try {
    if (projectIds != null && projectIds.length >= 1) {
   ProjectHome home = ProjectConstants.getPersistentHomes().getProjectHome();
   try {
project = home.findById(projectIds[0]);
   } catch (EJBException ejbException) {
if (isLoggingError()) {
logError("Cannot find project with Id " + projectIds[0], ejbException);
}
   } catch (FinderException finderException) {
if (isLoggingError()) {
       logError("Cannot find project with Id " 
                                                    + projectIds[0], finderException);
}
   }
}
if (project != null) {
workSpaceAssets = project.getAssets();
}
if (workSpaceAssets != null) {
 workspaceAssetsIterator = (Iterator<AssetVersion>) workSpaceAssets.iterator();
}
if (workspaceAssetsIterator != null) {
while (workspaceAssetsIterator.hasNext()) {
AssetVersion assetVersion = workspaceAssetsIterator.next();
try {
if (assetVersion != null) {
repositoryItem = assetVersion.getRepositoryItem();                        repositoryItemsUpdated.add(repositoryItem);
}
} catch (RepositoryException repoExc) {
      vlogError("Repository Exception while accessing item " 
                                                                   + repositoryItem, repoExc);
}
}
}

if (!repositoryItemsUpdated.isEmpty()) {
//print updated items
printUpdatedItems(repositoryItemsUpdated);
}
} catch (VersionException versionExc) {
vlogError("Version exception occured while processing deployed project"
                                                               , versionExc);
}
 }
vlogDebug("Exit deploymentEvent method of CustomDeploymentEventListener");
         }
/**
* Method to print updated repository items
* @param repositoryItems
*/
private void printUpdatedItems(Set<RepositoryItem> repositoryItems) {
vlogDebug("Start :: printUpdatedItems(Set<RepositoryItem>)");
for (RepositoryItem repositoryItem : repositoryItems) {
System.out.println("Updated item is "+repositoryItem);
}
vlogDebug("End :: printUpdatedItems(Set<RepositoryItem>)");
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

2.   Custom listener component.
Path : /Custom/config/com/test/service/listener/CustomDeploymentEventListener.properties
File Content :
       $class=com.test.service.CustomDeploymentEventListener
       enabled=true

3.   Deployment agent configuration (For agent instances).
Path : /Custom/config/atg/epub/DeploymentAgent.properties
File Content :
      deploymentEventListeners+=/com/test/service/listener/CustomDeploymentEventListener

4.   Deployment server configuration (For BCC instance).
Path : /Custom/config/atg/epub/DeploymentServer.properties
File Content :
       deploymentEventListeners+=/com/test/service/listener/CustomDeploymentEventListener

No comments:

Post a Comment