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   */
29  @Deprecated
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     @Override
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     @Override
267     public int hashCode() {
268         final int prime = 31;
269         int result = 1;
270         result = prime * result + eventType;
271         result = prime * result + ((exception == null) ? 0 : exception.hashCode());
272         result = prime * result + ((localFile == null) ? 0 : localFile.hashCode());
273         result = prime * result + requestType;
274         return result;
275     }
276 
277     @Override
278     public boolean equals(Object obj) {
279         if (this == obj) {
280             return true;
281         }
282         if ((obj == null) || (getClass() != obj.getClass())) {
283             return false;
284         }
285         final ArtifactTransferEvent other = (ArtifactTransferEvent) obj;
286         if (eventType != other.eventType) {
287             return false;
288         }
289         if (exception == null) {
290             if (other.exception != null) {
291                 return false;
292             }
293         } else if (!exception.getClass().equals(other.exception.getClass())) {
294             return false;
295         }
296         if (requestType != other.requestType) {
297             return false;
298         } else {
299             return source.equals(other.source);
300         }
301     }
302 }