001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net;
017:
018: import java.io.Serializable;
019: import java.util.Enumeration;
020: import org.apache.commons.net.util.ListenerList;
021:
022: /***
023: * ProtocolCommandSupport is a convenience class for managing a list of
024: * ProtocolCommandListeners and firing ProtocolCommandEvents. You can
025: * simply delegate ProtocolCommandEvent firing and listener
026: * registering/unregistering tasks to this class.
027: * <p>
028: * <p>
029: * @see ProtocolCommandEvent
030: * @see ProtocolCommandListener
031: * @author Daniel F. Savarese
032: ***/
033:
034: public class ProtocolCommandSupport implements Serializable {
035: private Object __source;
036: private ListenerList __listeners;
037:
038: /***
039: * Creates a ProtocolCommandSupport instant using the indicated source
040: * as the source of fired ProtocolCommandEvents.
041: * <p>
042: * @param source The source to use for all generated ProtocolCommandEvents.
043: ***/
044: public ProtocolCommandSupport(Object source) {
045: __listeners = new ListenerList();
046: __source = source;
047: }
048:
049: /***
050: * Fires a ProtocolCommandEvent signalling the sending of a command to all
051: * registered listeners, invoking their
052: * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent() }
053: * methods.
054: * <p>
055: * @param command The string representation of the command type sent, not
056: * including the arguments (e.g., "STAT" or "GET").
057: * @param message The entire command string verbatim as sent to the server,
058: * including all arguments.
059: ***/
060: public void fireCommandSent(String command, String message) {
061: Enumeration en;
062: ProtocolCommandEvent event;
063: ProtocolCommandListener listener;
064:
065: en = __listeners.getListeners();
066:
067: event = new ProtocolCommandEvent(__source, command, message);
068:
069: while (en.hasMoreElements()) {
070: listener = (ProtocolCommandListener) en.nextElement();
071: listener.protocolCommandSent(event);
072: }
073: }
074:
075: /***
076: * Fires a ProtocolCommandEvent signalling the reception of a command reply
077: * to all registered listeners, invoking their
078: * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived() }
079: * methods.
080: * <p>
081: * @param replyCode The integer code indicating the natureof the reply.
082: * This will be the protocol integer value for protocols
083: * that use integer reply codes, or the reply class constant
084: * corresponding to the reply for protocols like POP3 that use
085: * strings like OK rather than integer codes (i.e., POP3Repy.OK).
086: * @param message The entire reply as received from the server.
087: ***/
088: public void fireReplyReceived(int replyCode, String message) {
089: Enumeration en;
090: ProtocolCommandEvent event;
091: ProtocolCommandListener listener;
092:
093: en = __listeners.getListeners();
094:
095: event = new ProtocolCommandEvent(__source, replyCode, message);
096:
097: while (en.hasMoreElements()) {
098: listener = (ProtocolCommandListener) en.nextElement();
099: 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(
109: ProtocolCommandListener listener) {
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(
119: ProtocolCommandListener listener) {
120: __listeners.removeListener(listener);
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: return __listeners.getListenerCount();
130: }
131:
132: }
|