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