View Javadoc

    1   //========================================================================
    2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.jetty;
    16
    17  import java.util.ArrayList;
    18
    19  import org.mortbay.component.AbstractLifeCycle;
    20  import org.mortbay.io.Buffer;
    21  import org.mortbay.io.Buffers;
    22
    23  /* ------------------------------------------------------------ */
    24  /** Abstract Buffer pool.
    25   * simple unbounded pool of buffers.
    26   * @author gregw
    27   *
    28   */
    29  public abstract class AbstractBuffers extends AbstractLifeCycle implements Buffers
    30  {
    31      private int _headerBufferSize=4*1024;
    32      private int _requestBufferSize=8*1024;
    33      private int _responseBufferSize=24*1024;
    34
    35      // Use and array of buffers to avoid contention
    36      private transient ArrayList _headerBuffers=new ArrayList();
    37      protected transient int _loss;
    38      private transient ArrayList _requestBuffers;
    39      private transient ArrayList _responseBuffers;
    40
    41      /* ------------------------------------------------------------ */
    42      /**
    43       * @return Returns the headerBufferSize.
    44       */
    45      public int getHeaderBufferSize()
    46      {
    47          return _headerBufferSize;
    48      }
    49
    50      /* ------------------------------------------------------------ */
    51      /**
    52       * @param headerBufferSize The headerBufferSize to set.
    53       */
    54      public void setHeaderBufferSize(int headerBufferSize)
    55      {
    56          _headerBufferSize = headerBufferSize;
    57      }
    58
    59      /* ------------------------------------------------------------ */
    60      /**
    61       * @return Returns the requestBufferSize.
    62       */
    63      public int getRequestBufferSize()
    64      {
    65          return _requestBufferSize;
    66      }
    67
    68      /* ------------------------------------------------------------ */
    69      /**
    70       * @param requestBufferSize The requestBufferSize to set.
    71       */
    72      public void setRequestBufferSize(int requestBufferSize)
    73      {
    74          _requestBufferSize = requestBufferSize;
    75      }
    76
    77      /* ------------------------------------------------------------ */
    78      /**
    79       * @return Returns the responseBufferSize.
    80       */
    81      public int getResponseBufferSize()
    82      {
    83          return _responseBufferSize;
    84      }
    85
    86      /* ------------------------------------------------------------ */
    87      /**
    88       * @param responseBufferSize The responseBufferSize to set.
    89       */
    90      public void setResponseBufferSize(int responseBufferSize)
    91      {
    92          _responseBufferSize = responseBufferSize;
    93      }
    94
    95
    96      /* ------------------------------------------------------------ */
    97      protected abstract Buffer newBuffer(int size);
    98
    99
    100     /* ------------------------------------------------------------ */
    101     public Buffer getBuffer(int size)
    102     {
    103         if (size==_headerBufferSize)
    104         {
    105             synchronized(_headerBuffers)
    106             {
    107                 if (_headerBuffers.size()>0)
    108                     return (Buffer) _headerBuffers.remove(_headerBuffers.size()-1);
    109             }
    110             return newBuffer(size);
    111         }
    112         else if (size==_responseBufferSize)
    113         {
    114             synchronized(_responseBuffers)
    115             {
    116                 if (_responseBuffers.size()==0)
    117                     return newBuffer(size);
    118                 return (Buffer) _responseBuffers.remove(_responseBuffers.size()-1);
    119             }
    120         }
    121         else if (size==_requestBufferSize)
    122         {
    123             synchronized(_requestBuffers)
    124             {
    125                 if (_requestBuffers.size()==0)
    126                     return newBuffer(size);
    127                 return (Buffer) _requestBuffers.remove(_requestBuffers.size()-1);
    128             }
    129         }
    130
    131         return newBuffer(size);
    132     }
    133
    134
    135     /* ------------------------------------------------------------ */
    136     public void returnBuffer(Buffer buffer)
    137     {
    138         buffer.clear();
    139         if (!buffer.isVolatile() && !buffer.isImmutable())
    140         {
    141             int c=buffer.capacity();
    142             if (c==_headerBufferSize)
    143             {
    144                 synchronized(_headerBuffers)
    145                 {
    146                     _headerBuffers.add(buffer);
    147                 }
    148             }
    149             else if (c==_responseBufferSize)
    150             {
    151                 synchronized(_responseBuffers)
    152                 {
    153                     _responseBuffers.add(buffer);
    154                 }
    155             }
    156             else if (c==_requestBufferSize)
    157             {
    158                 synchronized(_requestBuffers)
    159                 {
    160                     _requestBuffers.add(buffer);
    161                 }
    162             }
    163         }
    164     }
    165
    166     /* ------------------------------------------------------------ */
    167     protected void doStart() throws Exception
    168     {
    169         super.doStart();
    170
    171         if (_headerBuffers!=null)
    172             _headerBuffers.clear();
    173         else
    174             _headerBuffers=new ArrayList();
    175
    176         if (_requestBuffers!=null)
    177             _requestBuffers.clear();
    178         else
    179             _requestBuffers=new ArrayList();
    180
    181         if (_responseBuffers!=null)
    182             _responseBuffers.clear();
    183         else
    184             _responseBuffers=new ArrayList();
    185     }
    186
    187
    188 }