001package org.apache.maven.wagon.events;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Iterator;
024import java.util.List;
025
026/**
027 * The class allows registration and deregistration of session listeners
028 *
029 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
030 *
031 */
032public final class SessionEventSupport
033{
034    /**
035     * registered listeners
036     */
037    private final List listeners = new ArrayList();
038
039    /**
040     * Adds the listener to the collection of listeners
041     * who will be notified when any session event occurs
042     * in this <code>Wagon</code> object.
043     * <br/>
044     * If listener is <code>null</code>, no exception is thrown and no action is performed
045     *
046     * @param listener the transfer listener
047     * @see #removeSessionListener(SessionListener)
048     * @see TransferListener
049     */
050    public void addSessionListener( final SessionListener listener )
051    {
052        if ( listener != null )
053        {
054            listeners.add( listener );
055        }
056    }
057
058    /**
059     * Removes the session listener from the collection of listeners so
060     * it no longer receives session events.
061     * <br/>
062     * If listener is <code>null</code> or specified listener was not added
063     * to this <code>SessionEventSupport</code> object
064     * no exception is thrown and no action is performed
065     *
066     * @param listener the session listener
067     * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
068     */
069    public void removeSessionListener( final SessionListener listener )
070    {
071        listeners.remove( listener );
072    }
073
074    /**
075     * Returns whether the specified instance of session
076     * listener was added to the collection of listeners
077     * who will be notified when an session event occurs
078     *
079     * @param listener the session listener
080     * @return <code>true<code>
081     *         if given listener was added to the collection of listeners
082     *         <code>false</code> otherwise
083     * @see org.apache.maven.wagon.events.SessionListener
084     * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
085     */
086    public boolean hasSessionListener( final SessionListener listener )
087    {
088        return listeners.contains( listener );
089    }
090
091    /**
092     * Dispatches the given <code>SessionEvent</code>
093     * to all registered listeners (calls method {@link SessionListener#sessionDisconnected(SessionEvent)} on all of
094     * them}. The Event should be of type {@link SessionEvent#SESSION_DISCONNECTED}
095     *
096     * @param sessionEvent the SessionEvent which will be dispatched to listeners
097     */
098    public void fireSessionDisconnected( final SessionEvent sessionEvent )
099    {
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}