View Javadoc

1   package org.apache.maven.wagon.events;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  /**
27   * The class allows registration and deregistration of session listeners
28   *
29   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
30   *
31   */
32  public final class SessionEventSupport
33  {
34      /**
35       * registered listeners
36       */
37      private final List listeners = new ArrayList();
38  
39      /**
40       * Adds the listener to the collection of listeners
41       * who will be notified when any session event occurs
42       * in this <code>Wagon</code> object.
43       * <br/>
44       * If listener is <code>null</code>, no exception is thrown and no action is performed
45       *
46       * @param listener the transfer listener
47       * @see #removeSessionListener(SessionListener)
48       * @see TransferListener
49       */
50      public void addSessionListener( final SessionListener listener )
51      {
52          if ( listener != null )
53          {
54              listeners.add( listener );
55          }
56      }
57  
58      /**
59       * Removes the session listener from the collection of listeners so
60       * it no longer receives session events.
61       * <br/>
62       * If listener is <code>null</code> or specified listener was not added
63       * to this <code>SessionEventSupport</code> object
64       * no exception is thrown and no action is performed
65       *
66       * @param listener the session listener
67       * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
68       */
69      public void removeSessionListener( final SessionListener listener )
70      {
71          listeners.remove( listener );
72      }
73  
74      /**
75       * Returns whether the specified instance of session
76       * listener was added to the collection of listeners
77       * who will be notified when an session event occurs
78       *
79       * @param listener the session listener
80       * @return <code>true<code>
81       *         if given listener was added to the collection of listeners
82       *         <code>false</code> otherwise
83       * @see org.apache.maven.wagon.events.SessionListener
84       * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
85       */
86      public boolean hasSessionListener( final SessionListener listener )
87      {
88          return listeners.contains( listener );
89      }
90  
91      /**
92       * Dispatches the given <code>SessionEvent</code>
93       * to all registered listeners (calls method {@link SessionListener#sessionDisconnected(SessionEvent)} on all of
94       * them}. The Event should be of type {@link SessionEvent#SESSION_DISCONNECTED}
95       *
96       * @param sessionEvent the SessionEvent which will be dispatched to listeners
97       */
98      public void fireSessionDisconnected( final SessionEvent sessionEvent )
99      {
100         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
101         {
102             final SessionListener listener = (SessionListener) iter.next();
103             listener.sessionDisconnected( sessionEvent );
104         }
105     }
106 
107     /**
108      * Dispatches the given <code>SessionEvent</code>
109      * to all registered listeners (calls method {@link SessionListener#sessionDisconnecting(SessionEvent)} } on all of
110      * them}. The Event should be of type {@link SessionEvent#SESSION_DISCONNECTING}
111      *
112      * @param sessionEvent the SessionEvent which will be dispatched to listeners
113      */
114     public void fireSessionDisconnecting( final SessionEvent sessionEvent )
115     {
116         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
117         {
118             final SessionListener listener = (SessionListener) iter.next();
119             listener.sessionDisconnecting( sessionEvent );
120         }
121     }
122 
123     /**
124      * Dispatches the given <code>SessionEvent</code>
125      * to all registered listeners (calls method {@link SessionListener#sessionLoggedIn(SessionEvent)} on all of them}.
126      * The Event should be of type {@link SessionEvent#SESSION_LOGGED_IN}
127      *
128      * @param sessionEvent the SessionEvent which will be dispatched to listeners
129      */
130     public void fireSessionLoggedIn( final SessionEvent sessionEvent )
131     {
132         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
133         {
134             final SessionListener listener = (SessionListener) iter.next();
135             listener.sessionLoggedIn( sessionEvent );
136         }
137     }
138 
139     /**
140      * Dispatches the given <code>SessionEvent</code>
141      * to all registered listeners (calls method {@link SessionListener#sessionLoggedOff(SessionEvent)} on all of
142      * them}. The Event should be of type {@link SessionEvent#SESSION_LOGGED_OFF}
143      *
144      * @param sessionEvent the SessionEvent which will be dispatched to listeners
145      */
146     public void fireSessionLoggedOff( final SessionEvent sessionEvent )
147     {
148         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
149         {
150             final SessionListener listener = (SessionListener) iter.next();
151             listener.sessionLoggedOff( sessionEvent );
152         }
153     }
154 
155     /**
156      * Dispatches the given <code>SessionEvent</code>
157      * to all registered listeners (calls method {@link SessionListener#sessionOpened(SessionEvent)} on all of them}.
158      * The Event should be of type {@link SessionEvent#SESSION_OPENED}
159      *
160      * @param sessionEvent the SessionEvent which will be dispatched to listeners
161      */
162     public void fireSessionOpened( final SessionEvent sessionEvent )
163     {
164         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
165         {
166             final SessionListener listener = (SessionListener) iter.next();
167             listener.sessionOpened( sessionEvent );
168         }
169     }
170 
171     /**
172      * Dispatches the given <code>SessionEvent</code>
173      * to all registered listeners (calls method {@link SessionListener#sessionOpening(SessionEvent)} on all of them}.
174      * The Event should be of type {@link SessionEvent#SESSION_OPENING}
175      *
176      * @param sessionEvent the SessionEvent which will be dispatched to listeners
177      */
178     public void fireSessionOpening( final SessionEvent sessionEvent )
179     {
180         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
181         {
182             final SessionListener listener = (SessionListener) iter.next();
183             listener.sessionOpening( sessionEvent );
184         }
185     }
186 
187     /**
188      * Dispatches the given <code>SessionEvent</code>
189      * to all registered listeners (calls method {@link SessionListener#sessionConnectionRefused(SessionEvent)} on all
190      * of them}. The Event should be of type {@link SessionEvent#SESSION_CONNECTION_REFUSED}
191      *
192      * @param sessionEvent the SessionEvent which will be dispatched to listeners
193      */
194     public void fireSessionConnectionRefused( final SessionEvent sessionEvent )
195     {
196         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
197         {
198             final SessionListener listener = (SessionListener) iter.next();
199             listener.sessionConnectionRefused( sessionEvent );
200         }
201     }
202 
203     /**
204      * Dispatches the given debug message
205      * to all registered listeners (calls method {@link SessionListener#debug(String)} on all of them}.
206      *
207      * @param message the debug message which will be dispatched to listeners
208      */
209     public void fireDebug( final String message )
210     {
211         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
212         {
213             final SessionListener listener = (SessionListener) iter.next();
214             listener.debug( message );
215         }
216     }
217 
218     /**
219      * Dispatches the given <code>SessionEvent</code>
220      * to all registered listeners (calls method {@link SessionListener#sessionConnectionRefused(SessionEvent)} on all
221      * of them}. The Event should be of type {@link SessionEvent#SESSION_ERROR_OCCURRED} and it is expected that
222      * {@link SessionEvent#getException()}  method will return not null value
223      *
224      * @param sessionEvent the SessionEvent which will be dispatched to listeners
225      */
226     public void fireSessionError( final SessionEvent sessionEvent )
227     {
228         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
229         {
230             final SessionListener listener = (SessionListener) iter.next();
231             listener.sessionError( sessionEvent );
232         }
233     }
234 }