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.capability.IdentityCapability;
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.PropertiesResource;
import org.apache.ws.resource.Resource;
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.properties.ResourceProperty;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.xmlbeans.XmlAnyURI;
import org.apache.xmlbeans.XmlObject;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart1.ManagementEventDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart1.ManagementEventType;
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);
public XmlBeansAdvertisementTopicImpl(String topicName)
{
super(topicName);
}
public XmlBeansAdvertisementTopicImpl(String topicName, boolean isVisible)
{
super(topicName);
setIsVisible(isVisible);
}
/**
* Returns the Muws Identity ResourceProperty from the Resource or null if the prop doesn't exist
*
* @param resource
* @return Muws Identity ResourceProperty or null if prop doesn't exist
*/
private ResourceProperty getIdentityProperty(Resource resource)
{
ResourceProperty identityProp = null;
if (resource instanceof PropertiesResource)
{
ResourceProperty resourceProperty = ((PropertiesResource) resource).getResourcePropertySet().get(IdentityCapability.PROP_NAME_RESOURCE_ID);
if (resourceProperty != null)
{
identityProp = resourceProperty;
}
}
return identityProp;
}
/**
* Handles the event when a creation occurs. Builds a CreationNotification
* and sends notif to subscribers
*
* @param event
*/
public void creationOccurred(ResourceCreationEvent event)
{
Resource resource = event.getResource();
//determine if resource is a muws resource
ResourceProperty identityProperty = getIdentityProperty(resource);
if (identityProperty != null)
{
CreationNotificationDocument creationNotifDoc = CreationNotificationDocument.Factory.newInstance();
CreationNotificationDocument.CreationNotification creationNotif = creationNotifDoc.addNewCreationNotification();
EndpointReference epr = resource.getEndpointReference();
if (epr != null && epr instanceof XmlBeansEndpointReference)
{
XmlBeansEndpointReference xBeansEPR = (XmlBeansEndpointReference) epr;
XmlObject xBean = xBeansEPR.getXmlObject(org.apache.ws.addressing.v2004_08_10.AddressingConstants.NSURI_ADDRESSING_SCHEMA);
if (xBean instanceof EndpointReferenceType)
{
creationNotif.setManageabilityEndpointReferenceArray(new EndpointReferenceType[]{(EndpointReferenceType) xBean});
try
{
publish(buildManagementEvent(creationNotifDoc));
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
{
LOG.debug("Publishing of the notification: " + creationNotifDoc + " failed.", e);
}
}
}
else
{
LOG.debug("EPR did not contain an instance of: " + EndpointReferenceType.class.getName() + " but rather it was: " + xBean.getClass().getName());
}
}
else
{
LOG.debug("The EndpointReference was either null or it was not an instance of XmlObjectWrapper. EPR: " + epr);
}
}
}
/**
* Handles the event when a destruction occurs. Builds a DestructionNotificationDocument
* and sends notif to subscribers
*
* @param event
*/
public void destructionOccurred(ResourceDestructionEvent event)
{
Resource resource = event.getResource();
ResourceProperty identityProperty = getIdentityProperty(resource);
if (identityProperty != null)
{
XmlAnyURI id = (XmlAnyURI) identityProperty.get(0);
String resourceID = null;
if (id == null)
{
resourceID = "ResourceID Unknown. May be singleton.";
}
else
{
resourceID = id.toString();
}
DestructionNotificationDocument destructionNotifDoc = DestructionNotificationDocument.Factory.newInstance();
DestructionNotificationDocument.DestructionNotification destructionNotif = destructionNotifDoc.addNewDestructionNotification();
destructionNotif.setResourceId((String) resourceID);
try
{
publish(buildManagementEvent(destructionNotifDoc));
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
{
LOG.debug("Publishing of the notification: " + destructionNotifDoc + " failed.", e);
}
}
}
}
/**
* Builds a ManagementEvent to hold the Creation/Destruction event.
*
* @param event
* @return ManagementEvent
*/
private XmlObject buildManagementEvent(XmlObject event)
{
Situation situation = new SituationImpl(new CategoryImpl(MuwsConstants.SITUATION_OTHER));
XmlBeansManagementEvent xme = new XmlBeansManagementEvent(situation);
ManagementEventDocument mgmtEventDoc = (ManagementEventDocument) ((XmlObjectWrapper) xme).getXmlObject();
ManagementEventType mgmtEvent = mgmtEventDoc.getManagementEvent();
XmlBeanUtils.addChildElement(mgmtEvent, event);
return mgmtEventDoc;
}
public boolean isVisible()
{
return m_isVisible;
}
}