View Javadoc

1   package org.apache.maven.repository;
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  
23  import java.io.File;
24  import java.util.EventObject;
25  
26  /**
27   * TransferEvent is used to notify TransferListeners about progress
28   * in transfer of resources form/to the repository
29   *
30   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
31   * @version $Id: ArtifactTransferEvent.java 829934 2009-10-26 20:16:00Z bentmann $
32   */
33  public class ArtifactTransferEvent
34      extends EventObject
35  {
36      /**
37       * A transfer was attempted, but has not yet commenced.
38       */
39      public static final int TRANSFER_INITIATED = 0;
40  
41      /**
42       * A transfer was started.
43       */
44      public static final int TRANSFER_STARTED = 1;
45  
46      /**
47       * A transfer is completed.
48       */
49      public static final int TRANSFER_COMPLETED = 2;
50  
51      /**
52       * A transfer is in progress.
53       */
54      public static final int TRANSFER_PROGRESS = 3;
55  
56      /**
57       * An error occurred during transfer
58       */
59      public static final int TRANSFER_ERROR = 4;
60  
61      /**
62       * Indicates GET transfer  (from the repository)
63       */
64      public static final int REQUEST_GET = 5;
65  
66      /**
67       * Indicates PUT transfer (to the repository)
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      * @return Returns the exception.
116      */
117     public Exception getException()
118     {
119         return exception;
120     }
121 
122     /**
123      * Returns the request type.
124      *
125      * @return Returns the request type. The Request type is one of
126      *         <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>
127      */
128     public int getRequestType()
129     {
130         return requestType;
131     }
132 
133     /**
134      * Sets the request type
135      *
136      * @param requestType The requestType to set.
137      *                    The Request type value should be either
138      *                    <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>.
139      * @throws IllegalArgumentException when
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      * @return Returns the eventType.
160      */
161     public int getEventType()
162     {
163         return eventType;
164     }
165 
166     /**
167      * @param eventType The eventType to set.
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      * @return Returns the local file.
193      */
194     public File getLocalFile()
195     {
196         return localFile;
197     }
198 
199     /**
200      * @param localFile The local file to set.
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 }