asmuts      02/01/14 22:25:35

  Added:       src/java/org/apache/stratum/jcs/auxiliary/lateral/http/server
                        AbstractDeleteCacheServlet.java
                        DeleteCacheServlet.java
                        LateralCacheServletReciever.java
  Log:
  The delete cache servlet provides an administrative view of the cache.
  You can clear one or all the caches, see the number of items in memory, and get hit 
rate stats.
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/auxiliary/lateral/http/server/AbstractDeleteCacheServlet.java
  
  Index: AbstractDeleteCacheServlet.java
  ===================================================================
  package org.apache.stratum.jcs.auxiliary.lateral.http.server;
  
  import org.apache.stratum.jcs.engine.control.*;
  import org.apache.stratum.jcs.engine.group.*;
  import org.apache.stratum.jcs.engine.control.Cache;
  import org.apache.stratum.jcs.engine.behavior.ICache;
  import org.apache.stratum.jcs.access.*;
  import org.apache.stratum.jcs.engine.*;
  import org.apache.stratum.jcs.utils.servlet.*;
  import org.apache.stratum.jcs.utils.log.*;
  import org.apache.stratum.jcs.utils.file.*;
  
  import  javax.servlet.*;
  import  javax.servlet.http.*;
  import  java.io.*;
  import  java.util.*;
  import  java.sql.*;
  
  public abstract class AbstractDeleteCacheServlet extends HttpServlet implements 
