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.List;
24  
25  /**
26   * The class allows registration and deregistration of session listeners
27   *
28   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
29   *
30   */
31  public final class SessionEventSupport
32  {
33      /**
34       * registered listeners
35       */
36      private final List<SessionListener> listeners = new ArrayList<SessionListener>();
37  
38      /**
39       * Adds the listener to the collection of listeners
40       * who will be notified when any session event occurs
41       * in this <code>Wagon</code> object.
42       * <br/>
43       * If listener is <code>null</code>, no exception is thrown and no action is performed
44       *
45       * @param listener the transfer listener
46       * @see #removeSessionListener(SessionListener)
47       * @see TransferListener
48       */
49      public void addSessionListener( final SessionListener listener )
50      {
51          if ( listener != null )
52          {
53              listeners.add( listener );
54          }
55      }
56  
57      /**
58       * Removes the session listener from the collection of listeners so
59       * it no longer receives session events.
60       * <br/>
61       * If listener is <code>null</code> or specified listener was not added
62       * to this <code>SessionEventSupport</code> object
63       * no exception is thrown and no action is performed
64       *
65       * @param listener the session listener
66       * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
67       */
68      public void removeSessionListener( final SessionListener listener )
69      {
70          listeners.remove( listener );
71      }
72  
73      /**
74       * Returns whether the specified instance of session
75       * listener was added to the collection of listeners
76       * who will be notified when an session event occurs
77       *
78       * @param listener the session listener
79       * @return <code>true<code>
80       *         if given listener was added to the collection of listeners
81       *         <code>false</code> otherwise
82       * @see org.apache.maven.wagon.events.SessionListener
83       * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
84       */
85      public boolean hasSessionListener( final SessionListener listener )
86      {
87          return listeners.contains( listener );
88      }
89  
90      /**
91       * Dispatches the given <code>SessionEvent</code>
92       * to all registered listeners (calls method {@link SessionListener#sessionDisconnected(SessionEvent)} on all of
93       * them}. The Event should be of type {@link SessionEvent#SESSION_DISCONNECTED}
94       *
95       * @param sessionEvent the SessionEvent which will be dispatched to listeners
96       */
97      public void fireSessionDisconnected( final SessionEvent sessionEvent )
98      {
99          for ( SessionListener listener : listeners )
100         {
101             listener.sessionDisconnected( sessionEvent );
102         }
103     }
104 
105     /**
106      * Dispatches the given <code>SessionEvent</code>
107      * to all registered listeners (calls method {@link SessionListener#sessionDisconnecting(SessionEvent)} } on all of
108      * them}. The Event should be of type {@link SessionEvent#SESSION_DISCONNECTING}
109      *
110      * @param sessionEvent the SessionEvent which will be dispatched to listeners
111      */
112     public void fireSessionDisconnecting( final SessionEvent sessionEvent )
113     {
114         for ( SessionListener listener : listeners )
115         {
116             listener.sessionDisconnecting( sessionEvent );
117         }
118     }
119 
120     /**
121      * Dispatches the given <code>SessionEvent</code>
122      * to all registered listeners (calls method {@link SessionListener#sessionLoggedIn(SessionEvent)} on all of them}.
123      * The Event should be of type {@link SessionEvent#SESSION_LOGGED_IN}
124      *
125      * @param sessionEvent the SessionEvent which will be dispatched to listeners
126      */
127     public void fireSessionLoggedIn( final SessionEvent sessionEvent )
128     {
129         for ( SessionListener listener : listeners )
130         {
131             listener.sessionLoggedIn( sessionEvent );
132         }
133     }
134 
135     /**
136      * Dispatches the given <code>SessionEvent</code>
137      * to all registered listeners (calls method {@link SessionListener#sessionLoggedOff(SessionEvent)} on all of
138      * them}. The Event should be of type {@link SessionEvent#SESSION_LOGGED_OFF}
139      *
140      * @param sessionEvent the SessionEvent which will be dispatched to listeners
141      */
142     public void fireSessionLoggedOff( final SessionEvent sessionEvent )
143     {
144         for ( SessionListener listener : listeners )
145         {
146             listener.sessionLoggedOff( sessionEvent );
147         }
148     }
149 
150     /**
151      * Dispatches the given <code>SessionEvent</code>
152      * to all registered listeners (calls method {@link SessionListener#sessionOpened(SessionEvent)} on all of them}.
153      * The Event should be of type {@link SessionEvent#SESSION_OPENED}
154      *
155      * @param sessionEvent the SessionEvent which will be dispatched to listeners
156      */
157     public void fireSessionOpened( final SessionEvent sessionEvent )
158     {
159         for ( SessionListener listener : listeners )
160         {
161             listener.sessionOpened( sessionEvent );
162         }
163     }
164 
165     /**
166      * Dispatches the given <code>SessionEvent</code>
167      * to all registered listeners (calls method {@link SessionListener#sessionOpening(SessionEvent)} on all of them}.
168      * The Event should be of type {@link SessionEvent#SESSION_OPENING}
169      *
170      * @param sessionEvent the SessionEvent which will be dispatched to listeners
171      */
172     public void fireSessionOpening( final SessionEvent sessionEvent )
173     {
174         for ( SessionListener listener : listeners )
175         {
176             listener.sessionOpening( sessionEvent );
177         }
178     }
179 
180     /**
181      * Dispatches the given <code>SessionEvent</code>
182      * to all registered listeners (calls method {@link SessionListener#sessionConnectionRefused(SessionEvent)} on all
183      * of them}. The Event should be of type {@link SessionEvent#SESSION_CONNECTION_REFUSED}
184      *
185      * @param sessionEvent the SessionEvent which will be dispatched to listeners
186      */
187     public void fireSessionConnectionRefused( final SessionEvent sessionEvent )
188     {
189         for ( SessionListener listener : listeners )
190         {
191             listener.sessionConnectionRefused( sessionEvent );
192         }
193     }
194 
195     /**
196      * Dispatches the given debug message
197      * to all registered listeners (calls method {@link SessionListener#debug(String)} on all of them}.
198      *
199      * @param message the debug message which will be dispatched to listeners
200      */
201     public void fireDebug( final String message )
202     {
203         for ( SessionListener listener : listeners )
204         {
205             listener.debug( message );
206         }
207     }
208 
209     /**
210      * Dispatches the given <code>SessionEvent</code>
211      * to all registered listeners (calls method {@link SessionListener#sessionConnectionRefused(SessionEvent)} on all
212      * of them}. The Event should be of type {@link SessionEvent#SESSION_ERROR_OCCURRED} and it is expected that
213      * {@link SessionEvent#getException()}  method will return not null value
214      *
215      * @param sessionEvent the SessionEvent which will be dispatched to listeners
216      */
217     public void fireSessionError( final SessionEvent sessionEvent )
218     {
219         for ( SessionListener listener : listeners )
220         {
221             listener.sessionError( sessionEvent );
222         }
223     }
224 }