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