1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net;
  17. import java.io.Serializable;
  18. import java.util.Enumeration;
  19. import org.apache.commons.net.util.ListenerList;
  20. /***
  21. * ProtocolCommandSupport is a convenience class for managing a list of
  22. * ProtocolCommandListeners and firing ProtocolCommandEvents. You can
  23. * simply delegate ProtocolCommandEvent firing and listener
  24. * registering/unregistering tasks to this class.
  25. * <p>
  26. * <p>
  27. * @see ProtocolCommandEvent
  28. * @see ProtocolCommandListener
  29. * @author Daniel F. Savarese
  30. ***/
  31. public class ProtocolCommandSupport implements Serializable
  32. {
  33. private Object __source;
  34. private ListenerList __listeners;
  35. /***
  36. * Creates a ProtocolCommandSupport instant using the indicated source
  37. * as the source of fired ProtocolCommandEvents.
  38. * <p>
  39. * @param source The source to use for all generated ProtocolCommandEvents.
  40. ***/
  41. public ProtocolCommandSupport(Object source)
  42. {
  43. __listeners = new ListenerList();
  44. __source = source;
  45. }
  46. /***
  47. * Fires a ProtocolCommandEvent signalling the sending of a command to all
  48. * registered listeners, invoking their
  49. * <a href="org.apache.commons.net.ProtocolCommandListener.html#protocolCommandSent">
  50. * protocolCommandSent() </a> methods.
  51. * <p>
  52. * @param command The string representation of the command type sent, not
  53. * including the arguments (e.g., "STAT" or "GET").
  54. * @param message The entire command string verbatim as sent to the server,
  55. * including all arguments.
  56. ***/
  57. public void fireCommandSent(String command, String message)
  58. {
  59. Enumeration enum;
  60. ProtocolCommandEvent event;
  61. ProtocolCommandListener listener;
  62. enum = __listeners.getListeners();
  63. event = new ProtocolCommandEvent(__source, command, message);
  64. while (enum.hasMoreElements())
  65. {
  66. listener = (ProtocolCommandListener)enum.nextElement();
  67. listener.protocolCommandSent(event);
  68. }
  69. }
  70. /***
  71. * Fires a ProtocolCommandEvent signalling the reception of a command reply
  72. * to all registered listeners, invoking their
  73. * <a href="org.apache.commons.net.ProtocolCommandListener.html#protocolReplyReceived">
  74. * protocolReplyReceived() </a> methods.
  75. * <p>
  76. * @param replyCode The integer code indicating the natureof the reply.
  77. * This will be the protocol integer value for protocols
  78. * that use integer reply codes, or the reply class constant
  79. * corresponding to the reply for protocols like POP3 that use
  80. * strings like OK rather than integer codes (i.e., POP3Repy.OK).
  81. * @param message The entire reply as received from the server.
  82. ***/
  83. public void fireReplyReceived(int replyCode, String message)
  84. {
  85. Enumeration enum;
  86. ProtocolCommandEvent event;
  87. ProtocolCommandListener listener;
  88. enum = __listeners.getListeners();
  89. event = new ProtocolCommandEvent(__source, replyCode, message);
  90. while (enum.hasMoreElements())
  91. {
  92. listener = (ProtocolCommandListener)enum.nextElement();
  93. listener.protocolReplyReceived(event);
  94. }
  95. }
  96. /***
  97. * Adds a ProtocolCommandListener.
  98. * <p>
  99. * @param listener The ProtocolCommandListener to add.
  100. ***/
  101. public void addProtocolCommandListener(ProtocolCommandListener listener)
  102. {
  103. __listeners.addListener(listener);
  104. }
  105. /***
  106. * Removes a ProtocolCommandListener.
  107. * <p>
  108. * @param listener The ProtocolCommandListener to remove.
  109. ***/
  110. public void removeProtocolCommandListener(ProtocolCommandListener listener)
  111. {
  112. __listeners.removeListener(listener);
  113. }
  114. /***
  115. * Returns the number of ProtocolCommandListeners currently registered.
  116. * <p>
  117. * @return The number of ProtocolCommandListeners currently registered.
  118. ***/
  119. public int getListenerCount()
  120. {
  121. return __listeners.getListenerCount();
  122. }
  123. }