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