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 org.apache.maven.wagon.Wagon;
23
24 /**
25 * SessionEvent is used for notifying SessionListeners about
26 * occurrences of various situations related.
27 * <p/>
28 * The session event is emitted by <code>Wagon</code> objects when
29 * <p/>
30 * <ul>
31 * <li>Before connection to the repository will be opened</li>
32 * <li>After connection to the repository was opened</li>
33 * <li>After wagon has logged-in to the repository</li>
34 * <li>After wagon has logged-off from the repository</li>
35 * <li>Before connection to the repository will be closed</li>
36 * <li>After connection to the repository was closed</li>
37 * </ul>
38 *
39 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
40 *
41 */
42 public class SessionEvent
43 extends WagonEvent
44 {
45
46 /**
47 * A SESSION was closed.
48 */
49 public static final int SESSION_CLOSED = 1;
50
51 /**
52 * A SESSION is about to be disconnected.
53 */
54 public static final int SESSION_DISCONNECTING = 2;
55
56 /**
57 * A SESSION was disconnected (not currently used).
58 */
59 public static final int SESSION_DISCONNECTED = 3;
60
61 /**
62 * A SESSION was refused.
63 */
64 public static final int SESSION_CONNECTION_REFUSED = 4;
65
66 /**
67 * A SESSION is about to be opened.
68 */
69 public static final int SESSION_OPENING = 5;
70
71 /**
72 * A SESSION was opened.
73 */
74 public static final int SESSION_OPENED = 6;
75
76 /**
77 * A SESSION was opened.
78 */
79 public static final int SESSION_LOGGED_IN = 7;
80
81 /**
82 * A SESSION was opened.
83 */
84 public static final int SESSION_LOGGED_OFF = 8;
85
86 /**
87 * A SESSION was opened.
88 */
89 public static final int SESSION_ERROR_OCCURRED = 9;
90
91 /**
92 * The type of the event. One of the SESSSION_XXX constants
93 */
94 private int eventType;
95
96 private Exception exception;
97
98 /**
99 * 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 }