/*=============================================================================*
* 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 );
private static final Messages MSG = MessagesImpl.getInstance( );
private int m_poolSize = 1;
private List m_taskList = new LinkedList( );
private List m_timerPool = new ArrayList( );
private int m_currentTimer = 0;
/**
* Creates a new {@link TimerManagerImpl} object.
*/
public TimerManagerImpl( )
{
for ( int i = 0; i < this.m_poolSize; i++ )
{
m_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.m_poolSize; i < poolSize; i++ )
{
m_timerPool.add( new java.util.Timer( true ) );
}
this.m_poolSize = poolSize;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public int getPoolSize( )
{
return this.m_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.m_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) m_timerPool.get( this.m_currentTimer );
this.m_currentTimer = ( this.m_currentTimer + 1 ) % this.m_poolSize;
Timer newTimer = new TimerImpl( listener, period );
TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
this.m_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) m_timerPool.get( this.m_currentTimer );
this.m_currentTimer = ( this.m_currentTimer + 1 ) % this.m_poolSize;
Timer newTimer = new TimerImpl( listener, 0 );
TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
this.m_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) m_timerPool.get( this.m_currentTimer );
this.m_currentTimer = ( this.m_currentTimer + 1 ) % this.m_poolSize;
Timer newTimer = new TimerImpl( listener, period );
TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
this.m_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) m_timerPool.get( this.m_currentTimer );
this.m_currentTimer = ( this.m_currentTimer + 1 ) % this.m_poolSize;
Timer newTimer = new TimerImpl( listener, 0 );
TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
this.m_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) m_timerPool.get( this.m_currentTimer );
this.m_currentTimer = ( this.m_currentTimer + 1 ) % this.m_poolSize;
Timer newTimer = new TimerImpl( listener, period );
TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
this.m_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) m_timerPool.get( this.m_currentTimer );
this.m_currentTimer = ( this.m_currentTimer + 1 ) % this.m_poolSize;
Timer newTimer = new TimerImpl( listener, period );
TimerTaskImpl task = new TimerTaskImpl( newTimer, this );
this.m_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.m_taskList.iterator( );
while ( taskIterator.hasNext( ) )
{
TimerTaskImpl task = (TimerTaskImpl) taskIterator.next( );
task.stop( );
TimerListener listener = task.getListener( );
if ( listener instanceof StopTimerListener )
{
( (StopTimerListener) listener ).timerStop( task.getTimer( ) );
}
}
this.m_taskList.clear( );
}
/**
* DOCUMENT_ME
*/
public synchronized void suspend( )
{
LOG.debug( MSG.getMessage( Keys.SUSPENDING ) );
Iterator taskIterator = this.m_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.m_taskList.remove( task );
}
}