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 { 036 private Object __source; 037 private ListenerList __listeners; 038 039 /*** 040 * Creates a ProtocolCommandSupport instant using the indicated source 041 * as the source of fired ProtocolCommandEvents. 042 * <p> 043 * @param source The source to use for all generated ProtocolCommandEvents. 044 ***/ 045 public ProtocolCommandSupport(Object source) 046 { 047 __listeners = new ListenerList(); 048 __source = source; 049 } 050 051 052 /*** 053 * Fires a ProtocolCommandEvent signalling the sending of a command to all 054 * registered listeners, invoking their 055 * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent() } 056 * methods. 057 * <p> 058 * @param command The string representation of the command type sent, not 059 * including the arguments (e.g., "STAT" or "GET"). 060 * @param message The entire command string verbatim as sent to the server, 061 * including all arguments. 062 ***/ 063 public void fireCommandSent(String command, String message) 064 { 065 Enumeration en; 066 ProtocolCommandEvent event; 067 ProtocolCommandListener listener; 068 069 en = __listeners.getListeners(); 070 071 event = new ProtocolCommandEvent(__source, command, message); 072 073 while (en.hasMoreElements()) 074 { 075 listener = (ProtocolCommandListener)en.nextElement(); 076 listener.protocolCommandSent(event); 077 } 078 } 079 080 /*** 081 * Fires a ProtocolCommandEvent signalling the reception of a command reply 082 * to all registered listeners, invoking their 083 * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived() } 084 * methods. 085 * <p> 086 * @param replyCode The integer code indicating the natureof the reply. 087 * This will be the protocol integer value for protocols 088 * that use integer reply codes, or the reply class constant 089 * corresponding to the reply for protocols like POP3 that use 090 * strings like OK rather than integer codes (i.e., POP3Repy.OK). 091 * @param message The entire reply as received from the server. 092 ***/ 093 public void fireReplyReceived(int replyCode, String message) 094 { 095 Enumeration en; 096 ProtocolCommandEvent event; 097 ProtocolCommandListener listener; 098 099 en = __listeners.getListeners(); 100 101 event = new ProtocolCommandEvent(__source, replyCode, message); 102 103 while (en.hasMoreElements()) 104 { 105 listener = (ProtocolCommandListener)en.nextElement(); 106 listener.protocolReplyReceived(event); 107 } 108 } 109 110 /*** 111 * Adds a ProtocolCommandListener. 112 * <p> 113 * @param listener The ProtocolCommandListener to add. 114 ***/ 115 public void addProtocolCommandListener(ProtocolCommandListener listener) 116 { 117 __listeners.addListener(listener); 118 } 119 120 /*** 121 * Removes a ProtocolCommandListener. 122 * <p> 123 * @param listener The ProtocolCommandListener to remove. 124 ***/ 125 public void removeProtocolCommandListener(ProtocolCommandListener listener) 126 { 127 __listeners.removeListener(listener); 128 } 129 130 131 /*** 132 * Returns the number of ProtocolCommandListeners currently registered. 133 * <p> 134 * @return The number of ProtocolCommandListeners currently registered. 135 ***/ 136 public int getListenerCount() 137 { 138 return __listeners.getListenerCount(); 139 } 140 141 } 142