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