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.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023import java.util.EventListener;
024
025import org.apache.commons.net.util.ListenerList;
026
027/**
028 * ProtocolCommandSupport is a convenience class for managing a list of ProtocolCommandListeners and firing ProtocolCommandEvents. You can simply delegate
029 * ProtocolCommandEvent firing and listener registering/unregistering tasks to this class.
030 *
031 *
032 * @see ProtocolCommandEvent
033 * @see ProtocolCommandListener
034 */
035
036public class ProtocolCommandSupport implements Serializable {
037    private static final long serialVersionUID = -8017692739988399978L;
038
039    private final Object source;
040    private final ListenerList listeners;
041
042    /**
043     * Creates a ProtocolCommandSupport instance using the indicated source as the source of ProtocolCommandEvents.
044     *
045     * @param source The source to use for all generated ProtocolCommandEvents.
046     */
047    public ProtocolCommandSupport(final Object source) {
048        this.listeners = new ListenerList();
049        this.source = source;
050    }
051
052    /**
053     * Adds a ProtocolCommandListener.
054     *
055     * @param listener The ProtocolCommandListener to add.
056     */
057    public void addProtocolCommandListener(final ProtocolCommandListener listener) {
058        listeners.addListener(listener);
059    }
060
061    /**
062     * Fires a ProtocolCommandEvent signalling the sending of a command to all registered listeners, invoking their
063     * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent() } methods.
064     *
065     * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
066     * @param message The entire command string verbatim as sent to the server, including all arguments.
067     */
068    public void fireCommandSent(final String command, final String message) {
069        final ProtocolCommandEvent event;
070
071        event = new ProtocolCommandEvent(source, command, message);
072
073        for (final EventListener listener : listeners) {
074            ((ProtocolCommandListener) listener).protocolCommandSent(event);
075        }
076    }
077
078    /**
079     * Fires a ProtocolCommandEvent signalling the reception of a command reply to all registered listeners, invoking their
080     * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived() } methods.
081     *
082     * @param replyCode The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes,
083     *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
084     *                  POP3Repy.OK).
085     * @param message   The entire reply as received from the server.
086     */
087    public void fireReplyReceived(final int replyCode, final String message) {
088        final ProtocolCommandEvent event;
089        event = new ProtocolCommandEvent(source, replyCode, message);
090
091        for (final EventListener listener : listeners) {
092            ((ProtocolCommandListener) listener).protocolReplyReceived(event);
093        }
094    }
095
096    /**
097     * Returns the number of ProtocolCommandListeners currently registered.
098     *
099     * @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}