/*=============================================================================*
* 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.resource.impl;
import commonj.timers.Timer;
import commonj.timers.TimerListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.resource.Resource;
import org.apache.ws.resource.ResourceException;
import org.apache.ws.resource.ResourceHome;
import org.apache.ws.resource.ResourceKey;
import org.apache.ws.resource.i18n.Keys;
import org.apache.ws.resource.i18n.MessagesImpl;
import org.apache.ws.resource.lifetime.ScheduledResourceTerminationResource;
import org.apache.ws.util.i18n.Messages;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
/**
* LOG-DONE
* DOCUMENT_ME
*/
public class ResourceSweeper
implements TimerListener
{
/**
* DOCUMENT_ME
*/
private static final Log LOG = LogFactory.getLog( ResourceSweeper.class.getName( ) );
public static final Messages MSG = MessagesImpl.getInstance();
/**
* DOCUMENT_ME
*/
protected Map m_resources;
/**
* DOCUMENT_ME
*/
protected ResourceHome m_home;
/**
* @param resources must be synchronized map
*/
public ResourceSweeper( ResourceHome home,
Map resources )
{
m_home = home;
m_resources = resources;
}
/**
* DOCUMENT_ME
*
* @param resource DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public static boolean isExpired( Resource resource )
{
if ( !( resource instanceof ScheduledResourceTerminationResource ) )
{
return false;
}
return isExpired( (ScheduledResourceTerminationResource) resource,
Calendar.getInstance( ) );
}
/**
* DOCUMENT_ME
*
* @param resource DOCUMENT_ME
* @param currentTime DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public static boolean isExpired( ScheduledResourceTerminationResource resource,
Calendar currentTime )
{
Calendar terminationTime = resource.getTerminationTime( );
return ( ( terminationTime != null ) && terminationTime.before( currentTime ) );
}
/**
* DOCUMENT_ME
*
* @param timer DOCUMENT_ME
*/
public void timerExpired( Timer timer )
{
LOG.debug( MSG.getMessage( Keys.CLEANING_EXPIRED_RESOURCES) );
Calendar currentTime = Calendar.getInstance( );
ResourceKey key;
Resource resource;
LinkedList list = new LinkedList( );
synchronized ( m_resources )
{
Iterator keyIterator = m_resources.keySet( ).iterator( );
while ( keyIterator.hasNext( ) )
{
key = (ResourceKey) keyIterator.next( );
try
{
resource = getResource( key );
if ( ( resource != null ) && isExpired( resource, currentTime ) )
{
list.add( key );
}
}
catch ( ResourceException re )
{
LOG.error( re );
}
}
}
Iterator iter = list.iterator( );
while ( iter.hasNext( ) )
{
key = (ResourceKey) iter.next( );
try
{
m_home.remove( key );
}
catch ( ResourceException re )
{
LOG.error( re );
}
}
}
/**
* DOCUMENT_ME
*
* @param resource DOCUMENT_ME
* @param currentTime DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
protected boolean isExpired( Resource resource,
Calendar currentTime )
{
if ( !( resource instanceof ScheduledResourceTerminationResource ) )
{
return false;
}
return isExpired( (ScheduledResourceTerminationResource) resource, currentTime );
}
/**
* DOCUMENT_ME
*
* @param key DOCUMENT_ME
*
* @return DOCUMENT_ME
*
* @throws ResourceException DOCUMENT_ME
*/
protected Resource getResource( ResourceKey key )
throws ResourceException
{
return m_home.find( key );
}
}