/*
* Created on Apr 3, 2003
*/
package net.sf.jportlet.portlet.application;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jportlet.impl.PortletResponseImpl;
import net.sf.jportlet.portlet.Portlet;
import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.PortletRequest;
import net.sf.jportlet.portlet.PortletResponse;
import net.sf.jportlet.portlet.User;
import net.sf.jportlet.portlet.descriptor.CacheDescriptor;
import net.sf.jportlet.portlet.descriptor.PortletDescriptor;
import net.sf.jportlet.portlet.event.ActionEvent;
import net.sf.jportlet.service.cache.CacheRegion;
import net.sf.jportlet.service.cache.CacheService;
import net.sf.jportlet.service.cache.Cacheable;
import net.sf.jportlet.service.cache.CacheableImpl;
/**
* This interceptor get the portlet content from the cache.
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*
* @deprecated This interceptor is no longer used
*/
public class CacheInterceptor
implements Interceptor
{
//~ Static fields/initializers ---------------------------------------------
private static final Log __log = LogFactory.getLog( CacheInterceptor.class );
public static final String ANONYMOUS_ID = CacheInterceptor.class.getName( ) + "#Anonymous";
//~ Instance fields --------------------------------------------------------
private CacheService _cacheService;
//~ Methods ----------------------------------------------------------------
/**
* @see net.sf.jportlet.portlet.application.Interceptor#adterActionPerformed(net.sf.jportlet.portlet.application.PortletProxy, net.sf.jportlet.portlet.PortletRequest, net.sf.jportlet.portlet.PortletResponse)
*/
public void afterActionPerformed( PortletProxy proxy,
ActionEvent event )
throws PortletException {}
/**
* @see net.sf.jportlet.portlet.application.Interceptor#afterService(net.sf.jportlet.portlet.application.PortletProxy, net.sf.jportlet.portlet.PortletRequest, net.sf.jportlet.portlet.PortletResponse)
*/
public void afterService( PortletProxy proxy,
PortletRequest request,
PortletResponse response )
throws PortletException,
IOException
{
boolean debug = __log.isDebugEnabled( );
String id = getId( proxy, request, response );
if ( id != null )
{
PortletResponseImpl resp = ( PortletResponseImpl ) response;
PortletDescriptor descr = proxy.getDescriptor( );
CacheDescriptor cache = descr.getCacheDescriptor( request.getMode( ) );
CacheRegion region = getCacheService( proxy ).getRegion( descr.getName( ), true );
CacheableImpl cacheable = new CacheableImpl( id, resp.getBuffer( ).toString( ), 1000 * cache.getExpires( ) );
if ( debug )
{
__log.debug( "Caching portlet content:" + id );
}
region.put( cacheable );
}
}
/**
* @see net.sf.jportlet.portlet.application.Interceptor#beforeActionPerformed(net.sf.jportlet.portlet.application.PortletProxy, net.sf.jportlet.portlet.PortletRequest, net.sf.jportlet.portlet.PortletResponse)
*/
public int beforeActionPerformed( PortletProxy proxy,
ActionEvent event )
throws PortletException
{
return Interceptor.CONTINUE;
}
/**
* @see net.sf.jportlet.portlet.application.Interceptor#beforService(net.sf.jportlet.portlet.application.PortletProxy, net.sf.jportlet.portlet.PortletRequest, net.sf.jportlet.portlet.PortletResponse)
*/
public int beforeService( PortletProxy proxy,
PortletRequest request,
PortletResponse response )
throws PortletException,
IOException
{
boolean debug = __log.isDebugEnabled( );
String id = getId( proxy, request, response );
if ( debug )
{
__log.debug( "cacheable.id=" + id );
}
if ( id != null )
{
/* Get the cacheable object */
CacheRegion region = getCacheService( proxy ).getRegion( proxy.getDescriptor( ).getName( ), true );
Cacheable cacheable = region.get( id );
if ( cacheable != null )
{
if ( !proxy.isDirty( request ) )
{
if ( debug )
{
__log.debug( "...writing cached content into the portlet response" );
}
Object data = cacheable.getData( );
PortletResponseImpl resp = ( PortletResponseImpl ) response;
resp.getBuffer( ).append( data );
return Interceptor.SKIP;
}
else
{
if ( debug )
{
__log.debug( "...portlet dirty. expiring the portlet content" );
}
cacheable.expire( );
}
}
}
return Interceptor.CONTINUE;
}
public CacheService getCacheService( PortletProxy proxy )
throws PortletException
{
if ( _cacheService == null )
{
synchronized ( CacheInterceptor.class )
{
_cacheService = ( CacheService ) proxy.getPortletContext( ).getService( CacheService.NAME );
}
}
return _cacheService;
}
public String getId( PortletProxy proxy,
PortletRequest request,
PortletResponse response )
{
/* No caching */
StringBuffer id = new StringBuffer( );
PortletDescriptor descr = proxy.getDescriptor( );
Portlet.Mode mode = request.getMode( );
CacheDescriptor cache = descr.getCacheDescriptor( mode );
if ( ( cache == null ) || ( cache.getExpires( ) == 0 ) )
{
return null;
}
id.append( '/' ).append( mode ).append( '/' ).append( request.getWindow( ).getState( ) );
if ( !cache.isShared( ) )
{
User usr = request.getUser( );
if ( usr == null )
{
id.append( '/' ).append( ANONYMOUS_ID );
}
else
{
id.append( '/' ).append( usr.getId( ) );
}
}
return id.toString( );
}
}