[Join-cvs] join/src/main/org/figure8/join/service Notifier.java,NONE,1.1 NotifierSupport.java,NONE,1
Brought to you by:
lbroudoux
|
From: <lbr...@us...> - 2003-07-30 14:21:17
|
Update of /cvsroot/join/join/src/main/org/figure8/join/service
In directory sc8-pr-cvs1:/tmp/cvs-serv19850
Added Files:
Notifier.java NotifierSupport.java MDBNotificationManager.java
Log Message:
Initial checkin
--- NEW FILE: Notifier.java ---
/*
* Notifier.java
*
* Created on 25 janvier 2003, 15:58
*/
package org.figure8.join.service;
import org.figure8.join.view.UpdateView;
import org.figure8.join.view.VersionView;
import org.figure8.join.businessobjects.environment.interfaces.LogicalEnvData;
/**
* Provides methods that the Join application Notification Service must implement.
* The Notification Service is used when Join is configured "full-featured" : as
* an Integration Network. Notification Service is used to keep current software
* project clients up-to-date with the project evolution. The notification of
* clients can occurs basically on 3 types of event : a logical environment creation
* event, a version creation event or an environment update event.
* @author <a href="mailto:lau...@fr...">Laurent Broudoux</a>
*/
public interface Notifier{
// Static -------------------------------------------------------------------
public static final int NOTIFICATION_KO = 0;
public static final int NOTIFICATION_OK = 1;
// Public -------------------------------------------------------------------
/**
* Notify current software project client of an environment update.
* @return NOTIFICATION_OK when nofication is successfull, NOTIFICATION_KO otherwise.
*/
public int notifyUpdate(UpdateView view);
/**
* Notify current software project client of a new sofware version creation.
* @return NOTIFICATION_OK when nofication is successfull, NOTIFICATION_KO otherwise.
*/
public int notifyVersion(VersionView view);
/**
* Notify current software project client of a new logical environment creation.
* @return NOTIFICATION_OK when notification is successfull, NOTIFICATION_KO otherwise.
*/
public int notifyEnvironment(LogicalEnvData data);
/**
* Set notifyer parameters before notifying clients of environment update.
* When defining a notification manager, this method must be called before
* notifyUpdate(). The parameters are typically implementation specific
* properties to set, such as :<br>
* <li>a connection url and a connection driver to use,
* <li>some security properties (user/password),
* <li>a Web Service endpoint address, ...
*/
public void setUpdateParameters(String parameters);
/**
* Set notifyer parameters before notifying clients of a new software version
* creation. When defining a notification manager, this method must be called
* before notifyVersion(). The parameters are typically implementation specific
* properties to set, such as :<br>
* <li>a connection url and a connection driver to use,
* <li>some security properties (user/password),
* <li>a Web Service endpoint address, ...
*/
public void setVersionParameters(String parameters);
/**
* Set notifyer parameters before notifying clients of a new logical environment
* creation. When defining a notification manager, this method must be called
* before notifyEnvironment(). The parameters are typically implementation
* specific properties to set, such as :<br>
* <li>a connection url and a connection driver to use,
* <li>some security properties (user/password),
* <li>a Web Service endpoint address, ...
*/
public void setEnvironmentParameters(String parameters);
}
--- NEW FILE: NotifierSupport.java ---
/*
* NotifierSupport.java
*
* Created on 25 janvier 2003, 16:06
*/
package org.figure8.join.service;
import org.figure8.join.util.LogUtil;
import org.figure8.join.view.UpdateView;
import org.figure8.join.view.VersionView;
import org.figure8.join.businessobjects.component.util.VersionUtil;
import org.figure8.join.businessobjects.environment.util.UpdateUtil;
import org.figure8.join.businessobjects.component.interfaces.VersionLocal;
import org.figure8.join.businessobjects.environment.interfaces.UpdateLocal;
import org.figure8.join.businessobjects.environment.interfaces.LogicalEnvData;
import org.apache.commons.logging.Log;
import java.lang.reflect.Method;
import java.util.StringTokenizer;
/**
* Abstract implementation of Notifier providing helper method for
* setting parameters of notification.
* @author <a href="mailto:lau...@fr...">Laurent Broudoux</a>
*/
public abstract class NotifierSupport implements Notifier{
// Attributes ---------------------------------------------------------------
/** Logger for diagnostic messages */
protected Log log;
// Constructors -------------------------------------------------------------
/** Creates a new instance of NotifierSupport */
public NotifierSupport(){
log = LogUtil.getLog(this.getClass());
}
// Public -------------------------------------------------------------------
/**
* Helper for setUpdateParameters() and setVersionParameters() of Notifyer.
* Take parameters as a dot-comma separated list of key/value pairs and set
* properties with name equals to <br>key</b>.<br>
* Example : parameters may be "driver=com.oracle.jdbc.Driver;user=tiger", this
* method will try to call setDriver("com.oracle.jdbc.Driver") and setUser("tiger")
* on current NotificationSupport extension class.
* @param dot-comma separated list of key/value
*/
public void setParameters(String parameters){
StringTokenizer tokenizer = new StringTokenizer(parameters, ";");
// Browse tokens to set properties using reflection.
while (tokenizer != null && tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
// Get key/value pair.
String key = token.substring(0, token.indexOf("="));
String value = token.substring(token.indexOf("=") + 1, token.length());
// Get property name with 1st letter in upper case.
String property = key.substring(0, 1).toUpperCase() + key.substring(1, key.length());
if (log.isDebugEnabled()){
log.debug("Setting notification props... Property :" + property + " - Value: " + value);
}
try{
// Get setProperty(String) method and use it.
Class clazz = this.getClass();
Method setProperty = clazz.getDeclaredMethod("set" + property, new Class[]{String.class});
setProperty.invoke(this, new Object[]{value});
}
catch (Exception e){
log.error("Setting notification property failed for property: " + property);
log.error("Check that Notifier implementation as a setter for this property !");
}
}
}
// Implementation of Notifyer -----------------------------------------------
/**
* Notify current software project client of an environment update.
* @return NOTIFICATION_OK when nofication is successfull, NOTIFICATION_KO otherwise.
*/
public abstract int notifyUpdate(UpdateView view);
/**
* Notify current software project client of an environment update.
* @return NOTIFICATION_OK when nofication is successfull, NOTIFICATION_KO otherwise.
*/
public abstract int notifyVersion(VersionView view);
/**
* Notify current software project client of a new logical environment creation.
* @return NOTIFICATION_OK when notification is successfull, NOTIFICATION_KO otherwise.
*/
public abstract int notifyEnvironment(LogicalEnvData data);
/**
* Set notifier parameters before notifying clients of environment update.
* When defining a notification manager, this method must be called before
* notifyUpdate(). The parameters are typically implementation specific
* properties to set, such as :<br>
* <li>a connection url and a connection driver to use,
* <li>some security properties (user/password),
* <li>a Web Service endpoint address, ...
*/
public void setUpdateParameters(String parameters){
setParameters(parameters);
}
/**
* Set notifier parameters before notifying clients of a new software version
* creation. When defining a notification manager, this method must be called
* before notifyVersion(). The parameters are typically implementation spcecific
* properties to set, such as :<br>
* <li>a connection url and a connection driver to use,
* <li>some security properties (user/password),
* <li>a Web Service endpoint address, ...
*/
public void setVersionParameters(String parameters){
setParameters(parameters);
}
/**
* Set notifier parameters before notifying clients of a new logical environment
* creation. When defining a notification manager, this method must be called
* before notifyEnvironment(). The parameters are typically implementation
* specific properties to set, such as :<br>
* <li>a connection url and a connection driver to use,
* <li>some security properties (user/password),
* <li>a Web Service endpoint address, ...
*/
public void setEnvironmentParameters(String parameters){
setParameters(parameters);
}
}
--- NEW FILE: MDBNotificationManager.java ---
/*
* MDBNotificationManager.java
*
* Created on 25 janvier 2003, 18:10
*/
package org.figure8.join.service;
import org.figure8.join.service.Notifier;
import org.figure8.join.view.UpdateView;
import org.figure8.join.view.VersionView;
import org.figure8.join.view.ConfigurationView;
import org.figure8.join.businessfacades.interfaces.ConfigurationManager;
import org.figure8.join.businessfacades.interfaces.ConfigurationManagerHome;
import org.figure8.join.businessobjects.environment.interfaces.LogicalEnvData;
import org.figure8.join.businessobjects.configuration.interfaces.ClientData;
import org.figure8.join.businessobjects.configuration.interfaces.NotificationFailureData;
import org.figure8.join.util.LogUtil;
import org.apache.commons.logging.Log;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.MessageListener;
import javax.ejb.MessageDrivenContext;
import javax.naming.InitialContext;
import java.util.Iterator;
/**
* Message Driven Bean for Join system's clients notification. This bean manages the
* notification of all the current configuration clients for a received event. This
* Message Driven Bean uses 1 parameter specified as an ejb reference :
* <li> <b>java:comp/env/ejb/ConfigurationManager</b> must be a reference to the remote
* home interface of the ConfigurationManager session bean handling the configuration
* operations of your Join system.
* @author <a href="mailto:lau...@fr...">Laurent Broudoux</a>
*
* @ejb.bean name="NotificationManager"
* destination-type="javax.jms.Topic"
* subscription-durability="NonDurable"
* transaction-type="Bean"
*
* @ejb.ejb-ref ejb-name="ConfigurationManager" ref-name="ejb/ConfigurationManager" view-type="remote"
*
* @jboss.ejb-ref-jndi ref-name="ConfigurationManager" jndi-name="${facades.configuration.remote.jndi-name}"
* @jboss.destination-jndi-name name="${resources.notificationtopic.jndi-name}"
*/
public class MDBNotificationManager implements javax.ejb.MessageDrivenBean, MessageListener{
// Attributes ---------------------------------------------------------------
private MessageDrivenContext mdctx = null;
private Log log;
private ConfigurationView config;
private ConfigurationManagerHome managerHome;
// Implementation of MessageListener ----------------------------------------
/**
* Notification Manager business method. This method takes place in steps
* <li> First, retrieve the object from JMS Message,
* <li> Then, depending on object class invoke a manageXXXNotification() method
* (this methods are responsible of clients notification, failures tracking and
* reporting).
* This methods are public so that you can extend them.
* @param message Message picked in topic.
*/
public void onMessage(Message message){
// Process only the ObjectMessages.
if (message instanceof ObjectMessage){
// Retrieve Object from JMS Message.
Object obj = null;
try {obj = ((ObjectMessage)message).getObject();}
catch (Exception e){
log.error("Exception when casting message in NotificationManager: " + e.getMessage());
return;
}
// Notify clients if there's at least one ...
if (config.getClients() != null && config.getClients().size() > 0){
// Depending on object class, manage notifications.
if (obj instanceof UpdateView)
manageUpdateNotification((UpdateView)obj);
else if (obj instanceof VersionView)
manageVersionNotification((VersionView)obj);
else if (obj instanceof LogicalEnvData)
manageEnvironmentNotification((LogicalEnvData)obj);
else
log.warn("Object class is not supported: " + obj.getClass());
}
}
return;
}
// Public -------------------------------------------------------------------
/**
* Get the ConfigurationManager remote home interface used.
* Useful when extending this bean and re-defining managerXXXNotification()
* methods.
*/
public ConfigurationManagerHome getConfigurationManagerHome(){
return managerHome;
}
/**
* Manage the notification of an "environment update" event for all
* the clients of the current configuration.
* @param update The view on Update conserned by this event.
*/
public void manageUpdateNotification(UpdateView update){
if (log.isInfoEnabled()){
log.info("Notifying clients of '" + config.getLabel() + "' configuration");
log.info("Event is environment Update with id: " + update.getId());
}
// Browse clients and notify each of them.
Iterator iterator = config.getClients().iterator();
while (iterator != null && iterator.hasNext()){
ClientData client = (ClientData)iterator.next();
try{
Notifier notifier = getNotifier(client);
log.debug("Notifying the client '" + client.getName() + "' of environment update");
if (client.getNotifierUpdateParams() != null && client.getNotifierUpdateParams().length() > 0){
// Launch client notification.
notifier.setUpdateParameters(client.getNotifierUpdateParams());
int result = notifier.notifyUpdate(update);
// Check result and and log a failure if KO.
if (result == Notifier.NOTIFICATION_KO)
throw new Exception("Failure in notifying client: " + client.getName());
}
}
catch (Exception e){
// Build a notification failure data object.
NotificationFailureData data = new NotificationFailureData();
data.setType("update");
data.setReference(update.getId());
try{
// Create the notification failure into system.
ConfigurationManager manager = managerHome.create();
manager.createNotificationFailure(data, client.getId());
manager.remove();
}
catch (Exception ee){
log.error("Error while reporting notification failure for update: " + data);
ee.printStackTrace();
}
}
}
}
/**
* Manage the notification of an "version creation" event for all
* the clients of the current configuration.
* @param version The view on Version concerned by this event.
*/
public void manageVersionNotification(VersionView version){
if (log.isInfoEnabled()){
log.info("Notifying clients of '" + config.getLabel() + "' configuration");
log.info("Event is a Version creation with id: " + version.getId());
}
// Browse clients and notify each of them.
Iterator iterator = config.getClients().iterator();
while (iterator != null && iterator.hasNext()){
ClientData client = (ClientData)iterator.next();
try{
Notifier notifier = getNotifier(client);
log.debug("Notifying the client '" + client.getName() +"' of version creation");
if (client.getNotifierVersionParams() != null && client.getNotifierVersionParams().length() > 0){
// Launch client notification.
notifier.setVersionParameters(client.getNotifierVersionParams());
int result = notifier.notifyVersion(version);
// Check result and log a failure if KO
if (result == Notifier.NOTIFICATION_KO)
throw new Exception("Failure in notifying client: " + client.getName());
}
}
catch (Exception e){
// Build a notification failure data object.
NotificationFailureData data = new NotificationFailureData();
data.setType("version");
data.setReference(version.getId());
try{
// Create the notification failure into system.
ConfigurationManager manager = managerHome.create();
manager.createNotificationFailure(data, client.getId());
manager.remove();
}
catch (Exception ee){
log.error("Error while reporting notification failure for version: " + data);
ee.printStackTrace();
}
}
}
}
/**
* Manage the notification of an "environment creation" event for all
* the clients of the current configuration.
* @param version The data of Environment concerned by this event.
*/
public void manageEnvironmentNotification(LogicalEnvData env){
if (log.isInfoEnabled()){
log.info("Notifying clients of '" + config.getLabel() + "' configuration");
log.info("Event is an Environment creation with label: " + env.getLabel());
}
// Browse clients and notify each of them.
Iterator iterator = config.getClients().iterator();
while (iterator != null && iterator.hasNext()){
ClientData client = (ClientData)iterator.next();
try{
Notifier notifier = getNotifier(client);
log.debug("Notifying the client '" + client.getName() +"' of environment creation");
if (client.getNotifierEnvironmentParams() != null && client.getNotifierEnvironmentParams().length() > 0){
// Launch client notification.
notifier.setEnvironmentParameters(client.getNotifierEnvironmentParams());
int result = notifier.notifyEnvironment(env);
// Check result and log a failure if KO.
if (result == Notifier.NOTIFICATION_KO)
throw new Exception("Failure in notifying client: " + client.getName());
}
}
catch (Exception e){
// Build a notification failure data object.
NotificationFailureData data = new NotificationFailureData();
data.setType("environment");
data.setReference(env.getId().toString());
try{
// Create the notification failure into system.
ConfigurationManager manager = managerHome.create();
manager.createNotificationFailure(data, client.getId());
manager.remove();
}
catch (Exception ee){
log.error("Error while reporting notification failure for update: " + data);
ee.printStackTrace();
}
}
}
}
// Implementation of MessageDrivenBean --------------------------------------
/** Create a new NotificationManager */
public void ejbCreate() {}
/** Retrieve message driven context and env-entries */
public void setMessageDrivenContext(MessageDrivenContext mdctx) throws javax.ejb.EJBException{
this.mdctx = mdctx;
// Get the logger for this MDB.
log = LogUtil.getLog(this.getClass());
try{
InitialContext ctx = new InitialContext();
// Get the configuration manager home JNDI and instance.
managerHome = (ConfigurationManagerHome)ctx.lookup("java:comp/env/ejb/ConfigurationManager");
// Retrieve and initialize the current config.
ConfigurationManager manager = managerHome.create();
config = manager.getActiveConfiguration();
manager.remove();
}
catch (Exception e) {e.printStackTrace(); throw new javax.ejb.EJBException("Unable to create a NotificationManager instance, checl environmenbt entries.");}
}
/** Remove this NotificationManager */
public void ejbRemove(){
this.mdctx = null;
}
// Protected ----------------------------------------------------------------
/**
* Build specifc notifier implementation.
* @param Client to retrieve a notifyer for.
*/
protected Notifier getNotifier(ClientData client) throws Exception{
if (log.isDebugEnabled())
log.debug("Building notifier of class: " + client.getNotifierImpl());
// Use current context class loader.
String notifierClass = client.getNotifierImpl();
Class notifierClazz = Thread.currentThread().getContextClassLoader().loadClass(notifierClass);
Notifier notifier = (Notifier)notifierClazz.newInstance();
// Return result.
return notifier;
}
}
|