SingleThreadModel {
    protected Logger log;
    protected GroupCacheManager cacheMgr;
    private BasicHttpAuthenticator authenticator;
  
    public void init (ServletConfig config) throws ServletException {
      log = LoggerManager.getLogger(this);
  
      // subsclass must initialize the cacheMgr before here.
      authenticator = new BasicHttpAuthenticator( "jcs" );
      super.init(config);
    }
  
    public void service (HttpServletRequest req, HttpServletResponse res) throws 
ServletException,
        IOException {
      if (!authenticator.authenticate(req, res)) {
        return;
      }
      Hashtable params = new Hashtable();
      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      try {
        String paramName;
        String paramValue;
  
        // GET PARAMETERS INTO HASHTABLE
        for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
          paramName = (String)e.nextElement();
          paramValue = req.getParameter(paramName);
          params.put(paramName, paramValue);
          if (log.DEBUG <= log.logLevel) {
            log.debug(paramName + "=" + paramValue);
          }
        }
        String hashtableName = req.getParameter("hashtableName");
        String key = req.getParameter("key");
  
        if (hashtableName == null) {
          hashtableName = req.getParameter("cacheName");
        }
        out.println("<html><body bgcolor=#FFFFFF>");
  
        if (hashtableName != null) {
  
          if (log.DEBUG <= log.logLevel) {
            log.debug("hashtableName = " + hashtableName);
          }
          out.println("(Last hashtableName = " + hashtableName + ")");
  
          if (hashtableName.equals("ALL")) {
  
            // Clear all caches.
            String[] list = cacheMgr.getCacheNames();
            Arrays.sort(list);
  
            for (int i = 0; i < list.length; i++) {
              String name = list[i];
              ICache cache = cacheMgr.getCache(name);
              cache.removeAll();
            }
            out.println("All caches have been cleared!");
          }
          else {
  
            ICache cache = cacheMgr.getCache(hashtableName);
  
            String task = (String)params.get( "task" );
            if ( task == null ) {
              task = "delete";
            }
  
            if ( task.equalsIgnoreCase( "stats" ) ) {
  
               out.println( "<br><br>" );
               out.println( "<b>Stats for " + hashtableName + ":</b><br>" );
               out.println( cache.getStats() );
               out.println( "<br>" );
  
            } else {
  
              // Remove the specified cache.
  
              if (key != null) {
                if (key.toUpperCase().equals("ALL")) {
                  cache.removeAll();
  
                  if (log.DEBUG <= log.logLevel) {
                    log.debug("Removed all elements from " + hashtableName);
                  }
                  out.println("key = " + key);
                }
                else {
                  if (log.DEBUG <= log.logLevel) {
                    log.debug("key = " + key);
                  }
                  out.println("key = " + key);
                  StringTokenizer toke = new StringTokenizer(key, "_");
  
                  while (toke.hasMoreElements()) {
                    String temp = (String)toke.nextElement();
                    cache.remove(key);
  
                    if (log.DEBUG <= log.logLevel) {
                      log.debug("Removed " + temp + " from " + hashtableName);
                    }
                  }
                }
              }
              else {
                out.println("key is null");
              }
  
            } // end is task == delete
  
          }
        }
        else {
          out.println("(No hashTableName specified.)");
        }
  
        // PRINT OUT MENU
        out.println("<br>");
        int antiCacheRandom = (int)(10000.0*Math.random());
        out.println("<a href=?antiCacheRandom=" + antiCacheRandom
            + ">List all caches</a><br>");
        out.println("<br>");
        out.println("<a href=?hashtableName=ALL&key=ALL&antiCacheRandom="
            + antiCacheRandom
            + "><font color=RED>Clear All Cache Regions</font></a><br>");
        out.println("<br>");
        String[] list = cacheMgr.getCacheNames();
        Arrays.sort(list);
        out.println("<div align=CENTER>");
        out.println("<table border=1 width=80%>");
        out.println("<tr bgcolor=#eeeeee><td>Cache Region 
Name</td><td>Size</td><td>Status</td><td>Stats</td>");
        for (int i = 0; i < list.length; i++) {
          String name = list[i];
          out.println("<tr><td><a href=?hashtableName=" + name + 
"&key=ALL&antiCacheRandom="
              + antiCacheRandom + ">" + name + "</a></td>");
          ICache cache = cacheMgr.getCache(name);
          out.println("<td>");
          out.print(cache.getSize());
          out.print("</td><td>");
          int status = cache.getStatus();
          out.print(status == ICache.STATUS_ALIVE ? "ALIVE"
                  : status == ICache.STATUS_DISPOSED ? "DISPOSED"
                  : status == ICache.STATUS_ERROR ? "ERROR"
                  : "UNKNOWN");
          out.print("</td>");
          out.println("<td><a href=?task=stats&hashtableName=" + name + 
"&key=NONE&antiCacheRandom="
              + antiCacheRandom + ">stats</a></td>");
        }
        out.println("</table>");
        out.println("</div>");
      }           //CATCH EXCEPTIONS
      catch (Exception e) {
        log.logEx(e);
        //log.logIt( "hashtableName = " + hashtableName );
        //log.logIt( "key = " + key );
      }           // end try{
      finally {
        String isRedirect = (String)params.get("isRedirect");
        if (isRedirect == null) {
          isRedirect = "N";
        }
        if (log.DEBUG <= log.logLevel) {
          log.debug("isRedirect = " + isRedirect);
        }
        String url;
        if (isRedirect.equals("Y")) {
          url = (String)params.get("url");
          if (log.DEBUG <= log.logLevel) {
            log.debug("url = " + url);
          }
          res.sendRedirect(url);                  // will not work if there's a 
previously sent header
          out.println("<br>\n");
          out.println(" <script>");
          out.println(" location.href='" + url + "'; ");
          out.println(" </script> ");
          out.flush();
        }
        else {
          url = "";
        }
        out.println("</body></html>");
      }
    }             //end service()
  
  } // end class
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/auxiliary/lateral/http/server/DeleteCacheServlet.java
  
  Index: DeleteCacheServlet.java
  ===================================================================
  package org.apache.stratum.jcs.auxiliary.lateral.http.server;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.*;
  import java.util.*;
  import java.sql.*;
  
  import org.apache.stratum.jcs.engine.control.*;
  import org.apache.stratum.jcs.engine.group.*;
  import org.apache.stratum.jcs.engine.control.Cache;
  import org.apache.stratum.jcs.engine.behavior.ICache;
  import org.apache.stratum.jcs.access.*;
  import org.apache.stratum.jcs.engine.*;
  import org.apache.stratum.jcs.utils.log.*;
  import org.apache.stratum.jcs.utils.file.*;
  
  
  public class DeleteCacheServlet extends AbstractDeleteCacheServlet {
  
    /////////////////////////////////////////////////////////
    public void init (ServletConfig config) throws ServletException {
  
      cacheMgr = GroupCacheManagerFactory.getInstance();
  
      super.init(config);
  
    }
  
    /////////////////////////////////////////////////
    public String getServletInfo () {
      return  "DeleteCacheServlet Version 2, extends AbstractDeleteCacheServlet";
    }
  
  } // end class
  
  
  //////////////////////////////////////////////////////////////////////
  //public class DeleteCacheServlet extends HttpServlet implements SingleThreadModel {
  //
  //  boolean debug = true;
  //
  //  String servlet = "/jcs/cache/DeleteCache"; //"/ramraf/DeleteCacheServlet";
  //
  //  Logger log;
  //
  //  CompositeCacheManager cacheMgr;
  //
  ////////////////////////////////////////////////////////
  //  //Initialize global variables
  //  public void init(ServletConfig config) throws ServletException {
  //
  //   log = LoggerManager.getLogger( this );
  //
  //   cacheMgr = CacheManagerFactory.getInstance();
  //
  //   super.init( config );
  //
  //  }// end init
  //
  //
  //
  ///////////////////////////////////////////////////////////
  //  //SERVICE THE REQUEST
  //  public void service(HttpServletRequest req,
  //    HttpServletResponse res)
  //    throws ServletException, IOException {
  //
  //    if ( debug ) {
  //      log.logIt( "The DeleteCacheServlet has been called.\n" );
  //    }
  //
  //    Hashtable params = new Hashtable();
  //
  //            res.setContentType("text/html");
  //            PrintWriter out = res.getWriter();
  //
  //    try {
  //
  //                    String paramName;
  //                    String paramValue;
  //
  //                    // GET PARAMETERS INTO HASHTABLE
  //                    for ( Enumeration e = req.getParameterNames() ; 
