View Javadoc

    1   package org.mortbay.jetty.handler.rewrite;
    2
    3   import java.io.IOException;
    4
    5   import javax.servlet.http.HttpServletRequest;
    6   import javax.servlet.http.HttpServletResponse;
    7
    8   import org.mortbay.jetty.AbstractConnector;
    9   import org.mortbay.jetty.Connector;
    10  import org.mortbay.jetty.Request;
    11  import org.mortbay.jetty.Server;
    12  import org.mortbay.log.Log;
    13  import org.mortbay.thread.ThreadPool;
    14
    15  /**
    16   * {@link RuleContainer} for when the {@link ThreadPool} is low on threads
    17   *
    18   * @author joakime
    19   */
    20  public class LowThreadsRuleContainer
    21      extends RuleContainer
    22  {
    23      private ThreadPool _threadPool;
    24
    25      private Server _server;
    26
    27      /* ------------------------------------------------------------------------------- */
    28      public Server getServer()
    29      {
    30          return _server;
    31      }
    32
    33      /* ------------------------------------------------------------------------------- */
    34      public void setServer( Server server )
    35      {
    36          _server = server;
    37      }
    38
    39      /* ------------------------------------------------------------------------------- */
    40      public ThreadPool getThreadPool()
    41      {
    42          return _threadPool;
    43      }
    44
    45      /* ------------------------------------------------------------------------------- */
    46      private ThreadPool getThreadPool( Request request )
    47      {
    48          if ( _threadPool == null )
    49          {
    50              // Lazy load the thread pool from the connector.
    51              Connector connector = request.getConnection().getConnector();
    52              if ( connector instanceof AbstractConnector )
    53              {
    54                  _threadPool = ( (AbstractConnector) connector ).getThreadPool();
    55                  return _threadPool;
    56              }
    57
    58              if ( _server != null )
    59              {
    60                  // Next, try to load the thread pool from the server.
    61                  _threadPool = _server.getThreadPool();
    62                  return _threadPool;
    63              }
    64          }
    65
    66          return _threadPool;
    67      }
    68
    69      /* ------------------------------------------------------------------------------- */
    70      public void setThreadPool( ThreadPool pool )
    71      {
    72          _threadPool = pool;
    73      }
    74
    75      /**
    76       * Process the contained rules if the threadpool is low on threads
    77       * @param target target field to pass on to the contained rules
    78       * @param request request object to pass on to the contained rules
    79       * @param response response object to pass on to the contained rules
    80       */
    81      public String matchAndApply( String target, HttpServletRequest request, HttpServletResponse response )
    82          throws IOException
    83      {
    84          _threadPool = getThreadPool( (Request) request );
    85
    86          if ( _threadPool == null )
    87          {
    88              Log.warn( "ThreadPool not found" );
    89              return target;
    90          }
    91
    92          Log.debug( "Low on threads: ", _threadPool.isLowOnThreads() );
    93          if ( !_threadPool.isLowOnThreads() )
    94          {
    95              return target;
    96          }
    97
    98          return apply( target, request, response );
    99      }
    100 }