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 org.apache.maven.wagon.Wagon;
023
024/**
025 * SessionEvent is used for notifying SessionListeners about
026 * occurrences of various situations related.
027 * <p/>
028 * The session event is emitted by <code>Wagon</code> objects when
029 * <p/>
030 * <ul>
031 * <li>Before connection to the repository will be opened</li>
032 * <li>After connection to the repository was opened</li>
033 * <li>After wagon has logged-in to the repository</li>
034 * <li>After wagon has logged-off from the repository</li>
035 * <li>Before connection to the repository will be closed</li>
036 * <li>After connection to the repository was closed</li>
037 * </ul>
038 *
039 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
040 *
041 */
042public class SessionEvent
043    extends WagonEvent
044{
045
046    /**
047     * A SESSION was closed.
048     */
049    public static final int SESSION_CLOSED = 1;
050
051    /**
052     * A SESSION is about to be disconnected.
053     */
054    public static final int SESSION_DISCONNECTING = 2;
055
056    /**
057     * A SESSION was disconnected (not currently used).
058     */
059    public static final int SESSION_DISCONNECTED = 3;
060
061    /**
062     * A SESSION was refused.
063     */
064    public static final int SESSION_CONNECTION_REFUSED = 4;
065
066    /**
067     * A SESSION is about to be opened.
068     */
069    public static final int SESSION_OPENING = 5;
070
071    /**
072     * A SESSION was opened.
073     */
074    public static final int SESSION_OPENED = 6;
075
076    /**
077     * A SESSION was opened.
078     */
079    public static final int SESSION_LOGGED_IN = 7;
080
081    /**
082     * A SESSION was opened.
083     */
084    public static final int SESSION_LOGGED_OFF = 8;
085
086    /**
087     * A SESSION was opened.
088     */
089    public static final int SESSION_ERROR_OCCURRED = 9;
090
091    /**
092     * The type of the event. One of the SESSSION_XXX constants
093     */
094    private int eventType;
095
096    private Exception exception;
097
098    /**
099     * Creates new instance of SessionEvent
100     *
101     * @param wagon     <code>Wagon<code> object which created this event
102     * @param eventType the type of the event
103     */
104    public SessionEvent( final Wagon wagon, final int eventType )
105    {
106        super( wagon );
107        this.eventType = eventType;
108
109    }
110
111    /**
112     * Creates new instance of SessionEvent. Sets event type to <code>SESSION_ERROR_OCCURRED</code>
113     *
114     * @param wagon     <code>Wagon<code> object which created this event
115     * @param exception the exception
116     */
117    public SessionEvent( final Wagon wagon, final Exception exception )
118    {
119        super( wagon );
120        this.exception = exception;
121        this.eventType = SESSION_ERROR_OCCURRED;
122
123    }
124
125    /**
126     * @return Returns the type.
127     */
128    public int getEventType()
129    {
130        return eventType;
131    }
132
133    /**
134     * @return Returns the exception.
135     */
136    public Exception getException()
137    {
138        return exception;
139    }
140
141    /**
142     * @param eventType The eventType to set.
143     */
144    public void setEventType( final int eventType )
145    {
146        switch ( eventType )
147        {
148            case SessionEvent.SESSION_CLOSED:
149            case SessionEvent.SESSION_DISCONNECTED:
150            case SessionEvent.SESSION_DISCONNECTING:
151            case SessionEvent.SESSION_ERROR_OCCURRED:
152            case SessionEvent.SESSION_LOGGED_IN:
153            case SessionEvent.SESSION_LOGGED_OFF:
154            case SessionEvent.SESSION_OPENED:
155            case SessionEvent.SESSION_OPENING:
156            case SessionEvent.SESSION_CONNECTION_REFUSED:
157                break;
158            default :
159                throw new IllegalArgumentException( "Illegal event type: " + eventType );
160        }
161        this.eventType = eventType;
162    }
163
164    /**
165     * @param exception The exception to set.
166     */
167    public void setException( final Exception exception )
168    {
169        this.exception = exception;
170    }
171
172    public String toString()
173    {
174        StringBuilder sb = new StringBuilder();
175
176        sb.append( "SessionEvent[" );
177
178        switch ( this.eventType )
179        {
180            case SessionEvent.SESSION_CLOSED:
181                sb.append( "CONNECTION_CLOSED" );
182                break;
183            case SessionEvent.SESSION_DISCONNECTED:
184                sb.append( "CONNECTION_DISCONNECTED" );
185                break;
186            case SessionEvent.SESSION_DISCONNECTING:
187                sb.append( "CONNECTION_DISCONNECTING" );
188                break;
189            case SessionEvent.SESSION_ERROR_OCCURRED:
190                sb.append( "CONNECTION_ERROR_OCCURRED" );
191                break;
192            case SessionEvent.SESSION_LOGGED_IN:
193                sb.append( "CONNECTION_LOGGED_IN" );
194                break;
195            case SessionEvent.SESSION_LOGGED_OFF:
196                sb.append( "CONNECTION_LOGGED_OFF" );
197                break;
198            case SessionEvent.SESSION_OPENED:
199                sb.append( "CONNECTION_OPENED" );
200                break;
201            case SessionEvent.SESSION_OPENING:
202                sb.append( "CONNECTION_OPENING" );
203                break;
204            case SessionEvent.SESSION_CONNECTION_REFUSED:
205                sb.append( "CONNECTION_CONNECTION_REFUSED" );
206                break;
207            default:
208                sb.append( eventType );
209        }
210        sb.append( "|" );
211
212        sb.append( this.getWagon().getRepository() ).append( "|" );
213        sb.append( this.source );
214
215        if ( exception != null )
216        {
217            sb.append( "|" );
218            sb.append( exception.getClass().getName() ).append( ":" );
219            sb.append( exception.getMessage() );
220        }
221
222        sb.append( "]" );
223
224        return sb.toString();
225    }
226}