001package org.apache.maven.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022
023import java.io.File;
024import java.util.EventObject;
025
026/**
027 * TransferEvent is used to notify TransferListeners about progress
028 * in transfer of resources form/to the repository
029 *
030 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
031 */
032public class ArtifactTransferEvent
033    extends EventObject
034{
035    /**
036     * A transfer was attempted, but has not yet commenced.
037     */
038    public static final int TRANSFER_INITIATED = 0;
039
040    /**
041     * A transfer was started.
042     */
043    public static final int TRANSFER_STARTED = 1;
044
045    /**
046     * A transfer is completed.
047     */
048    public static final int TRANSFER_COMPLETED = 2;
049
050    /**
051     * A transfer is in progress.
052     */
053    public static final int TRANSFER_PROGRESS = 3;
054
055    /**
056     * An error occurred during transfer
057     */
058    public static final int TRANSFER_ERROR = 4;
059
060    /**
061     * Indicates GET transfer  (from the repository)
062     */
063    public static final int REQUEST_GET = 5;
064
065    /**
066     * Indicates PUT transfer (to the repository)
067     */
068    public static final int REQUEST_PUT = 6;
069
070    private int eventType;
071
072    private int requestType;
073
074    private Exception exception;
075
076    private File localFile;
077
078    private ArtifactTransferResource artifact;
079
080    private long transferredBytes;
081
082    private byte[] dataBuffer;
083
084    private int dataOffset;
085
086    private int dataLength;
087
088    public ArtifactTransferEvent( String wagon, final int eventType, final int requestType,
089                                  ArtifactTransferResource artifact )
090    {
091        super( wagon );
092
093        setEventType( eventType );
094
095        setRequestType( requestType );
096
097        this.artifact = artifact;
098    }
099
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}