Package org.apache.ws.util.timer

Source Code of org.apache.ws.util.timer.TimerManagerImpl

/*=============================================================================*
*  Copyright 2004 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.util.timer;

import commonj.timers.StopTimerListener;
import commonj.timers.Timer;
import commonj.timers.TimerListener;
import commonj.timers.TimerManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.util.i18n.Keys;
import org.apache.ws.util.i18n.Messages;
import org.apache.ws.util.i18n.MessagesImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
* LOG-DONE
* DOCUMENT_ME
*
* @author $author$
* @version $Revision: 1.7 $
*/
public class TimerManagerImpl
   implements TimerManager
{
   private static final Log LOG = LogFactory.getLog( TimerManagerImpl.class );
   public static final Messages MSG = MessagesImpl.getInstance();
   private int  poolSize     = 1;
   private List taskList     = new LinkedList(  );
   private List timerPool    = new ArrayList(  );
   private int  currentTimer = 0;

   /**
    * Creates a new {@link TimerManagerImpl} object.
    */
   public TimerManagerImpl(  )
   {
      for ( int i = 0; i < this.poolSize; i++ )
      {
         timerPool.add( new java.util.Timer( true ) );
      }
   }

   /**
    * DOCUMENT_ME
    *
    * @param poolSize DOCUMENT_ME
    */
   public void setPoolSize( int poolSize )
   {
      LOG.debug(MSG.getMessage( Keys.SET_POOL_SIZE, Integer.toString(poolSize)));
      // don't allow shrinking of the pool
      for ( int i = this.poolSize; i < poolSize; i++ )
      {
         timerPool.add( new java.util.Timer( true ) );
      }

      this.poolSize = poolSize;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public int getPoolSize(  )
   {
      return this.poolSize;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public boolean isStopped(  )
   {
      return false;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public boolean isStopping(  )
   {
      return false;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    *
    * @throws IllegalStateException DOCUMENT_ME
    */
   public boolean isSuspended(  )
   throws IllegalStateException
   {
      return false;
   }

   //todo implement new methods!
   public boolean isSuspending(  )
   throws IllegalStateException
   {
      return false;
   }

   /**
    * DOCUMENT_ME
    */
   public synchronized void resume(  )
   {
      LOG.debug(MSG.getMessage( Keys.RESUMING));
      Iterator taskIterator = this.taskList.iterator(  );
      while ( taskIterator.hasNext(  ) )
      {
         ( (TimerTaskImpl) taskIterator.next(  ) ).resume(  );
      }
   }

   /**
    * DOCUMENT_ME
    *
    * @param listener  DOCUMENT_ME
    * @param firstTime DOCUMENT_ME
    * @param period    DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Timer schedule( TimerListener listener,
                          Date          firstTime,
                          long          period )
   {
      java.util.Timer timer = (java.util.Timer) timerPool.get( this.currentTimer );
      this.currentTimer = ( this.currentTimer + 1 ) % this.poolSize;
      Timer                newTimer = new TimerImpl( listener, period );
      TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
      this.taskList.add( task );
      timer.schedule( task, firstTime, period );
      return newTimer;
   }

   /**
    * DOCUMENT_ME
    *
    * @param listener DOCUMENT_ME
    * @param time     DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Timer schedule( TimerListener listener,
                          Date          time )
   {
      java.util.Timer timer = (java.util.Timer) timerPool.get( this.currentTimer );
      this.currentTimer = ( this.currentTimer + 1 ) % this.poolSize;
      Timer                newTimer = new TimerImpl( listener, 0 );
      TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
      this.taskList.add( task );
      timer.schedule( task, time );
      return newTimer;
   }

   /**
    * DOCUMENT_ME
    *
    * @param listener DOCUMENT_ME
    * @param delay    DOCUMENT_ME
    * @param period   DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Timer schedule( TimerListener listener,
                          long          delay,
                          long          period )
   {
      java.util.Timer timer = (java.util.Timer) timerPool.get( this.currentTimer );
      this.currentTimer = ( this.currentTimer + 1 ) % this.poolSize;
      Timer                newTimer = new TimerImpl( listener, period );
      TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
      this.taskList.add( task );
      timer.schedule( task, delay, period );
      return newTimer;
   }

   /**
    * DOCUMENT_ME
    *
    * @param listener DOCUMENT_ME
    * @param delay    DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Timer schedule( TimerListener listener,
                          long          delay )
   {
      java.util.Timer timer = (java.util.Timer) timerPool.get( this.currentTimer );
      this.currentTimer = ( this.currentTimer + 1 ) % this.poolSize;
      Timer                newTimer = new TimerImpl( listener, 0 );
      TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
      this.taskList.add( task );
      timer.schedule( task, delay );
      return newTimer;
   }

   /**
    * DOCUMENT_ME
    *
    * @param listener  DOCUMENT_ME
    * @param firstTime DOCUMENT_ME
    * @param period    DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Timer scheduleAtFixedRate( TimerListener listener,
                                     Date          firstTime,
                                     long          period )
   {
      java.util.Timer timer = (java.util.Timer) timerPool.get( this.currentTimer );
      this.currentTimer = ( this.currentTimer + 1 ) % this.poolSize;
      Timer                newTimer = new TimerImpl( listener, period );
      TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
      this.taskList.add( task );
      timer.scheduleAtFixedRate( task, firstTime, period );
      return newTimer;
   }

   /**
    * DOCUMENT_ME
    *
    * @param listener DOCUMENT_ME
    * @param delay    DOCUMENT_ME
    * @param period   DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Timer scheduleAtFixedRate( TimerListener listener,
                                     long          delay,
                                     long          period )
   {
      java.util.Timer timer = (java.util.Timer) timerPool.get( this.currentTimer );
      this.currentTimer = ( this.currentTimer + 1 ) % this.poolSize;
      Timer                newTimer = new TimerImpl( listener, period );
      TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
      this.taskList.add( task );
      timer.scheduleAtFixedRate( task, delay, period );
      return newTimer;
   }

   /**
    * DOCUMENT_ME
    */
   public synchronized void stop(  )
   {
      LOG.debug(MSG.getMessage( Keys.STOPPING));
      Iterator             taskIterator = this.taskList.iterator(  );
      TimerTaskImpl task;
      TimerListener        listener;

      while ( taskIterator.hasNext(  ) )
      {
         task = (TimerTaskImpl) taskIterator.next(  );
         task.stop(  );
         listener = task.getListener(  );
         if ( listener instanceof StopTimerListener )
         {
            ( (StopTimerListener) listener ).timerStop( task.getTimer(  ) );
         }
      }

      this.taskList.clear(  );
   }

   /**
    * DOCUMENT_ME
    */
   public synchronized void suspend(  )
   {
      LOG.debug(MSG.getMessage( Keys.SUSPENDING));
      Iterator taskIterator = this.taskList.iterator(  );
      while ( taskIterator.hasNext(  ) )
      {
         ( (TimerTaskImpl) taskIterator.next(  ) ).suspend(  );
      }
   }

   /**
    * DOCUMENT_ME
    *
    * @param timeout_ms DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    *
    * @throws InterruptedException     DOCUMENT_ME
    * @throws IllegalArgumentException DOCUMENT_ME
    */
   public boolean waitForStop( long timeout_ms )
   throws InterruptedException,
          IllegalArgumentException
   {
      return false;
   }

   /**
    * DOCUMENT_ME
    *
    * @param timeout_ms DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    *
    * @throws InterruptedException     DOCUMENT_ME
    * @throws IllegalStateException    DOCUMENT_ME
    * @throws IllegalArgumentException DOCUMENT_ME
    */
   public boolean waitForSuspend( long timeout_ms )
   throws InterruptedException,
          IllegalStateException,
          IllegalArgumentException
   {
      return false;
   }

   /**
    * DOCUMENT_ME
    *
    * @param task DOCUMENT_ME
    */
   protected synchronized void removeTask( Object task )
   {
      LOG.debug(MSG.getMessage( Keys.REMOVING_TASK,task.toString()));
      this.taskList.remove( task );
   }
}
TOP

Related Classes of org.apache.ws.util.timer.TimerManagerImpl

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.