View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository;
20  
21  import java.io.File;
22  import java.util.EventObject;
23  
24  /**
25   * TransferEvent is used to notify TransferListeners about progress
26   * in transfer of resources form/to the repository
27   *
28   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
29   */
30  public class ArtifactTransferEvent extends EventObject {
31      /**
32       * A transfer was attempted, but has not yet commenced.
33       */
34      public static final int TRANSFER_INITIATED = 0;
35  
36      /**
37       * A transfer was started.
38       */
39      public static final int TRANSFER_STARTED = 1;
40  
41      /**
42       * A transfer is completed.
43       */
44      public static final int TRANSFER_COMPLETED = 2;
45  
46      /**
47       * A transfer is in progress.
48       */
49      public static final int TRANSFER_PROGRESS = 3;
50  
51      /**
52       * An error occurred during transfer
53       */
54      public static final int TRANSFER_ERROR = 4;
55  
56      /**
57       * Indicates GET transfer  (from the repository)
58       */
59      public static final int REQUEST_GET = 5;
60  
61      /**
62       * Indicates PUT transfer (to the repository)
63       */
64      public static final int REQUEST_PUT = 6;
65  
66      private int eventType;
67  
68      private int requestType;
69  
70      private Exception exception;
71  
72      private File localFile;
73  
74      private ArtifactTransferResource artifact;
75  
76      private long transferredBytes;
77  
78      private byte[] dataBuffer;
79  
80      private int dataOffset;
81  
82      private int dataLength;
83  
84      public ArtifactTransferEvent(
85              String wagon, final int eventType, final int requestType, ArtifactTransferResource artifact) {
86          super(wagon);
87  
88          setEventType(eventType);
89  
90          setRequestType(requestType);
91  
92          this.artifact = artifact;
93      }
94  
95      public ArtifactTransferEvent(
96              String wagon, final Exception exception, final int requestType, ArtifactTransferResource artifact) {
97          this(wagon, TRANSFER_ERROR, requestType, artifact);
98  
99          this.exception = exception;
100     }
101 
102     public ArtifactTransferResource getResource() {
103         return artifact;
104     }
105 
106     /**
107      * @return Returns the exception.
108      */
109     public Exception getException() {
110         return exception;
111     }
112 
113     /**
114      * Returns the request type.
115      *
116      * @return Returns the request type. The Request type is one of
117      *         <code>TransferEvent.REQUEST_GET</code> or <code>TransferEvent.REQUEST_PUT</code>
118      */
119     public int getRequestType() {
120         return requestType;
121     }
122 
123     /**
124      * Sets the request type
125      *
126      * @param requestType The requestType to set.
127      *                    The Request type value should be either
128      *                    <code>TransferEvent.REQUEST_GET</code> or <code>TransferEvent.REQUEST_PUT</code>.
129      * @throws IllegalArgumentException when
130      */
131     public void setRequestType(final int requestType) {
132         switch (requestType) {
133             case REQUEST_PUT:
134                 break;
135             case REQUEST_GET:
136                 break;
137             default:
138                 throw new IllegalArgumentException("Illegal request type: " + requestType);
139         }
140 
141         this.requestType = requestType;
142     }
143 
144     /**
145      * @return Returns the eventType.
146      */
147     public int getEventType() {
148         return eventType;
149     }
150 
151     /**
152      * @param eventType The eventType to set.
153      */
154     public void setEventType(final int eventType) {
155         switch (eventType) {
156             case TRANSFER_INITIATED:
157                 break;
158             case TRANSFER_STARTED:
159                 break;
160             case TRANSFER_COMPLETED:
161                 break;
162             case TRANSFER_PROGRESS:
163                 break;
164             case TRANSFER_ERROR:
165                 break;
166             default:
167                 throw new IllegalArgumentException("Illegal event type: " + eventType);
168         }
169 
170         this.eventType = eventType;
171     }
172 
173     /**
174      * @return Returns the local file.
175      */
176     public File getLocalFile() {
177         return localFile;
178     }
179 
180     /**
181      * @param localFile The local file to set.
182      */
183     public void setLocalFile(File localFile) {
184         this.localFile = localFile;
185     }
186 
187     public long getTransferredBytes() {
188         return transferredBytes;
189     }
190 
191     public void setTransferredBytes(long transferredBytes) {
192         this.transferredBytes = transferredBytes;
193     }
194 
195     public byte[] getDataBuffer() {
196         return dataBuffer;
197     }
198 
199     public void setDataBuffer(byte[] dataBuffer) {
200         this.dataBuffer = dataBuffer;
201     }
202 
203     public int getDataOffset() {
204         return dataOffset;
205     }
206 
207     public void setDataOffset(int dataOffset) {
208         this.dataOffset = dataOffset;
209     }
210 
211     public int getDataLength() {
212         return dataLength;
213     }
214 
215     public void setDataLength(int dataLength) {
216         this.dataLength = dataLength;
217     }
218 
219     public String toString() {
220         StringBuilder sb = new StringBuilder(64);
221 
222         sb.append("TransferEvent[");
223 
224         switch (this.getRequestType()) {
225             case REQUEST_GET:
226                 sb.append("GET");
227                 break;
228             case REQUEST_PUT:
229                 sb.append("PUT");
230                 break;
231             default:
232                 sb.append(this.getRequestType());
233                 break;
234         }
235 
236         sb.append('|');
237         switch (this.getEventType()) {
238             case TRANSFER_COMPLETED:
239                 sb.append("COMPLETED");
240                 break;
241             case TRANSFER_ERROR:
242                 sb.append("ERROR");
243                 break;
244             case TRANSFER_INITIATED:
245                 sb.append("INITIATED");
246                 break;
247             case TRANSFER_PROGRESS:
248                 sb.append("PROGRESS");
249                 break;
250             case TRANSFER_STARTED:
251                 sb.append("STARTED");
252                 break;
253             default:
254                 sb.append(this.getEventType());
255                 break;
256         }
257 
258         sb.append('|');
259         sb.append(this.getLocalFile()).append('|');
260         sb.append(']');
261 
262         return sb.toString();
263     }
264 
265     public int hashCode() {
266         final int prime = 31;
267         int result = 1;
268         result = prime * result + eventType;
269         result = prime * result + ((exception == null) ? 0 : exception.hashCode());
270         result = prime * result + ((localFile == null) ? 0 : localFile.hashCode());
271         result = prime * result + requestType;
272         return result;
273     }
274 
275     public boolean equals(Object obj) {
276         if (this == obj) {
277             return true;
278         }
279         if ((obj == null) || (getClass() != obj.getClass())) {
280             return false;
281         }
282         final ArtifactTransferEvent other = (ArtifactTransferEvent) obj;
283         if (eventType != other.eventType) {
284             return false;
285         }
286         if (exception == null) {
287             if (other.exception != null) {
288                 return false;
289             }
290         } else if (!exception.getClass().equals(other.exception.getClass())) {
291             return false;
292         }
293         if (requestType != other.requestType) {
294             return false;
295         } else if (!source.equals(other.source)) {
296             return false;
297         }
298         return true;
299     }
300 }