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