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