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 
145             case REQUEST_PUT:
146                 break;
147             case REQUEST_GET:
148                 break;
149 
150             default :
151                 throw new IllegalArgumentException( "Illegal request type: " + requestType );
152         }
153 
154         this.requestType = requestType;
155     }
156 
157     /**
158      * @return Returns the eventType.
159      */
160     public int getEventType()
161     {
162         return eventType;
163     }
164 
165     /**
166      * @param eventType The eventType to set.
167      */
168     public void setEventType( final int eventType )
169     {
170         switch ( eventType )
171         {
172 
173             case TRANSFER_INITIATED:
174                 break;
175             case TRANSFER_STARTED:
176                 break;
177             case TRANSFER_COMPLETED:
178                 break;
179             case TRANSFER_PROGRESS:
180                 break;
181             case TRANSFER_ERROR:
182                 break;
183             default :
184                 throw new IllegalArgumentException( "Illegal event type: " + eventType );
185         }
186 
187         this.eventType = eventType;
188     }
189 
190     /**
191      * @return Returns the local file.
192      */
193     public File getLocalFile()
194     {
195         return localFile;
196     }
197 
198     /**
199      * @param localFile The local file to set.
200      */
201     public void setLocalFile( File localFile )
202     {
203         this.localFile = localFile;
204     }
205 
206     public long getTransferredBytes()
207     {
208         return transferredBytes;
209     }
210 
211     public void setTransferredBytes( long transferredBytes )
212     {
213         this.transferredBytes = transferredBytes;
214     }
215 
216     public byte[] getDataBuffer()
217     {
218         return dataBuffer;
219     }
220 
221     public void setDataBuffer( byte[] dataBuffer )
222     {
223         this.dataBuffer = dataBuffer;
224     }
225 
226     public int getDataOffset()
227     {
228         return dataOffset;
229     }
230 
231     public void setDataOffset( int dataOffset )
232     {
233         this.dataOffset = dataOffset;
234     }
235 
236     public int getDataLength()
237     {
238         return dataLength;
239     }
240 
241     public void setDataLength( int dataLength )
242     {
243         this.dataLength = dataLength;
244     }
245 
246     public String toString()
247     {
248         StringBuffer sb = new StringBuffer();
249 
250         sb.append( "TransferEvent[" );
251 
252         switch ( this.getRequestType() )
253         {
254             case REQUEST_GET:
255                 sb.append( "GET" );
256                 break;
257             case REQUEST_PUT:
258                 sb.append( "PUT" );
259                 break;
260             default:
261                 sb.append( this.getRequestType() );
262                 break;
263         }
264 
265         sb.append( "|" );
266         switch ( this.getEventType() )
267         {
268             case TRANSFER_COMPLETED:
269                 sb.append( "COMPLETED" );
270                 break;
271             case TRANSFER_ERROR:
272                 sb.append( "ERROR" );
273                 break;
274             case TRANSFER_INITIATED:
275                 sb.append( "INITIATED" );
276                 break;
277             case TRANSFER_PROGRESS:
278                 sb.append( "PROGRESS" );
279                 break;
280             case TRANSFER_STARTED:
281                 sb.append( "STARTED" );
282                 break;
283             default:
284                 sb.append( this.getEventType() );
285                 break;
286         }
287 
288         sb.append( "|" );
289         sb.append( this.getLocalFile() ).append( "|" );
290         sb.append( "]" );
291 
292         return sb.toString();
293     }
294 
295     public int hashCode()
296     {
297         final int prime = 31;
298         int result = 1;
299         result = prime * result + eventType;
300         result = prime * result + ( ( exception == null ) ? 0 : exception.hashCode() );
301         result = prime * result + ( ( localFile == null ) ? 0 : localFile.hashCode() );
302         result = prime * result + requestType;
303         return result;
304     }
305 
306     public boolean equals( Object obj )
307     {
308         if ( this == obj )
309         {
310             return true;
311         }
312         if ( ( obj == null ) || ( getClass() != obj.getClass() ) )
313         {
314             return false;
315         }
316         final ArtifactTransferEvent other = (ArtifactTransferEvent) obj;
317         if ( eventType != other.eventType )
318         {
319             return false;
320         }
321         if ( exception == null )
322         {
323             if ( other.exception != null )
324             {
325                 return false;
326             }
327         }
328         else if ( !exception.getClass().equals( other.exception.getClass() ) )
329         {
330             return false;
331         }
332         if ( requestType != other.requestType )
333         {
334             return false;
335         }
336         else if ( !source.equals( other.source ) )
337         {
338             return false;
339         }
340         return true;
341     }
342     
343 }