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.List;
024
025/**
026 * The class allows registration and deregistration of session listeners
027 *
028 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
029 *
030 */
031public final class SessionEventSupport
032{
033    /**
034     * registered listeners
035     */
036    private final List<SessionListener> listeners = new ArrayList<SessionListener>();
037
038    /**
039     * Adds the listener to the collection of listeners
040     * who will be notified when any session event occurs
041     * in this <code>Wagon</code> object.
042     * <br/>
043     * If listener is <code>null</code>, no exception is thrown and no action is performed
044     *
045     * @param listener the transfer listener
046     * @see #removeSessionListener(SessionListener)
047     * @see TransferListener
048     */
049    public void addSessionListener( final SessionListener listener )
050    {
051        if ( listener != null )
052        {
053            listeners.add( listener );
054        }
055    }
056
057    /**
058     * Removes the session listener from the collection of listeners so
059     * it no longer receives session events.
060     * <br/>
061     * If listener is <code>null</code> or specified listener was not added
062     * to this <code>SessionEventSupport</code> object
063     * no exception is thrown and no action is performed
064     *
065     * @param listener the session listener
066     * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
067     */
068    public void removeSessionListener( final SessionListener listener )
069    {
070        listeners.remove( listener );
071    }
072
073    /**
074     * Returns whether the specified instance of session
075     * listener was added to the collection of listeners
076     * who will be notified when an session event occurs
077     *
078     * @param listener the session listener
079     * @return <code>true<code>
080     *         if given listener was added to the collection of listeners
081     *         <code>false</code> otherwise
082     * @see org.apache.maven.wagon.events.SessionListener
083     * @see #addSessionListener(org.apache.maven.wagon.events.SessionListener)
084     */
085    public boolean hasSessionListener( final SessionListener listener )
086    {
087        return listeners.contains( listener );
088    }
089
090    /**
091     * Dispatches the given <code>SessionEvent</code>
092     * to all registered listeners (calls method {@link SessionListener#sessionDisconnected(SessionEvent)} on all of
093     * them}. The Event should be of type {@link SessionEvent#SESSION_DISCONNECTED}
094     *
095     * @param sessionEvent the SessionEvent which will be dispatched to listeners
096     */
097    public void fireSessionDisconnected( final SessionEvent sessionEvent )
098    {
099        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}