e.hasMoreElements() ; ) {
  //                            paramName = (String)e.nextElement();
  //                            paramValue = req.getParameter(paramName);
  //                            params.put(paramName, paramValue);
  //        log.logIt(paramName + "=" + paramValue);
  //                    }
  //
  //              String hashtableName = req.getParameter( "hashtableName" );
  //              String key = req.getParameter( "key" );
  //
  //      if ( hashtableName == null ) {
  //              hashtableName = req.getParameter( "cacheName" );
  //      }
  //
  //      if ( hashtableName != null ) {
  //
  //        if ( debug ) {
  //          log.logIt( "hashtableName = " + hashtableName );
  //        }
  //        out.println( "hashtableName = " + hashtableName );
  //
  //
  //        ICache cache = cacheMgr.getCache( hashtableName );
  //
  //        if ( key != null ) {
  //
  //          if ( key.toUpperCase().equals("ALL") ) {
  //
  //            cache.removeAll();
  //            if ( debug ) {
  //              log.logIt( "Removed all elements from " + hashtableName );
  //            }
  //            out.println( "key = " + key );
  //
  //
  //          } else {
  //
  //            if ( debug ) {
  //              log.logIt( "key = " + key );
  //            }
  //            out.println( "key = " + key );
  //
  //            StringTokenizer toke = new StringTokenizer( key, "_" );
  //            while ( toke.hasMoreElements() ) {
  //
  //              String temp = (String)toke.nextElement();
  //
  //              // this remove shouldn't spawn another lateral remove call
  //              // it is debateable whether is should call a remote remove
  //              // probably not since this should only be called
  //              // non remote caches.  Running the two together could be a mess.
  //              // If a remove call was made on a cahce with both, then the remote
  //              // should have been called.  If it wasn't then the remote is down.
  //              // we'll assume it is down for all.
  //              //cache.remove( key, cache.LATERAL_INVOKATION );
  //              //cache.remove( key, true );
  //              cache.remove( key );
  //              if ( debug ) {
  //                log.logIt( "Removed " + temp + " from " + hashtableName );
  //              }
  //
  //            }
  //
  //          }
  //
  //        } else {
  //          out.println( "key is null" );
  //        }
  //
  //      } else {
  //        out.println( "hashTableName is null" );
  //      }
  //
  //      out.println( "<br>" );
  //
  //                    int antiCacheRandom = (int)(10000.0 * Math.random() );
  //
  //      out.println( "<a href=" + servlet + "?antiCacheRandom=" + antiCacheRandom + 
">List all caches</a><br>" );
  //      out.println( "<br>" );
  //
  //      String[] list = cacheMgr.getCacheNames();
  //
  //      for (int i=0; i < list.length; i++) {
  //        String name = list[i];
  //        out.println( "<a href=" + servlet + "?hashtableName=" + name + 
"&key=ALL&antiCacheRandom=" + antiCacheRandom + ">" + name + "</a><br>" );
  //      }
  //
  //
  //    }//CATCH EXCEPTIONS
  //    catch (Exception e) {
  //      log.logEx( e );
  //      //log.logIt( "hashtableName = " + hashtableName );
  //      //log.logIt( "key = " + key );
  //    }// end try{
  //
  //    finally {
  //                    String isRedirect = (String)params.get( "isRedirect" );
  //      if ( isRedirect == null ) {
  //        isRedirect = "N";
  //      }
  //                    if ( debug ) {
  //                            log.logIt( "isRedirect = " + isRedirect );
  //                    }
  //      String url;
  //                    if ( isRedirect.equals("Y")) {
  //                            url = (String)params.get( "url" );
  //                            if ( debug ) {
  //                                    log.logIt( "url = " + url );
  //                            }
  //                            res.sendRedirect( url );        // will not work if 
