1   package org.apache.maven.repository;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  import java.io.File;
24  import java.util.EventObject;
25  
26  
27  
28  
29  
30  
31  
32  
33  public class ArtifactTransferEvent
34      extends EventObject
35  {
36      
37  
38  
39      public static final int TRANSFER_INITIATED = 0;
40  
41      
42  
43  
44      public static final int TRANSFER_STARTED = 1;
45  
46      
47  
48  
49      public static final int TRANSFER_COMPLETED = 2;
50  
51      
52  
53  
54      public static final int TRANSFER_PROGRESS = 3;
55  
56      
57  
58  
59      public static final int TRANSFER_ERROR = 4;
60  
61      
62  
63  
64      public static final int REQUEST_GET = 5;
65  
66      
67  
68  
69      public static final int REQUEST_PUT = 6;
70  
71      private int eventType;
72  
73      private int requestType;
74  
75      private Exception exception;
76  
77      private File localFile;
78  
79      private ArtifactTransferResource artifact;
80  
81      private long transferredBytes;
82  
83      private byte[] dataBuffer;
84  
85      private int dataOffset;
86  
87      private int dataLength;
88  
89      public ArtifactTransferEvent( String wagon, final int eventType, final int requestType,
90                                    ArtifactTransferResource artifact )
91      {
92          super( wagon );
93  
94          setEventType( eventType );
95  
96          setRequestType( requestType );
97  
98          this.artifact = artifact;
99      }
100 
101     public ArtifactTransferEvent( String wagon, final Exception exception, final int requestType,
102                                   ArtifactTransferResource artifact )
103     {
104         this( wagon, TRANSFER_ERROR, requestType, artifact );
105 
106         this.exception = exception;
107     }
108 
109     public ArtifactTransferResource getResource()
110     {
111         return artifact;
112     }
113 
114     
115 
116 
117     public Exception getException()
118     {
119         return exception;
120     }
121 
122     
123 
124 
125 
126 
127 
128     public int getRequestType()
129     {
130         return requestType;
131     }
132 
133     
134 
135 
136 
137 
138 
139 
140 
141     public void setRequestType( final int requestType )
142     {
143         switch ( requestType )
144         {
145 
146             case REQUEST_PUT:
147                 break;
148             case REQUEST_GET:
149                 break;
150 
151             default :
152                 throw new IllegalArgumentException( "Illegal request type: " + requestType );
153         }
154 
155         this.requestType = requestType;
156     }
157 
158     
159 
160 
161     public int getEventType()
162     {
163         return eventType;
164     }
165 
166     
167 
168 
169     public void setEventType( final int eventType )
170     {
171         switch ( eventType )
172         {
173 
174             case TRANSFER_INITIATED:
175                 break;
176             case TRANSFER_STARTED:
177                 break;
178             case TRANSFER_COMPLETED:
179                 break;
180             case TRANSFER_PROGRESS:
181                 break;
182             case TRANSFER_ERROR:
183                 break;
184             default :
185                 throw new IllegalArgumentException( "Illegal event type: " + eventType );
186         }
187 
188         this.eventType = eventType;
189     }
190 
191     
192 
193 
194     public File getLocalFile()
195     {
196         return localFile;
197     }
198 
199     
200 
201 
202     public void setLocalFile( File localFile )
203     {
204         this.localFile = localFile;
205     }
206 
207     public long getTransferredBytes()
208     {
209         return transferredBytes;
210     }
211 
212     public void setTransferredBytes( long transferredBytes )
213     {
214         this.transferredBytes = transferredBytes;
215     }
216 
217     public byte[] getDataBuffer()
218     {
219         return dataBuffer;
220     }
221 
222     public void setDataBuffer( byte[] dataBuffer )
223     {
224         this.dataBuffer = dataBuffer;
225     }
226 
227     public int getDataOffset()
228     {
229         return dataOffset;
230     }
231 
232     public void setDataOffset( int dataOffset )
233     {
234         this.dataOffset = dataOffset;
235     }
236 
237     public int getDataLength()
238     {
239         return dataLength;
240     }
241 
242     public void setDataLength( int dataLength )
243     {
244         this.dataLength = dataLength;
245     }
246 
247     public String toString()
248     {
249         StringBuffer sb = new StringBuffer();
250 
251         sb.append( "TransferEvent[" );
252 
253         switch ( this.getRequestType() )
254         {
255             case REQUEST_GET:
256                 sb.append( "GET" );
257                 break;
258             case REQUEST_PUT:
259                 sb.append( "PUT" );
260                 break;
261             default:
262                 sb.append( this.getRequestType() );
263                 break;
264         }
265 
266         sb.append( "|" );
267         switch ( this.getEventType() )
268         {
269             case TRANSFER_COMPLETED:
270                 sb.append( "COMPLETED" );
271                 break;
272             case TRANSFER_ERROR:
273                 sb.append( "ERROR" );
274                 break;
275             case TRANSFER_INITIATED:
276                 sb.append( "INITIATED" );
277                 break;
278             case TRANSFER_PROGRESS:
279                 sb.append( "PROGRESS" );
280                 break;
281             case TRANSFER_STARTED:
282                 sb.append( "STARTED" );
283                 break;
284             default:
285                 sb.append( this.getEventType() );
286                 break;
287         }
288 
289         sb.append( "|" );
290         sb.append( this.getLocalFile() ).append( "|" );
291         sb.append( "]" );
292 
293         return sb.toString();
294     }
295 
296     public int hashCode()
297     {
298         final int prime = 31;
299         int result = 1;
300         result = prime * result + eventType;
301         result = prime * result + ( ( exception == null ) ? 0 : exception.hashCode() );
302         result = prime * result + ( ( localFile == null ) ? 0 : localFile.hashCode() );
303         result = prime * result + requestType;
304         return result;
305     }
306 
307     public boolean equals( Object obj )
308     {
309         if ( this == obj )
310         {
311             return true;
312         }
313         if ( ( obj == null ) || ( getClass() != obj.getClass() ) )
314         {
315             return false;
316         }
317         final ArtifactTransferEvent other = (ArtifactTransferEvent) obj;
318         if ( eventType != other.eventType )
319         {
320             return false;
321         }
322         if ( exception == null )
323         {
324             if ( other.exception != null )
325             {
326                 return false;
327             }
328         }
329         else if ( !exception.getClass().equals( other.exception.getClass() ) )
330         {
331             return false;
332         }
333         if ( requestType != other.requestType )
334         {
335             return false;
336         }
337         else if ( !source.equals( other.source ) )
338         {
339             return false;
340         }
341         return true;
342     }
343     
344 }