package org.apache.ws.muws.v1_0.topics.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.XmlObjectWrapper;
import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.muws.impl.CategoryImpl;
import org.apache.ws.muws.v1_0.MuwsConstants;
import org.apache.ws.muws.v1_0.events.Situation;
import org.apache.ws.muws.v1_0.events.impl.SituationImpl;
import org.apache.ws.muws.v1_0.events.impl.XmlBeansManagementEvent;
import org.apache.ws.notification.topics.impl.TopicImpl;
import org.apache.ws.resource.ResourceCreationEvent;
import org.apache.ws.resource.ResourceCreationListener;
import org.apache.ws.resource.ResourceDestructionEvent;
import org.apache.ws.resource.ResourceDestructionListener;
import org.apache.ws.resource.i18n.MessagesImpl;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.i18n.Messages;
import org.apache.xmlbeans.XmlObject;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart1.ManagementEventDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.CreationNotificationDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.DestructionNotificationDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;
/**
* An Advertisement Topic impl for Resource Creation and Deletion.
*
* @author Sal Campana
*/
public class XmlBeansAdvertisementTopicImpl extends TopicImpl implements ResourceCreationListener, ResourceDestructionListener
{
private static final Log LOG = LogFactory.getLog( XmlBeansAdvertisementTopicImpl.class );
private static final Messages MSG = MessagesImpl.getInstance();
public XmlBeansAdvertisementTopicImpl(String s)
{
super(s);
}
/**
* Publishes an event to subscribers.
*
* @param o
* @throws Exception
*/
public void publish(Object o) throws Exception
{
super.publish(o);
}
/**
* Handles the event when a creation occurs. Builds a CreationNotification
* and sends notif to subscribers
*
* @param event
*/
public void creationOccurred(ResourceCreationEvent event)
{
CreationNotificationDocument creationNotificationDocument = CreationNotificationDocument.Factory.newInstance();
org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.CreationNotificationDocument.CreationNotification creationNotification = creationNotificationDocument.addNewCreationNotification();
EndpointReference endpointReference = event.getEndpointReference();
if(endpointReference != null && endpointReference instanceof XmlBeansEndpointReference)
{
XmlBeansEndpointReference xmlepr = (XmlBeansEndpointReference)endpointReference;
XmlObject xmlObject = xmlepr.getXmlObject(org.apache.ws.addressing.v2004_08_10.AddressingConstants.NSURI_ADDRESSING_SCHEMA);
if(xmlObject instanceof EndpointReferenceType)
{
creationNotification.setManageabilityEndpointReferenceArray(new EndpointReferenceType[]{(EndpointReferenceType)xmlObject});
try
{
publish(buildManagementEvent(creationNotificationDocument));
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
{
LOG.debug("Publishing of the notification: " + creationNotificationDocument.toString() + " failed.", e);
}
}
}
else
{
LOG.debug("EPR did not contain an instance of: " + EndpointReferenceType.class.getName() + " but rather it was: " + xmlObject.getClass().getName());
}
}
else
{
LOG.debug("The EndpointReference was either null or it was not an instance of XmlObjectWrapper. EPR: " + endpointReference );
}
}
/**
* Handles the event when a destruction occurs. Builds a DestructionNotificationDocument
* and sends notif to subscribers
*
* @param event
*/
public void destructionOccurred(ResourceDestructionEvent event)
{
Object resourceID = event.getResourceID();
if (resourceID != null)
{
DestructionNotificationDocument destructionNotificationDocument = DestructionNotificationDocument.Factory.newInstance();
org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.DestructionNotificationDocument.DestructionNotification destructionNotification = destructionNotificationDocument.addNewDestructionNotification();
destructionNotification.setResourceId((String) resourceID);
try
{
publish(buildManagementEvent(destructionNotificationDocument));
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
{
LOG.debug("Publishing of the notification: " + destructionNotificationDocument.toString() + " failed.", e);
}
}
}
}
/**
* Builds a ManagementEvent to hold the Creation/Destruction event.
*
* @param event
* @return ManagementEvent
*/
private XmlObject buildManagementEvent(XmlObject event)
{
ManagementEventDocument me;
Situation situation = new SituationImpl(new CategoryImpl(MuwsConstants.SITUATION_OTHER));
XmlBeansManagementEvent xme = new XmlBeansManagementEvent(situation);
me = (ManagementEventDocument) ((XmlObjectWrapper) xme).getXmlObject();
org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart1.ManagementEventType managementEvent = me.getManagementEvent();
XmlBeanUtils.addChildElement(managementEvent, event);
return me;
}
}