Package org.apache.ws.muse.example.application

Source Code of org.apache.ws.muse.example.application.EventScheduler$EventTask

/*=============================================================================*
*  Copyright 2005 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.ws.muse.example.application;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.id.uuid.UUID;
import org.apache.commons.id.IdentifierUtils;
import org.apache.ws.mows.v1_0.capability.OperationalStatusCapability;
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.LangStringImpl;
import org.apache.ws.muws.v1_0.events.impl.SituationImpl;
import org.apache.ws.muws.v1_0.events.impl.XmlBeansManagementEvent;
import org.apache.ws.muws.v1_0.topics.ManagementEventTopic;
import org.apache.ws.namespaces.muse.muwsExt1.CorrelationIdDocument;
import org.apache.ws.notification.base.NotificationProducerResource;
import org.apache.ws.notification.topics.TopicSpace;
import org.apache.ws.notification.topics.TopicSpaceSet;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.resource.properties.ResourcePropertySet;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlAnyUriImpl;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.LangString;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

/**
* @author Kinga Dziembowski
*
* EventScheduler is an utility class to simulate operational events.
* You can set the delay in minutes when the EventScheduler will start generate events.
* You can set the frequency of events.
* The events are generated in pairs - the first event will notify that the disk space is low, the second event will
* notify that the disk space is at appropriate level. Both events will have this same CorrelationId to identify them by the manager.
*/
public class EventScheduler
{
   private static final Log LOG = LogFactory.getLog( EventScheduler.class.getName(  ) );

   /* The timer */
   private Timer m_timer;

   /* Boolean variable to alternate positive and negative events */
   private boolean firstEvent = true;

   /* The delay in minutes the first event will be fired after the start is called */
   private int m_minutesDelay = 60;

   /* The frequency in second in which the events will be generated */
   private int m_frequencySeconds = 5;

   /* The NotificationProducerResource reference */
   private NotificationProducerResource m_notificationProducerResource;

   /** The constructor used when the default delay and frequency are acceptable
    *
    * @param resource
    * @throws Exception
    */
   public EventScheduler( NotificationProducerResource resource )
   throws Exception
   {
      if ( resource == null )
      {
         throw new Exception( "Notification Producer is null" );
      }

      this.m_notificationProducerResource = resource;
   }

   /** The constructor used when the default delay and frequency are acceptable
    *
    * @param minutesDelay
    * @param secondsFrequency
    * @param resource
    * @throws Exception
    */
   public EventScheduler( int                          minutesDelay,
                          int                          secondsFrequency,
                          NotificationProducerResource resource )
   throws Exception
   {
      this.m_frequencySeconds                = secondsFrequency;
      this.m_minutesDelay                    = minutesDelay;
      if ( resource == null )
      {
         throw new Exception( "Notification Producer is null" );
      }

      this.m_notificationProducerResource = resource;
   }