there's a previously sent header
  //                            out.println( "<br>\n" );
  //
  //                            out.println( " <script>" );
  //                            out.println( " location.href='" + url + "'; ");
  //                            out.println( " </script> " );
  //                            out.flush();
  //                    } else {
  //        url="";
  //      }
  //    }
  //
  //  } //end service()
  //
  //
  //
  //  /////////////////////////////////////////////////////////////////////////
  //  public void destroy() {
  //
  //    cacheMgr.release();
  //
  //  }
  //
  //
  ////////////////////////////////////////////////////////
  //  //Get Servlet information
  //  public String getServletInfo() {
  //    return "DeleteCacheServlet Information";
  //  }
  //
  //}//end servlet
  
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/auxiliary/lateral/http/server/LateralCacheServletReciever.java
  
  Index: LateralCacheServletReciever.java
  ===================================================================
  package org.apache.stratum.jcs.auxiliary.lateral.http.server;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.*;
  import java.util.*;
  import java.sql.*;
  
  import org.apache.stratum.jcs.access.*;
  import org.apache.stratum.jcs.engine.*;
  import org.apache.stratum.jcs.engine.control.*;
  import org.apache.stratum.jcs.engine.control.Cache;
  import org.apache.stratum.jcs.access.exception.*;
  import org.apache.stratum.jcs.engine.memory.*;
  import org.apache.stratum.jcs.engine.behavior.ICacheElement;
  import org.apache.stratum.jcs.engine.behavior.ICache;
  
  
  import org.apache.stratum.jcs.utils.log.*;
  import org.apache.stratum.jcs.engine.*;
  import org.apache.stratum.jcs.utils.file.*;
  
  ////////////////////////////////////////////////////////////////////////
  /**
   *  @author Aaron Smuts
   *  @version 1.0
   */
  public class LateralCacheServletReciever extends HttpServlet {
  
    boolean debug = true;
    Logger log;
    private static CompositeCacheManager cacheMgr;
  
    //////////////////////////////////////////////////////////
    //Initialize global variables
    public void init(ServletConfig config) throws ServletException {
  
     LoggerManager loggerMgr = LoggerManager.getInstance();
     log = loggerMgr.getLogger( this );
     cacheMgr = CacheManagerFactory.getInstance();
  
     super.init( config );
  
    }// end init
  
  
  
    /////////////////////////////////////////////////////////////
    //SERVICE THE REQUEST
    public void service(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {
  
      log.debug( "The LateralCacheServlet has been called.\n" );
  
      if ( cacheMgr == null ) {
        cacheMgr = CacheManagerFactory.getInstance();
        log.logIt( "cacheMgr was null in LateralCacheServlet" );
      }
  
      ICacheElement item = null;
  
      try {
  
        // Create the ObjectInputStream with
        // the Request InputStream.
        ObjectInputStream ois =
          new ObjectInputStream( request.getInputStream() );
  
                log.debug("after getting input stream and before reading it" );
  
                        // READ POLLOBJ
        item = (ICacheElement)ois.readObject();
        ois.close();
  
      }//CATCH EXCEPTIONS
      catch (ClassNotFoundException cnfe) {
        log.error("error = cnfe " + cnfe.getMessage());
      }// end try{
      catch ( Exception e) {
        log.logEx( e );
      }// end try{
  
  
      if ( item == null ) {
        log.logIt( "item is null in LateralCacheServlet" );
      } else {
                  String hashtableName = item.getCacheName();
                  Serializable key = item.getKey();
        Serializable val = item.getVal();
          if ( debug ) {
          log.logIt("item read in = " + item );
                  log.logIt("item.getKey = " + item.getKey() );
        }
  
        Cache cache = (Cache)cacheMgr.getCache( hashtableName );
        try {
          // need to set as from lateral
          cache.add( item );
        } catch( Exception e ) {}
      }
  
  
      try {
  
        // BEGIN RESPONSE
        response.setContentType("application/octet-stream");
  
        ObjectOutputStream oos =
          new ObjectOutputStream( response.getOutputStream() );
  
        if ( debug ) {
          log.logIt( "Opened output stream.\n" );
        }
  
        String result =  "Completed transfer";
  
        // ECHO THE OBJECT TO THE RESPONSE
        oos.writeObject( result );
        if ( debug ) {
          log.logIt( "Wrote object to output stream.\n" );
        }
        oos.flush();
        if ( debug ) {
          log.logIt( "Flushed output stream.\n" );
        }
        oos.close();
        if ( debug ) {
          log.logIt( "Closed output stream.\n" );
        }
  
      }//CATCH EXCEPTIONS
      catch (Exception e) {
        log.error( e );
      }// end try{
  
      log.flush();
  
    } //end service()
  
  
    /////////////////////////////////////////////////////////////////////////
    public void destroy() {
      cacheMgr.release();
    }
  
    //////////////////////////////////////////////////////////
    //Get Servlet information
    public String getServletInfo() {
      return "LateralCacheServlet v1";
    }
  
  }//end servlet
  
  
  
  

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

Reply via email to