View Javadoc

    1   /*
    2    * Copyright 2001-2005 The Apache Software Foundation
    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    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net;
    17
    18  import java.io.Serializable;
    19  import java.util.Enumeration;
    20  import org.apache.commons.net.util.ListenerList;
    21
    22  /****
    23   * ProtocolCommandSupport is a convenience class for managing a list of
    24   * ProtocolCommandListeners and firing ProtocolCommandEvents.  You can
    25   * simply delegate ProtocolCommandEvent firing and listener
    26   * registering/unregistering tasks to this class.
    27   * <p>
    28   * <p>
    29   * @see ProtocolCommandEvent
    30   * @see ProtocolCommandListener
    31   * @author Daniel F. Savarese
    32   ***/
    33
    34  public class ProtocolCommandSupport implements Serializable
    35  {
    36      private Object __source;
    37      private ListenerList __listeners;
    38
    39      /****
    40       * Creates a ProtocolCommandSupport instant using the indicated source
    41       * as the source of fired ProtocolCommandEvents.
    42       * <p>
    43       * @param source  The source to use for all generated ProtocolCommandEvents.
    44       ***/
    45      public ProtocolCommandSupport(Object source)
    46      {
    47          __listeners = new ListenerList();
    48          __source = source;
    49      }
    50
    51
    52      /****
    53       * Fires a ProtocolCommandEvent signalling the sending of a command to all
    54       * registered listeners, invoking their
    55       * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent() }
    56       *  methods.
    57       * <p>
    58       * @param command The string representation of the command type sent, not
    59       *      including the arguments (e.g., "STAT" or "GET").
    60       * @param message The entire command string verbatim as sent to the server,
    61       *        including all arguments.
    62       ***/
    63      public void fireCommandSent(String command, String message)
    64      {
    65          Enumeration en;
    66          ProtocolCommandEvent event;
    67          ProtocolCommandListener listener;
    68
    69          en = __listeners.getListeners();
    70
    71          event = new ProtocolCommandEvent(__source, command, message);
    72
    73          while (en.hasMoreElements())
    74          {
    75              listener = (ProtocolCommandListener)en.nextElement();
    76              listener.protocolCommandSent(event);
    77          }
    78      }
    79
    80      /****
    81       * Fires a ProtocolCommandEvent signalling the reception of a command reply
    82       * to all registered listeners, invoking their
    83       * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived() }
    84       *  methods.
    85       * <p>
    86       * @param replyCode The integer code indicating the natureof the reply.
    87       *   This will be the protocol integer value for protocols
    88       *   that use integer reply codes, or the reply class constant
    89       *   corresponding to the reply for protocols like POP3 that use
    90       *   strings like OK rather than integer codes (i.e., POP3Repy.OK).
    91       * @param message The entire reply as received from the server.
    92       ***/
    93      public void fireReplyReceived(int replyCode, String message)
    94      {
    95          Enumeration en;
    96          ProtocolCommandEvent event;
    97          ProtocolCommandListener listener;
    98
    99          en = __listeners.getListeners();
    100
    101         event = new ProtocolCommandEvent(__source, replyCode, message);
    102
    103         while (en.hasMoreElements())
    104         {
    105             listener = (ProtocolCommandListener)en.nextElement();
    106             listener.protocolReplyReceived(event);
    107         }
    108     }
    109
    110     /****
    111      * Adds a ProtocolCommandListener.
    112      * <p>
    113      * @param listener  The ProtocolCommandListener to add.
    114      ***/
    115     public void addProtocolCommandListener(ProtocolCommandListener listener)
    116     {
    117         __listeners.addListener(listener);
    118     }
    119
    120     /****
    121      * Removes a ProtocolCommandListener.
    122      * <p>
    123      * @param listener  The ProtocolCommandListener to remove.
    124      ***/
    125     public void removeProtocolCommandListener(ProtocolCommandListener listener)
    126     {
    127         __listeners.removeListener(listener);
    128     }
    129
    130
    131     /****
    132      * Returns the number of ProtocolCommandListeners currently registered.
    133      * <p>
    134      * @return The number of ProtocolCommandListeners currently registered.
    135      ***/
    136     public int getListenerCount()
    137     {
    138         return __listeners.getListenerCount();
    139     }
    140
    141 }
    142