   /**
    * DOCUMENT_ME
    *
    * @param frequencySeconds DOCUMENT_ME
    */
   public void setFrequencySeconds( int frequencySeconds )
   {
      m_frequencySeconds = frequencySeconds;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public int getFrequencySeconds(  )
   {
      return m_frequencySeconds;
   }

   /**
    * DOCUMENT_ME
    *
    * @param minutesDelay DOCUMENT_ME
    */
   public void setMinutesDelay( int minutesDelay )
   {
      m_minutesDelay = minutesDelay;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public int getMinutesDelay(  )
   {
      return m_minutesDelay;
   }

   /** Starts the EventScheduler event activity
    *
    */
   public void start(  )
   {
      if ( m_timer != null )
      {
         m_timer.cancel(  );
      }

      m_timer = new Timer(  );
      m_timer.scheduleAtFixedRate( new EventTask(  ),
                                   getSatrtDate(  ),
                                   m_frequencySeconds * 1000 );
   }

   /** Starts the EventScheduler event activity with specified delay and frequency
    *
    */
   public void start( int minutesDelay,
                      int secondsFrequency )
   {
      m_minutesDelay        = minutesDelay;
      m_frequencySeconds    = secondsFrequency;

      if ( m_timer != null )
      {
         m_timer.cancel(  );
      }

      m_timer = new Timer(  );
      m_timer.scheduleAtFixedRate( new EventTask(  ),
                                   getSatrtDate(  ),
                                   m_frequencySeconds * 1000 );
   }

   /**
    * Stops the EventScheduler activity
    *
    */
   public void stop(  )
   {
      m_timer.cancel(  );
      m_timer = null;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   protected Date getSatrtDate(  )
   {
      Calendar now = new GregorianCalendar(  );
      now.add( Calendar.MINUTE, m_minutesDelay );
      return now.getTime(  );
   }

   /**
    * DOCUMENT_ME
    *
    * @author Kinga Dziembowski
    */
   protected class EventTask
      extends TimerTask
   {
      /** DOCUMENT_ME */
      protected String m_correlationId = null;

      /**
       * DOCUMENT_ME
       */
      public void run(  )
      {
         publish(  );
      }

      /** The management event is composed. The custom CorrelationId is added to the spec defined managementEvent
       *
       * @param firstEvent true - low disk space event, false - disk space OK
       */
      void buildAndPublishEvent( boolean firstEvent )
      {
         try
         {
            TopicSpaceSet aSet = m_notificationProducerResource.getTopicSpaceSet(  );
            TopicSpace    aTopicSpace = aSet.getTopicSpace( MuwsConstants.NSURI_MUWS_PART2_TOPICS );
            ManagementEventTopic operationalStatus =  (ManagementEventTopic) aTopicSpace.getTopic( OperationalStatusCapability.TOPIC_NAME );
            org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.LangString lStringC = LangString.Factory.newInstance(  );
            lStringC.setLang( "en" );
            if ( operationalStatus == null )
            {
               throw new Exception( "The topic is not set" );
            }

            Situation aSituation = new SituationImpl( new CategoryImpl( MuwsConstants.SITUATION_AVAILABILITY ) );

            if ( firstEvent )
            {
               aSituation.setSeverity( (short) 4 );
               lStringC.setStringValue( "Available disk space is too low" );
               aSituation.setSuccessDisposition( Boolean.FALSE );
               m_correlationId = ((UUID) IdentifierUtils.UUID_VERSION_FOUR_GENERATOR.nextIdentifier()).toString();
            }
            else
            {
               aSituation.setSeverity( (short) 1 );
               lStringC.setStringValue( "Available disk space is OK" );
               aSituation.setSuccessDisposition( Boolean.TRUE );
            }

            LangStringImpl lstring = new LangStringImpl( lStringC );
            aSituation.setMessage( lstring );
            XmlBeansManagementEvent event = new XmlBeansManagementEvent( aSituation );

            // set resource Reporter and source  ResourceId
            ResourcePropertySet propSet      = m_notificationProducerResource.getResourcePropertySet(  );
            ResourceProperty    resourceId   = propSet.get( ApplicationPropertyQNames.RESOURCEID );
            String              Id           = null;
            Iterator            propElemIter = resourceId.iterator(  );
            while ( propElemIter.hasNext(  ) )
            {
               XmlObject propElem = (XmlObject) propElemIter.next(  );
               if ( propElem instanceof XmlAnyUriImpl )
               {
                  XmlAnyUriImpl aUri = (XmlAnyUriImpl) propElem;
                  Id = aUri.getStringValue(  );
                  event.setReporterResourceId( Id );
                  event.setSourceResourceId( Id );
               }
               else
               {
                  throw new Exception( " ResourceId property should be instanceof  XmlAnyUriImpl" );
               }
            }

            //set custom CorrelationId
            Object[]              corId = new Object[1];
            CorrelationIdDocument id = CorrelationIdDocument.Factory.newInstance(  );
            id.setCorrelationId( m_correlationId );
            corId[0] = id;
            event.setAny( corId );

            // Topic publish event to the subscribers
  
            operationalStatus.publish( event );

         }
         catch ( Exception e )
         {
            LOG.info( "Error: OperationalEvent " + e.toString(  ) );
         }
      }

      private void publish(  )
      {
         buildAndPublishEvent( firstEvent );
         LOG.info( "Event " + firstEvent );
         firstEvent = !firstEvent;

         // Start to emmit event...
      }
   }
}
TOP

Related Classes of org.apache.ws.muse.example.application.EventScheduler$EventTask

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.