View Javadoc
1 package org.apache.commons.net; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Commons" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.Serializable; 58 import java.util.Enumeration; 59 import org.apache.commons.net.util.ListenerList; 60 61 /**** 62 * ProtocolCommandSupport is a convenience class for managing a list of 63 * ProtocolCommandListeners and firing ProtocolCommandEvents. You can 64 * simply delegate ProtocolCommandEvent firing and listener 65 * registering/unregistering tasks to this class. 66 * <p> 67 * <p> 68 * @see ProtocolCommandEvent 69 * @see ProtocolCommandListener 70 * @author Daniel F. Savarese 71 ***/ 72 73 public class ProtocolCommandSupport implements Serializable 74 { 75 private Object __source; 76 private ListenerList __listeners; 77 78 /**** 79 * Creates a ProtocolCommandSupport instant using the indicated source 80 * as the source of fired ProtocolCommandEvents. 81 * <p> 82 * @param source The source to use for all generated ProtocolCommandEvents. 83 ***/ 84 public ProtocolCommandSupport(Object source) 85 { 86 __listeners = new ListenerList(); 87 __source = source; 88 } 89 90 91 /**** 92 * Fires a ProtocolCommandEvent signalling the sending of a command to all 93 * registered listeners, invoking their 94 * <a href="org.apache.commons.net.ProtocolCommandListener.html#protocolCommandSent"> 95 * protocolCommandSent() </a> methods. 96 * <p> 97 * @param command The string representation of the command type sent, not 98 * including the arguments (e.g., "STAT" or "GET"). 99 * @param message The entire command string verbatim as sent to the server, 100 * including all arguments. 101 ***/ 102 public void fireCommandSent(String command, String message) 103 { 104 Enumeration enum; 105 ProtocolCommandEvent event; 106 ProtocolCommandListener listener; 107 108 enum = __listeners.getListeners(); 109 110 event = new ProtocolCommandEvent(__source, command, message); 111 112 while (enum.hasMoreElements()) 113 { 114 listener = (ProtocolCommandListener)enum.nextElement(); 115 listener.protocolCommandSent(event); 116 } 117 } 118 119 /**** 120 * Fires a ProtocolCommandEvent signalling the reception of a command reply 121 * to all registered listeners, invoking their 122 * <a href="org.apache.commons.net.ProtocolCommandListener.html#protocolReplyReceived"> 123 * protocolReplyReceived() </a> methods. 124 * <p> 125 * @param replyCode The integer code indicating the natureof the reply. 126 * This will be the protocol integer value for protocols 127 * that use integer reply codes, or the reply class constant 128 * corresponding to the reply for protocols like POP3 that use 129 * strings like OK rather than integer codes (i.e., POP3Repy.OK). 130 * @param message The entire reply as received from the server. 131 ***/ 132 public void fireReplyReceived(int replyCode, String message) 133 { 134 Enumeration enum; 135 ProtocolCommandEvent event; 136 ProtocolCommandListener listener; 137 138 enum = __listeners.getListeners(); 139 140 event = new ProtocolCommandEvent(__source, replyCode, message); 141 142 while (enum.hasMoreElements()) 143 { 144 listener = (ProtocolCommandListener)enum.nextElement(); 145 listener.protocolReplyReceived(event); 146 } 147 } 148 149 /**** 150 * Adds a ProtocolCommandListener. 151 * <p> 152 * @param listener The ProtocolCommandListener to add. 153 ***/ 154 public void addProtocolCommandListener(ProtocolCommandListener listener) 155 { 156 __listeners.addListener(listener); 157 } 158 159 /**** 160 * Removes a ProtocolCommandListener. 161 * <p> 162 * @param listener The ProtocolCommandListener to remove. 163 ***/ 164 public void removeProtocolCommandListener(ProtocolCommandListener listener) 165 { 166 __listeners.removeListener(listener); 167 } 168 169 170 /**** 171 * Returns the number of ProtocolCommandListeners currently registered. 172 * <p> 173 * @return The number of ProtocolCommandListeners currently registered. 174 ***/ 175 public int getListenerCount() 176 { 177 return __listeners.getListenerCount(); 178 } 179 180 } 181

This page was automatically generated by Maven