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