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