ProtocolCommandSupport.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net;

  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.EventListener;

  22. import org.apache.commons.net.util.ListenerList;

  23. /**
  24.  * ProtocolCommandSupport is a convenience class for managing a list of ProtocolCommandListeners and firing ProtocolCommandEvents. You can simply delegate
  25.  * ProtocolCommandEvent firing and listener registering/unregistering tasks to this class.
  26.  *
  27.  *
  28.  * @see ProtocolCommandEvent
  29.  * @see ProtocolCommandListener
  30.  */

  31. public class ProtocolCommandSupport implements Serializable {
  32.     private static final long serialVersionUID = -8017692739988399978L;

  33.     private final Object source;
  34.     private final ListenerList listeners;

  35.     /**
  36.      * Creates a ProtocolCommandSupport instance using the indicated source as the source of ProtocolCommandEvents.
  37.      *
  38.      * @param source The source to use for all generated ProtocolCommandEvents.
  39.      */
  40.     public ProtocolCommandSupport(final Object source) {
  41.         this.listeners = new ListenerList();
  42.         this.source = source;
  43.     }

  44.     /**
  45.      * Adds a ProtocolCommandListener.
  46.      *
  47.      * @param listener The ProtocolCommandListener to add.
  48.      */
  49.     public void addProtocolCommandListener(final ProtocolCommandListener listener) {
  50.         listeners.addListener(listener);
  51.     }

  52.     /**
  53.      * Fires a ProtocolCommandEvent signalling the sending of a command to all registered listeners, invoking their
  54.      * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent() } methods.
  55.      *
  56.      * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
  57.      * @param message The entire command string verbatim as sent to the server, including all arguments.
  58.      */
  59.     public void fireCommandSent(final String command, final String message) {
  60.         final ProtocolCommandEvent event;

  61.         event = new ProtocolCommandEvent(source, command, message);

  62.         for (final EventListener listener : listeners) {
  63.             ((ProtocolCommandListener) listener).protocolCommandSent(event);
  64.         }
  65.     }

  66.     /**
  67.      * Fires a ProtocolCommandEvent signalling the reception of a command reply to all registered listeners, invoking their
  68.      * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived() } methods.
  69.      *
  70.      * @param replyCode The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes,
  71.      *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
  72.      *                  POP3Repy.OK).
  73.      * @param message   The entire reply as received from the server.
  74.      */
  75.     public void fireReplyReceived(final int replyCode, final String message) {
  76.         final ProtocolCommandEvent event;
  77.         event = new ProtocolCommandEvent(source, replyCode, message);

  78.         for (final EventListener listener : listeners) {
  79.             ((ProtocolCommandListener) listener).protocolReplyReceived(event);
  80.         }
  81.     }

  82.     /**
  83.      * Returns the number of ProtocolCommandListeners currently registered.
  84.      *
  85.      * @return The number of ProtocolCommandListeners currently registered.
  86.      */
  87.     public int getListenerCount() {
  88.         return listeners.getListenerCount();
  89.     }

  90.     private void readObject(final ObjectInputStream in) {
  91.         throw new UnsupportedOperationException("Serialization is not supported");
  92.     }

  93.     /*
  94.      * Serialization is unnecessary for this class. Reject attempts to do so until such time as the Serializable attribute can be dropped.
  95.      */

  96.     /**
  97.      * Removes a ProtocolCommandListener.
  98.      *
  99.      * @param listener The ProtocolCommandListener to remove.
  100.      */
  101.     public void removeProtocolCommandListener(final ProtocolCommandListener listener) {
  102.         listeners.removeListener(listener);
  103.     }

  104.     private void writeObject(final ObjectOutputStream out) {
  105.         throw new UnsupportedOperationException("Serialization is not supported");
  106.     }

  107. }