[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jboss-cvs-commits
Subject:    [jboss-cvs] contrib/varia/src/main/org/jboss/varia/scheduler XMLScheduleProvider.java AbstractSchedu
From:       Andreas Schaefer <schaefera () users ! sourceforge ! net>
Date:       2002-07-31 4:08:09
[Download RAW message or body]

  User: schaefera
  Date: 02/07/30 21:08:09

  Modified:    varia/src/main/org/jboss/varia/scheduler
                        AbstractScheduleProvider.java
                        DBScheduleProvider.java ScheduleManager.java
  Added:       varia/src/main/org/jboss/varia/scheduler
                        XMLScheduleProvider.java
  Log:
  Add the new XML based Schedule Provider allowing to add several schedules
  at once administred by the MBean configuration.
  
  Revision  Changes    Path
  1.3       +4 -4      \
contrib/varia/src/main/org/jboss/varia/scheduler/AbstractScheduleProvider.java  
  Index: AbstractScheduleProvider.java
  ===================================================================
  RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/scheduler/AbstractScheduleProvider.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractScheduleProvider.java	29 Jul 2002 05:02:02 -0000	1.2
  +++ AbstractScheduleProvider.java	31 Jul 2002 04:08:08 -0000	1.3
  @@ -23,7 +23,7 @@
    *            extends="org.jboss.system.ServiceMBean"
    *
    * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public abstract class AbstractScheduleProvider
      extends ServiceMBeanSupport
  @@ -116,7 +116,7 @@
      ) throws
         JMException
      {
  -      log.info( "addScheduler(), start date: " + pStart + ", period: " + pPeriod + \
", repetitions: " + pRepetitions );  +//AS      log.info( "addScheduler(), start \
date: " + pStart + ", period: " + pPeriod + ", repetitions: " + pRepetitions );  \
return ( (Integer) server.invoke(  mScheduleManagerName,
            "addSchedule",
  @@ -174,7 +174,7 @@
      protected void startService()
           throws Exception
      {
  -      log.info( "startService(), call registerProvider()" );
  +//AS      log.info( "startService(), call registerProvider(), service name: " + \
serviceName );  server.invoke(
            mScheduleManagerName,
            "registerProvider",
  @@ -195,7 +195,7 @@
       **/
      protected void stopService() {
         try {
  -         log.info( "stopService(), call unregisterProvider()" );
  +//AS         log.info( "stopService(), call unregisterProvider()" );
            server.invoke(
               mScheduleManagerName,
               "unregisterProvider",
  
  
  
  1.3       +2 -1      \
contrib/varia/src/main/org/jboss/varia/scheduler/DBScheduleProvider.java  
  Index: DBScheduleProvider.java
  ===================================================================
  RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/scheduler/DBScheduleProvider.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DBScheduleProvider.java	29 Jul 2002 05:33:38 -0000	1.2
  +++ DBScheduleProvider.java	31 Jul 2002 04:08:08 -0000	1.3
  @@ -51,13 +51,14 @@
    * <li>any full qualified Class name which the Scheduler will be set a "null" \
                value
    *     for it</li>
    * </ul>
  + * The "Period" is an long value greater than 0.
    * The "Repetitions" can be set to "-1" which means unlimited repetitions.
    *
    * @jmx:mbean name="jboss:service=DBScheduleProvider"
    *            extends="org.jboss.varia.scheduler.AbstractScheduleProviderMBean"
    *
    * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public class DBScheduleProvider
      extends AbstractScheduleProvider
  
  
  
  1.2       +7 -6      \
contrib/varia/src/main/org/jboss/varia/scheduler/ScheduleManager.java  
  Index: ScheduleManager.java
  ===================================================================
  RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/scheduler/ScheduleManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ScheduleManager.java	28 Jul 2002 21:08:28 -0000	1.1
  +++ ScheduleManager.java	31 Jul 2002 04:08:08 -0000	1.2
  @@ -36,7 +36,7 @@
    *            extends="org.jboss.system.ServiceMBean"
    *
    * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class ScheduleManager
      extends ServiceMBeanSupport
  @@ -181,6 +181,7 @@
         // Initiate the Provider to provide schedules
         try {
            ObjectName lProviderName = new ObjectName( pProviderObjectName );
  +//AS         log.info( "Start Providing on: " + lProviderName + ", based on: " + \
pProviderObjectName );  server.invoke(
               lProviderName,
               "startProviding",
  @@ -252,7 +253,7 @@
         long pPeriod,
         int pRepetitions
      ) {
  -      log.info( "addScheduler()" );
  +//AS      log.info( "addScheduler()" );
         ScheduleInstance lInstance = new ScheduleInstance(
            pProvider,
            pTarget,
  @@ -649,13 +650,13 @@
               // If then first check if a repetition is in the future
               long lNow = new Date().getTime() + 100;
               int lSkipRepeats = (int) ( ( lNow - mStartDate.getTime() ) / mPeriod ) \
                + 1;
  -//            log.debug( "Old start date: " + mStartDate + ", now: " + new Date( \
                lNow ) + ", Skip repeats: " + lSkipRepeats );
  -            log.info( "Old start date: " + mStartDate + ", now: " + new Date( lNow \
) + ", Skip repeats: " + lSkipRepeats );  +            log.debug( "Old start date: " \
+ mStartDate + ", now: " + new Date( lNow ) + ", Skip repeats: " + lSkipRepeats );  \
+//AS            log.info( "Old start date: " + mStartDate + ", now: " + new Date( \
lNow ) + ", Skip repeats: " + lSkipRepeats );  if( mInitialRepetitions > 0 ) {
                  // If not infinit loop
                  if( lSkipRepeats >= mInitialRepetitions ) {
                     // No repetition left -> exit
  -                  log.info( "No repetitions left because start date is in the past \
and could " +  +                  log.warn( "No repetitions left because start date \
                is in the past and could " +
                        "not be reached by Initial Repetitions * Schedule Period" );
                     return;
                  } else {
  @@ -675,7 +676,7 @@
               mRemainingRepetitions = mInitialRepetitions;
            }
   //         log.info( "start(), add Notification to Timer: " + mTimer );
  -         log.info( "start(), start date: " + lStartDate + ", period: " + mPeriod + \
", repetitions: " + mRemainingRepetitions );  +//         log.info( "start(), start \
date: " + lStartDate + ", period: " + mPeriod + ", repetitions: " + \
mRemainingRepetitions );  mNotificationID = ( (Integer) getServer().invoke(
               mTimer,
               "addNotification",
  
  
  
  1.1                  \
contrib/varia/src/main/org/jboss/varia/scheduler/XMLScheduleProvider.java  
  Index: XMLScheduleProvider.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.varia.scheduler;
  
  import java.security.InvalidParameterException;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.Iterator;
  import java.util.StringTokenizer;
  
  import javax.management.JMException;
  import javax.management.MalformedObjectNameException;
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.w3c.dom.Text;
  
  /**
   * This Provider get its Scheduler from a XML configuration string
   * allowing the administrator to add several Schedules with one
   * provider.
   * The "Schedules" properties has to look like:
   * <schedules>
   *    <schedule>
   *       <target-mbean-name/>
   *       <target-method-name/>
   *       <target-method-signature/>
   *       <start-date/>
   *       <period/>
   *       <repetitions/>
   *    </schedule>
   * </schedules>
   *
   * @jmx:mbean name="jboss:service=XMLScheduleProvider"
   *            extends="org.jboss.varia.scheduler.AbstractScheduleProviderMBean"
   *
   * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
   * @version $Revision: 1.1 $
   */
  public class XMLScheduleProvider
     extends AbstractScheduleProvider
     implements XMLScheduleProviderMBean
  {
  
     // -------------------------------------------------------------------------
     // Constants
     // -------------------------------------------------------------------------
     
     // -------------------------------------------------------------------------
     // Members
     // -------------------------------------------------------------------------
     
     private Element mSchedules;
     private DateFormat mDateFormatter;
     
     /** The ID of the Schedule used later to remove it later **/
     private ArrayList mIDList = new ArrayList();
     
     // -------------------------------------------------------------------------
     // Constructors
     // -------------------------------------------------------------------------
     
     /**
      * Default (no-args) Constructor
      **/
     public XMLScheduleProvider()
     {
     }
     
     // -------------------------------------------------------------------------
     // SchedulerMBean Methods
     // -------------------------------------------------------------------------
     
     /**
      * @return XML configuration attribute
      *
      * @jmx:managed-operation
      **/
     public Element getSchedules() {
        return mSchedules;
     }
     
     /**
      * Sets the XML configuration attribute
      *
      * @jmx:managed-operation
      **/
     public void setSchedules( final Element pSchedules ) {
        mSchedules = pSchedules;
     }
     
     /**
      * Add the Schedule to the Schedule Manager
      *
      * @jmx:managed-operation
      **/
     public void startProviding()
        throws Exception
     {
        try {
        NodeList lSchedules = mSchedules.getElementsByTagName( "schedule" );
        for( int i = 0; i < lSchedules.getLength(); i++ ) {
           Node lSchedule = lSchedules.item( i );
           NodeList lAttributes = lSchedule.getChildNodes();
           Text lItem = getNode( lAttributes, "target-mbean-name" );
           if( lItem == null ) {
              log.error( "No 'target-mbean-name' is specified therefore this Schedule \
is ignored" );  continue;
           }
           log.info( "Got 'target-mbean-name' element: " + lItem + ", node value: " + \
lItem.getData() + lItem.getChildNodes() );  String lTarget = lItem.getData();
           lItem = getNode( lAttributes, "target-method-name" );
           if( lItem == null ) {
              log.error( "No 'target-method-name' is specified therefore this \
Schedule is ignored" );  continue;
           }
           String lMethodName = lItem.getData();
           lItem = getNode( lAttributes, "target-method-signature" );
           if( lItem == null ) {
              log.error( "No 'target-method-signature' is specified therefore this \
Schedule is ignored" );  continue;
           }
           String lMethodSignature = lItem.getData();
           lItem = getNode( lAttributes, "start-date" );
           if( lItem == null ) {
              log.error( "No 'start-date' is specified therefore this Schedule is \
ignored" );  continue;
           }
           String lStartDate = lItem.getData();
           lItem = getNode( lAttributes, "period" );
           if( lItem == null ) {
              log.error( "No 'period' is specified therefore this Schedule is \
ignored" );  continue;
           }
           String lPeriod = lItem.getData();
           lItem = getNode( lAttributes, "repetitions" );
           if( lItem == null ) {
              log.error( "No 'repetitions' is specified therefore this Schedule is \
ignored" );  continue;
           }
           String lRepeptions = lItem.getData();
           try {
              // Add Schedule
              int lID = addSchedule(
                 new ObjectName( lTarget ),
                 lMethodName,
                 getSignature( lMethodSignature ),
                 getStartDate( lStartDate ),
                 new Long( lPeriod ).longValue(),
                 new Integer( lRepeptions ).intValue()
              );
              mIDList.add( new Integer( lID ) );
           }
           catch( NumberFormatException nfe ) {
              log.error( "Could not convert a number", nfe );
           }
        }
        } catch( Exception e ) {
           e.printStackTrace();
           throw e;
        }
     }
     
     /**
      * Loops of the given Node List and looks for the Element
      * with the given Node Name
      *
      * @param pNodeList The list of nodes to search through
      * @param pName The name of the node to look for
      *
      * @return Element if found otherwise null
      **/
     protected Text getNode( NodeList pList, String pName ) {
        if( pList == null ) {
           return null;
        }
        for( int i = 0; i < pList.getLength(); i++ ) {
           Node lNode = pList.item(i);
           switch( lNode.getNodeType() ) {
              case Node.ELEMENT_NODE:
                 Element lChild = (Element) lNode;
                 if( lChild.getNodeName().equals( pName ) ) {
                    return (Text) lChild.getFirstChild();
                 }
           }
        }
        return null;
     }
     
     /**
      * Stops the Provider from providing causing
      * the provider to remove the Schedule
      *
      * @jmx:managed-operation
      */
     public void stopProviding() {
        Iterator i = mIDList.iterator();
        while( i.hasNext() ) {
           Integer lID = (Integer) i.next();
           try {
              removeSchedule( lID.intValue() );
           }
           catch( JMException jme ) {
              log.error( "Could not remove Schedule in stop providing", jme );
           }
        }
     }
     
     /**
      * Converts a string of method arguments (separated by colons) into
      * an array of string
      **/
     protected String[] getSignature( String pMethodSignature )
     {
        if( pMethodSignature == null || "".equals( pMethodSignature.trim() ) ) {
           return new String[ 0 ];
        }
        StringTokenizer lTokenizer = new StringTokenizer( pMethodSignature, "," );
        String[] lReturn = new String[ lTokenizer.countTokens() ];
        int i = 0;
        while( lTokenizer.hasMoreTokens() ) {
           lReturn[ i++ ] = lTokenizer.nextToken().trim();
        }
        return lReturn;
     }
     
     /**
      * Converts the given Data string to a date
      * where not value means 1/1/1970, "NOW" means
      * now (plus a second), an long value means time
      * in milliseconds since 1/1/1970 and a String is
      * a Date string which is intepreted by a Simple
      * Data Formatter.
      */
     protected Date getStartDate( String pStartDate ) {
        pStartDate = pStartDate == null ? "" : pStartDate.trim();
        Date lReturn = null;
        if( pStartDate.equals( "" ) ) {
           lReturn = new Date( 0 );
        } else
        if( pStartDate.equals( "NOW" ) ) {
           lReturn = new Date( new Date().getTime() + 1000 );
        } else {
           try {
              long lDate = new Long( pStartDate ).longValue();
              lReturn = new Date( lDate );
           }
           catch( Exception e ) {
              try {
                 if( mDateFormatter == null ) {
                    mDateFormatter = new SimpleDateFormat();
                 }
                 lReturn = mDateFormatter.parse( pStartDate );
              }
              catch( Exception e2 ) {
                 log.error( "Could not parse given date string: " + pStartDate, e2 );
                 throw new InvalidParameterException( "Schedulable Date is not of \
correct format" );  }
           }
        }
        log.debug( "Initial Start Date is set to: " + lReturn );
        
        return lReturn;
     }
     
     // -------------------------------------------------------------------------
     // Methods
     // -------------------------------------------------------------------------
     
     public ObjectName getObjectName(
        MBeanServer pServer,
        ObjectName pName
     )
        throws MalformedObjectNameException
     {
        return pName == null ? XMLScheduleProviderMBean.OBJECT_NAME : pName;
     }
  }
  
  
  


-------------------------------------------------------
This sf.net email is sponsored by: Dice - The leading online job board
for high-tech professionals. Search and apply for tech jobs today!
http://seeker.dice.com/seeker.epl?rel_code=31
_______________________________________________
jboss-cvs-commits mailing list
jboss-cvs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic