View Javadoc
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  
18  package org.apache.commons.net;
19  
20  import java.io.Serializable;
21  import java.util.EventListener;
22  
23  import org.apache.commons.net.util.ListenerList;
24  
25  /**
26   * ProtocolCommandSupport is a convenience class for managing a list of ProtocolCommandListeners and firing ProtocolCommandEvents. You can simply delegate
27   * ProtocolCommandEvent firing and listener registering/unregistering tasks to this class.
28   *
29   *
30   * @see ProtocolCommandEvent
31   * @see ProtocolCommandListener
32   */
33  
34  public class ProtocolCommandSupport implements Serializable {
35      private static final long serialVersionUID = -8017692739988399978L;
36  
37      private final Object source;
38      private final ListenerList listeners;
39  
40      /**
41       * Creates a ProtocolCommandSupport instance using the indicated source as the source of ProtocolCommandEvents.
42       *
43       * @param source The source to use for all generated ProtocolCommandEvents.
44       */
45      public ProtocolCommandSupport(final Object source) {
46          this.listeners = new ListenerList();
47          this.source = source;
48      }
49  
50      /**
51       * Adds a ProtocolCommandListener.
52       *
53       * @param listener The ProtocolCommandListener to add.
54       */
55      public void addProtocolCommandListener(final ProtocolCommandListener listener) {
56          listeners.addListener(listener);
57      }
58  
59      /**
60       * Fires a ProtocolCommandEvent signalling the sending of a command to all registered listeners, invoking their
61       * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent() } methods.
62       *
63       * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
64       * @param message The entire command string verbatim as sent to the server, including all arguments.
65       */
66      public void fireCommandSent(final String command, final String message) {
67          final ProtocolCommandEvent event;
68  
69          event = new ProtocolCommandEvent(source, command, message);
70  
71          for (final EventListener listener : listeners) {
72              ((ProtocolCommandListener) listener).protocolCommandSent(event);
73          }
74      }
75  
76      /**
77       * Fires a ProtocolCommandEvent signalling the reception of a command reply to all registered listeners, invoking their
78       * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived() } methods.
79       *
80       * @param replyCode The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes,
81       *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
82       *                  POP3Repy.OK).
83       * @param message   The entire reply as received from the server.
84       */
85      public void fireReplyReceived(final int replyCode, final String message) {
86          final ProtocolCommandEvent event;
87          event = new ProtocolCommandEvent(source, replyCode, message);
88  
89          for (final EventListener listener : listeners) {
90              ((ProtocolCommandListener) listener).protocolReplyReceived(event);
91          }
92      }
93  
94      /**
95       * Returns the number of ProtocolCommandListeners currently registered.
96       *
97       * @return The number of ProtocolCommandListeners currently registered.
98       */
99      public int getListenerCount() {
100         return listeners.getListenerCount();
101     }
102 
103     private void readObject(final java.io.ObjectInputStream in) {
104         throw new UnsupportedOperationException("Serialization is not supported");
105     }
106 
107     /*
108      * Serialization is unnecessary for this class. Reject attempts to do so until such time as the Serializable attribute can be dropped.
109      */
110 
111     /**
112      * Removes a ProtocolCommandListener.
113      *
114      * @param listener The ProtocolCommandListener to remove.
115      */
116     public void removeProtocolCommandListener(final ProtocolCommandListener listener) {
117         listeners.removeListener(listener);
118     }
119 
120     private void writeObject(final java.io.ObjectOutputStream out) {
121         throw new UnsupportedOperationException("Serialization is not supported");
122     }
123 
124 }