jtaylor     02/05/17 19:11:31

  Modified:    .        build.xml project.properties project.xml
               src/experimental/org/apache/jcs/auxiliary/lateral/http/server
                        LateralCacheServletReciever.java
               src/java/org/apache/jcs/auxiliary/lateral/socket/tcp
                        LateralTCPListener.java
  Added:       src/conf JCSAdminServlet.velocity.properties
               src/java/org/apache/jcs/servlet JCSAdminServlet.java
                        JCSAdminServletDefault.vm
  Log:
  Reintroducing the Servlet interface to the cache. I rewrote it using velocity
  which makes the code a little cleaner. I still would like to add a detail page
  for viewing the items in the cache and removing one (or perhaps more than one).
  Seems like a decent start.
  
  Revision  Changes    Path
  1.8       +6 -0      jakarta-turbine-jcs/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/build.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- build.xml 15 May 2002 20:11:29 -0000      1.7
  +++ build.xml 18 May 2002 02:11:31 -0000      1.8
  @@ -27,6 +27,12 @@
   
     </target>
   
  +  <target name="jcs:copy-classpath-vm">
  +    <copy todir="${maven.build.dest}">
  +        <fileset dir="src/java" includes="**/*.vm"/>
  +    </copy>
  +  </target>
  +
     <!-- The targets people are used to -->
   
     <target name="clean" depends="maven:compile"/>
  
  
  
  1.2       +3 -0      jakarta-turbine-jcs/project.properties
  
  Index: project.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/project.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- project.properties        17 May 2002 14:12:51 -0000      1.1
  +++ project.properties        18 May 2002 02:11:31 -0000      1.2
  @@ -1,2 +1,5 @@
   maven.test.callback.pre-test.buildFile = build.xml
   maven.test.callback.pre-test.buildTarget = jcs:test-prepare
  +
  +maven.core.callback.post-compile.buildFile = build.xml
  +maven.core.callback.post-compile.buildTarget = jcs:copy-classpath-vm
  
  
  
  1.8       +8 -0      jakarta-turbine-jcs/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/project.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- project.xml       15 May 2002 20:00:39 -0000      1.7
  +++ project.xml       18 May 2002 02:11:31 -0000      1.8
  @@ -111,6 +111,14 @@
         <version>???</version>
         <jar>tomcat3.2.1.jar</jar>
       </dependency>
  +
  +    <dependency>
  +        <name>jakarta-velocity</name>
  +        <type>required</type>
  +        <version>1.3-dev</version>
  +        <jar>velocity-1.3-dev.jar</jar>
  +    </dependency>
  +
       <!-- Required for the server in JCS -->
   
       <dependency>
  
  
  
  1.1                  jakarta-turbine-jcs/src/conf/JCSAdminServlet.velocity.properties
  
  Index: JCSAdminServlet.velocity.properties
  ===================================================================
  runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
  runtime.log.logsystem.log4j.category=org.apache.velocity.Velocity
  
  resource.loader = classpath
  classpath.resource.loader.class = 
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  
  
  1.2       +1 -1      
jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/lateral/http/server/LateralCacheServletReciever.java
  
  Index: LateralCacheServletReciever.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-jcs/src/experimental/org/apache/jcs/auxiliary/lateral/http/server/LateralCacheServletReciever.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LateralCacheServletReciever.java  17 May 2002 14:02:39 -0000      1.1
  +++ LateralCacheServletReciever.java  18 May 2002 02:11:31 -0000      1.2
  @@ -94,7 +94,7 @@
               try
               {
                   // need to set as from lateral
  -                cache.add( item );
  +                cache.localUpdate( item );
               }
               catch ( Exception e )
               {
  
  
  
  1.2       +2 -4      
jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
  
  Index: LateralTCPListener.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LateralTCPListener.java   17 May 2002 14:22:16 -0000      1.1
  +++ LateralTCPListener.java   18 May 2002 02:11:31 -0000      1.2
  @@ -70,7 +70,6 @@
   import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
   import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
   import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheListener;
  -import org.apache.jcs.engine.CacheConstants;
   import org.apache.jcs.engine.behavior.ICacheElement;
   import org.apache.jcs.engine.behavior.ICompositeCache;
   import org.apache.jcs.engine.control.CacheHub;
  @@ -79,7 +78,7 @@
    * Listens for connections from other TCP lateral caches and handles them.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Aaron Smuts</a>
  - * @version $Id: LateralTCPListener.java,v 1.1 2002/05/17 14:22:16 jtaylor Exp $
  + * @version $Id: LateralTCPListener.java,v 1.2 2002/05/18 02:11:31 jtaylor Exp $
    */
   public class LateralTCPListener
       implements ILateralCacheListener, Serializable
  @@ -96,7 +95,7 @@
       /** Map of available instances, keyed by port */
       protected final static HashMap instances = new HashMap();
   
  -    // ---------- instance variables
  +    // ----------------------------------------------------- instance variables
   
       /** The socket listener */
       private ListenerThread receiver;
  @@ -352,7 +351,6 @@
               try
               {
                   ois = new ObjectInputStream( socket.getInputStream() );
  -                ;
               }
               catch ( Exception e )
               {
  
  
  
  1.1                  
jakarta-turbine-jcs/src/java/org/apache/jcs/servlet/JCSAdminServlet.java
  
  Index: JCSAdminServlet.java
  ===================================================================
  package org.apache.jcs.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.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/18 02:11:31 jtaylor Exp $
   */
  public class JCSAdminServlet extends VelocityServlet
  {
      private static final String DEFAULT_TEMPLATE_NAME =
          "/org/apache/jcs/servlet/JCSAdminServletDefault.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";
  
      // 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 CacheHub cacheHub = CacheHub.getInstance();
  
      /** @see VelocityServlet#handleRequest */
      protected Template handleRequest( HttpServletRequest request,
                                        HttpServletResponse response,
                                        Context context )
          throws Exception
      {
          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 ] );
                  }
              }
          }
  
          // Now populate the context
  
          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 );
          }
  
          context.put( "cacheInfoRecords", cacheInfo );
  
          return getTemplate( DEFAULT_TEMPLATE_NAME );
      }
  
      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/servlet/JCSAdminServletDefault.vm
  
  Index: JCSAdminServletDefault.vm
  ===================================================================
  <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=clearRegion&cacheName=${record.Name}"> Remove all 
</a> </td>
          </tr>
      #end
  
  </table>
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to