View Javadoc

    1   //========================================================================
    2   // Copyright 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.cometd.client;
    16
    17  import java.util.Map;
    18
    19  /**
    20   * Advice
    21   *
    22   * Implementation of advice field from Bayeux message.
    23   *
    24   */
    25  public class Advice
    26  {
    27      //reconnect: retry, handshake, none
    28      public static final int __RECONNECT_RETRY = 0;
    29      public static final int __RECONNECT_HANDSHAKE = 1;
    30      public static final int __RECONNECT_NONE = 2;
    31      private Integer _reconnect;
    32
    33      //interval: ms
    34      private Long _interval;
    35
    36      //multiple-clients: boolean
    37      private Boolean _multipleClients;
    38
    39      //hosts: list
    40      private String[] _hosts;
    41
    42      private Map _adviceField;
    43
    44
    45      public Advice (Map adviceField)
    46      {
    47          _adviceField = adviceField;
    48      }
    49
    50      /**
    51       * A client MAY attempt to reconnect with a /meta/connect after
    52       * the interval (as defined by "interval" advice or client-default backoff),
    53       * and with the same credentials.
    54       * @return
    55       */
    56      public boolean isReconnectRetry ()
    57      {
    58          if (_reconnect == null && _adviceField != null)
    59               _reconnect = parseReconnect((String)_adviceField.get("reconnect"));
    60
    61          return _reconnect != null && _reconnect.intValue() ==__RECONNECT_RETRY;
    62      }
    63
    64      /**
    65       * The server has terminated any prior connection status and
    66       * the client MUST reconnect with a /meta/handshake. A client
    67       * MUST NOT automatically retry if handshake advice has been received.
    68       * @return
    69       */
    70      public boolean isReconnectHandshake ()
    71      {
    72          if (_reconnect == null && _adviceField != null)
    73              _reconnect = parseReconnect((String)_adviceField.get("reconnect"));
    74
    75          return _reconnect != null && _reconnect.intValue() == __RECONNECT_HANDSHAKE;
    76      }
    77
    78      /**
    79       * Hard failure for the connect attempt. Do not attempt to
    80       * reconnect at all. A client MUST respect reconnect advice
    81       * of none and MUST NOT automatically retry or handshake.
    82       * @return
    83       */
    84      public boolean isReconnectNone ()
    85      {
    86          if (_reconnect == null && _adviceField != null)
    87              _reconnect = parseReconnect((String)_adviceField.get("reconnect"));
    88
    89          return _reconnect != null && _reconnect.intValue() == __RECONNECT_NONE;
    90      }
    91
    92      public long getInterval()
    93      {
    94          if (_interval==null && _adviceField != null)
    95              _interval = (Long)_adviceField.get("interval");
    96
    97          return (_interval==null?0:_interval.longValue());
    98      }
    99
    100     public boolean isMultipleClients()
    101     {
    102         if (_multipleClients == null && _adviceField != null)
    103             _multipleClients = (Boolean)_adviceField.get("multiple-clients");
    104
    105         return (_multipleClients==null?false:_multipleClients.booleanValue());
    106     }
    107
    108     public String[] getHosts()
    109     {
    110         if (_hosts == null && _adviceField != null)
    111         {
    112             Object[] hosts = (Object[])_adviceField.get("hosts");
    113             if (hosts!=null)
    114             {
    115                 _hosts = new String[hosts.length];
    116                 System.arraycopy(hosts, 0, _hosts, 0, hosts.length);
    117             }
    118         }
    119         return _hosts;
    120     }
    121
    122     private Integer parseReconnect (String reconnect)
    123     {
    124         if (reconnect != null)
    125         {
    126             reconnect = reconnect.trim();
    127             if (reconnect.equalsIgnoreCase("retry"))
    128                 return new Integer( __RECONNECT_RETRY);
    129             else if (reconnect.equalsIgnoreCase("handshake"))
    130                 return new Integer(__RECONNECT_HANDSHAKE);
    131             else if (reconnect.equalsIgnoreCase("none"))
    132                return new Integer(__RECONNECT_NONE);
    133         }
    134         return null;
    135     }
    136 }