Package org.apache.muse.ws.dm.muws.adv.impl

Source Code of org.apache.muse.ws.dm.muws.adv.impl.AbstractAdvertisement

/*=============================================================================*
*  Copyright 2006 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*=============================================================================*/

package org.apache.muse.ws.dm.muws.adv.impl;

import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.muse.core.AbstractCapability;
import org.apache.muse.core.Environment;
import org.apache.muse.core.Resource;
import org.apache.muse.core.ResourceManager;
import org.apache.muse.core.ResourceManagerListener;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.ws.dm.muws.MuwsConstants;
import org.apache.muse.ws.dm.muws.adv.Advertisement;
import org.apache.muse.ws.dm.muws.events.Component;
import org.apache.muse.ws.dm.muws.events.ComponentAddress;
import org.apache.muse.ws.dm.muws.events.ManagementEvent;
import org.apache.muse.ws.dm.muws.events.Situation;
import org.apache.muse.ws.dm.muws.events.WefFactory;
import org.apache.muse.ws.dm.muws.events.WefConstants;
import org.apache.muse.ws.dm.muws.events.impl.SimpleWefFactory;
import org.apache.muse.ws.notification.NotificationProducer;
import org.apache.muse.ws.notification.WsnConstants;
import org.apache.muse.ws.notification.impl.TopicFilter;

/**
*
* @author Dan Jemiolo (danj)
*
*/

public abstract class AbstractAdvertisement
    extends AbstractCapability implements Advertisement, ResourceManagerListener
{
    private static final String _CONSUMER_EPR_PARAM = "initial-consumer-reference";
       
    protected WefFactory createWefFactory()
    {
        return new SimpleWefFactory();
    }
   
    public void initializeCompleted()
        throws SoapFault
    {
        super.initializeCompleted();
       
        Resource resource = getResource();
       
        //
        // this allows the advertisement capability to hear about
        // all resource lifecycle events that happen in the application
        //
        ResourceManager manager = resource.getResourceManager();
        manager.addListener(this);
               
        //
        // initialize the advertisement topics
        //
        NotificationProducer wsn =
            (NotificationProducer)resource.getCapability(WsnConstants.PRODUCER_URI);
       
        wsn.addTopic(MuwsConstants.ADV_ME_CREATION_TOPIC);
        wsn.addTopic(MuwsConstants.ADV_ME_DESTRUCTION_TOPIC);
       
        //
        // create subscriptions for all event consumer that are going to
        // receive notifications from the above topics
        //
        subscribeInitialConsumer();
    }
   
    protected abstract boolean isAdvertised(EndpointReference epr);

    public void resourceAdded(EndpointReference epr, Resource resource)
        throws SoapFault
    {
        if (!isAdvertised(epr))
            return;
       
        Element payload = XmlUtils.createElement(MuwsConstants.MANAGEABILITY_EPR_QNAME, epr.toXML());
        sendMessage(epr, MuwsConstants.ADV_ME_CREATION_TOPIC, WefConstants.CREATE_SITUATION_QNAME, payload);
    }

    public void resourceRemoved(EndpointReference epr)
        throws SoapFault
    {
        if (!isAdvertised(epr))
            return;
       
        Element payload = XmlUtils.createElement(MuwsConstants.DESTROY_NOTIFICATION_QNAME);
        sendMessage(epr, MuwsConstants.ADV_ME_DESTRUCTION_TOPIC, WefConstants.DESTROY_SITUATION_QNAME, payload);
       
        Resource advertiser = getResource();
       
        if (advertiser.hasBeenShutdown())
            advertiser.getResourceManager().removeListener(this);
    }
   
    protected void sendMessage(EndpointReference sourceEPR,
                               QName topicName,
                               QName situationCategory,
                               Element payload)
        throws SoapFault
    {
        Resource advertiser = getResource();
       
        WefFactory factory = createWefFactory();
        ManagementEvent event = factory.createEvent();
       
        event.addExtendedElement(payload);

        Component source = factory.createComponent();
        source.setName(WefConstants.SOURCE_COMP_QNAME);
       
        ComponentAddress address = factory.createComponentAddress(sourceEPR);
        source.setAddress(address);
       
        event.setSource(source);
       
        Component reporter = factory.createComponent();
        reporter.setName(WefConstants.REPORTER_COMP_QNAME);
       
        address = factory.createComponentAddress(advertiser.getEndpointReference());
        reporter.setAddress(address);
       
        event.setReporter(reporter);
       
        Situation situation = factory.createSituation();
        situation.setCategoryType(situationCategory);
        event.setSituation(situation);

        NotificationProducer wsn = (NotificationProducer)advertiser.getCapability(WsnConstants.PRODUCER_URI);
        wsn.publish(topicName, event.toXML());
    }
   
    protected void subscribeInitialConsumer()
        throws SoapFault
    {
        String fileName = getInitializationParameter(_CONSUMER_EPR_PARAM);
       
        //
        // pre-existing consumer is optional - other consumers can
        // still subscribe using WS-N
        //
        if (fileName != null)
        {
            Environment env = getResource().getEnvironment();
            Document eprDoc = env.getDocument(fileName);
            Element eprXML = XmlUtils.getFirstElement(eprDoc);
           
            EndpointReference epr = new EndpointReference(eprXML);
           
            NotificationProducer wsn =
                (NotificationProducer)getResource().getCapability(WsnConstants.PRODUCER_URI);
           
            wsn.subscribe(epr, new TopicFilter(MuwsConstants.ADV_ME_CREATION_TOPIC), null, null);
            wsn.subscribe(epr, new TopicFilter(MuwsConstants.ADV_ME_DESTRUCTION_TOPIC), null, null);
        }
    }
}
TOP

Related Classes of org.apache.muse.ws.dm.muws.adv.impl.AbstractAdvertisement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.