jtaylor 02/05/20 10:05:24
Added: src/java/org/apache/jcs/admin/servlet JCSAdminServlet.java
JCSAdminServletDefault.vm
JCSAdminServletRegionDetail.vm
Removed: src/java/org/apache/jcs/servlet JCSAdminServlet.java
JCSAdminServletDefault.vm
JCSAdminServletRegionDetail.vm
Log:
Moved servlet to admin.servlet
Revision Changes Path
1.1
jakarta-turbine-jcs/src/java/org/apache/jcs/admin/servlet/JCSAdminServlet.java
Index: JCSAdminServlet.java
===================================================================
package org.apache.jcs.admin.servlet;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.jcs.engine.CacheConstants;
import org.apache.jcs.engine.memory.MemoryCache;
import org.apache.jcs.engine.memory.MemoryElementDescriptor;
import org.apache.jcs.engine.behavior.ICache;
import org.apache.jcs.engine.control.CacheHub;
import org.apache.jcs.engine.control.Cache;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
/**
* A servlet which provides HTTP access to JCS. Allows a summary of regions
* to be viewed, and removeAll to be run on individual regions or all regions.
* Also provides the ability to remove items (any number of key arguments can
* be provided with action 'remove'). Should be initialized with a properties
* file that provides at least a classpath resource loader. Since this extends
* VelocityServlet, which uses the singleton model for velocity, it will share
* configuration with any other Velocity in the same JVM.
*
* Initialization in a webapp will look something like this:
* <pre>
* [servlet]
* [servlet-name]JCSAdminServlet[/servlet-name]
* [servlet-class]org.apache.jcs.admin.servlet.JCSAdminServlet[/servlet-class]
* [init-param]
* [param-name]properties[/param-name]
*
[param-value]WEB-INF/conf/JCSAdminServlet.velocity.properties[/param-value]
* [/init-param]
* [/servlet]
* </pre>
*
* FIXME: It would be nice to use the VelocityEngine model so this can be truly
* standalone. Right now if you run it in the same container as, say,
* turbine, turbine must be run first to ensure it's config takes
* precedence.
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Taylor</a>
* @version $Id: JCSAdminServlet.java,v 1.1 2002/05/20 17:05:23 jtaylor Exp $
*/
public class JCSAdminServlet extends VelocityServlet
{
private static final String DEFAULT_TEMPLATE_NAME =
"/org/apache/jcs/admin/servlet/JCSAdminServletDefault.vm";
private static final String REGION_DETAIL_TEMPLATE_NAME =
"/org/apache/jcs/admin/servlet/JCSAdminServletRegionDetail.vm";
// Keys for parameters
private static final String CACHE_NAME_PARAM = "cacheName";
private static final String ACTION_PARAM = "action";
private static final String KEY_PARAM = "key";
private static final String SILENT_PARAM = "silent";
// Possible values for 'action' parameter
private static final String CLEAR_ALL_REGIONS_ACTION = "clearAllRegions";
private static final String CLEAR_REGION_ACTION = "clearRegion";
private static final String REMOVE_ACTION = "remove";
private static final String DETAIL_ACTION = "detail";
private CacheHub cacheHub = CacheHub.getInstance();
/** @see org.apache.velocity.servlet.VelocityServlet#handleRequest */
protected Template handleRequest( HttpServletRequest request,
HttpServletResponse response,
Context context )
throws Exception
{
String templateName = DEFAULT_TEMPLATE_NAME;
// Get cacheName for actions from request (might be null)
String cacheName = request.getParameter( CACHE_NAME_PARAM );
// If an action was provided, handle it
String action = request.getParameter( ACTION_PARAM );
if ( action != null )
{
if ( action.equals( CLEAR_ALL_REGIONS_ACTION ) )
{
clearAllRegions();
}
else if ( action.equals( CLEAR_REGION_ACTION ) )
{
if ( cacheName == null )
{
// Not Allowed
}
else
{
clearRegion( cacheName );
}
}
else if ( action.equals( REMOVE_ACTION ) )
{
String[] keys = request.getParameterValues( KEY_PARAM );
for ( int i = 0; i < keys.length; i++ )
{
removeItem( cacheName, keys[ i ] );
}
templateName = REGION_DETAIL_TEMPLATE_NAME;
}
else if ( action.equals( DETAIL_ACTION ) )
{
templateName = REGION_DETAIL_TEMPLATE_NAME;
}
}
if ( request.getParameter( SILENT_PARAM ) != null )
{
// If silent parameter was passed, no output should be produced.
return null;
}
else
{
// Populate the context based on the template
if ( templateName == REGION_DETAIL_TEMPLATE_NAME )
{
context.put( "cacheName", cacheName );
context.put( "keys", getSortedKeys( cacheName ) );
}
else if ( templateName == DEFAULT_TEMPLATE_NAME )
{
context.put( "cacheInfoRecords", buildCacheInfo() );
}
return getTemplate( templateName );
}
}
private Object[] getSortedKeys( String cacheName )
{
Cache cache = ( Cache ) cacheHub.getCache( cacheName );
Object[] keys = cache.getMemoryCache().getKeyArray();
Arrays.sort( keys );
return keys;
}
private LinkedList buildCacheInfo() throws Exception
{
String[] cacheNames = cacheHub.getCacheNames();
Arrays.sort( cacheNames );
LinkedList cacheInfo = new LinkedList();
CacheRegionInfo regionInfo;
Cache cache;
for ( int i = 0; i < cacheNames.length; i++ )
{
cache = ( Cache ) cacheHub.getCache( cacheNames[ i ] );
regionInfo = new CacheRegionInfo();
regionInfo.name = cache.getCacheName();
regionInfo.itemCount = cache.getSize();
regionInfo.byteCount = getByteCount( cache );
regionInfo.status = statusAsString( cache );
cacheInfo.add( regionInfo );
}
return cacheInfo;
}
public int getByteCount( Cache cache )
throws Exception
{
MemoryCache memCache = cache.getMemoryCache();
Iterator iter = memCache.getIterator();
CountingOnlyOutputStream counter = new CountingOnlyOutputStream();
ObjectOutputStream out = new ObjectOutputStream( counter );
while ( iter.hasNext() )
{
MemoryElementDescriptor node = ( MemoryElementDescriptor )
( ( Map.Entry ) iter.next() ).getValue();
out.writeObject( node.ce.getVal() );
}
// 4 bytes lost for the serialization header
return counter.getCount() - 4;
}
private String statusAsString( ICache cache )
{
int status = cache.getStatus();
return ( status == CacheConstants.STATUS_ALIVE ? "ALIVE"
: status == CacheConstants.STATUS_DISPOSED ? "DISPOSED"
: status == CacheConstants.STATUS_ERROR ? "ERROR"
: "UNKNOWN" );
}
private void clearAllRegions() throws IOException
{
String[] names = cacheHub.getCacheNames();
for ( int i = 0; i < names.length; i++ )
{
cacheHub.getCache( names[ i ] ).removeAll();
}
}
private void clearRegion( String cacheName ) throws IOException
{
cacheHub.getCache( cacheName ).removeAll();
}
private void removeItem( String cacheName, String key ) throws IOException
{
cacheHub.getCache( cacheName ).remove( key );
}
/** Stores info on a cache region for the template */
public class CacheRegionInfo
{
String name = null;
long itemCount = 0;
long byteCount = 0;
String status = null;
public String getName()
{
return name;
}
public long getItemCount()
{
return itemCount;
}
public long getByteCount()
{
return byteCount;
}
public String getStatus()
{
return status;
}
}
/**
* Keeps track of the number of bytes written to it, but doesn't write them
* anywhere.
*/
private static class CountingOnlyOutputStream extends OutputStream
{
private int count;
public void write( byte[] b ) throws IOException
{
count += b.length;
}
public void write( byte[] b, int off, int len ) throws IOException
{
count += len;
}
public void write( int b ) throws IOException
{
count++;
}
/**
* The number of bytes that have passed through this stream.
*/
public int getCount()
{
return this.count;
}
}
}
1.1
jakarta-turbine-jcs/src/java/org/apache/jcs/admin/servlet/JCSAdminServletDefault.vm
Index: JCSAdminServletDefault.vm
===================================================================
<html>
<head><title> JCS Admin Servlet </title></head>
<body>
<h1> Cache Regions </h1>
<p>These are the regions which are currently defined in the cache. 'Items' and
'Bytes' refer to the elements currently in memory (not spooled). You can clear
all items for a region by selecting 'Remove all' next to the desired region
below. You can also <a href="?action=clearAllRegions">Clear all regions</a>
which empties the entire cache.</p>
<table border="1" cellpadding="5" >
<tr>
<th> Cache Name </th>
<th> Items </th>
<th> Bytes </th>
<th> Status </th>
</tr>
#foreach ( $record in $cacheInfoRecords )
<tr>
<td> $record.Name </td>
<td> $record.ItemCount </td>
<td> $record.ByteCount </td>
<td> $record.Status </td>
<td>
<a href="?action=detail&cacheName=${record.Name}"> Detail </a>
| <a href="?action=clearRegion&cacheName=${record.Name}"> Remove all
</a>
</td>
</tr>
#end
</table>
</body>
</html>
1.1
jakarta-turbine-jcs/src/java/org/apache/jcs/admin/servlet/JCSAdminServletRegionDetail.vm
Index: JCSAdminServletRegionDetail.vm
===================================================================
<html>
<head><title> JCS Admin Servlet Region Detail </title></head>
<body>
<h1> Keys for region: $cacheName </h1>
<table border="1" cellpadding="5" >
<tr>
<th> Key </th>
</tr>
#foreach ( $key in $keys )
<tr>
<td> $key </td>
<td> <a href="?action=remove&cacheName=${cacheName}&key=${key}"> Remove
</a> </td>
</tr>
#end
</table>
</body>
</html>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>