001package org.apache.maven.wagon.events;
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
022import org.apache.maven.wagon.Wagon;
023import org.apache.maven.wagon.resource.Resource;
024
025import java.io.File;
026
027/**
028 * TransferEvent is used to notify TransferListeners about progress
029 * in transfer of resources form/to the repository
030 *
031 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
032 *
033 */
034public class TransferEvent
035    extends WagonEvent
036{
037
038    /**
039     * A transfer was attempted, but has not yet commenced.
040     */
041    public static final int TRANSFER_INITIATED = 0;
042
043    /**
044     * A transfer was started.
045     */
046    public static final int TRANSFER_STARTED = 1;
047
048    /**
049     * A transfer is completed.
050     */
051    public static final int TRANSFER_COMPLETED = 2;
052
053    /**
054     * A transfer is in progress.
055     */
056    public static final int TRANSFER_PROGRESS = 3;
057
058    /**
059     * An error occurred during transfer
060     */
061    public static final int TRANSFER_ERROR = 4;
062
063    /**
064     * Indicates GET transfer  (from the repository)
065     */
066    public static final int REQUEST_GET = 5;
067
068    /**
069     * Indicates PUT transfer (to the repository)
070     */
071    public static final int REQUEST_PUT = 6;
072
073    private Resource resource;
074
075    private int eventType;
076
077    private int requestType;
078
079    private Exception exception;
080
081    private File localFile;
082
083    public TransferEvent( final Wagon wagon, final Resource resource, final int eventType, final int requestType )
084    {
085        super( wagon );
086
087        this.resource = resource;
088
089        setEventType( eventType );
090
091        setRequestType( requestType );
092
093    }
094
095    public TransferEvent( final Wagon wagon, final Resource resource, final Exception exception, final int requestType )
096    {
097        this( wagon, resource, TRANSFER_ERROR, requestType );
098
099        this.exception = exception;
100    }
101
102    /**
103     * @return Returns the resource.
104     */
105    public Resource getResource()
106    {
107        return resource;
108    }
109
110    /**
111     * @return Returns the exception.
112     */
113    public Exception getException()
114    {
115        return exception;
116    }
117
118    /**
119     * Returns the request type.
120     *
121     * @return Returns the request type. The Request type is one of
122     *         <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>
123     */
124    public int getRequestType()
125    {
126        return requestType;
127    }
128
129    /**
130     * Sets the request type
131     *
132     * @param requestType The requestType to set.
133     *                    The Request type value should be either
134     *                    <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>.
135     * @throws IllegalArgumentException when
136     */
137    public void setRequestType( final int requestType )
138    {
139        switch ( requestType )
140        {
141
142            case REQUEST_PUT:
143            case REQUEST_GET:
144                break;
145
146            default :
147                throw new IllegalArgumentException( "Illegal request type: " + requestType );
148        }
149
150        this.requestType = requestType;
151    }
152
153    /**
154     * @return Returns the eventType.
155     */
156    public int getEventType()
157    {
158        return eventType;
159    }
160
161    /**
162     * @param eventType The eventType to set.
163     */
164    public void setEventType( final int eventType )
165    {
166        switch ( eventType )
167        {
168
169            case TRANSFER_INITIATED:
170            case TRANSFER_STARTED:
171            case TRANSFER_COMPLETED:
172            case TRANSFER_PROGRESS:
173            case TRANSFER_ERROR:
174                break;
175            default :
176                throw new IllegalArgumentException( "Illegal event type: " + eventType );
177        }
178
179        this.eventType = eventType;
180    }
181
182    /**
183     * @param resource The resource to set.
184     */
185    public void setResource( final Resource resource )
186    {
187        this.resource = resource;
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 String toString()
207    {
208        StringBuffer sb = new StringBuffer();
209
210        sb.append( "TransferEvent[" );
211
212        switch ( this.getRequestType() )
213        {
214            case REQUEST_GET:
215                sb.append( "GET" );
216                break;
217            case REQUEST_PUT:
218                sb.append( "PUT" );
219                break;
220            default:
221                sb.append( this.getRequestType() );
222                break;
223        }
224
225        sb.append( "|" );
226        switch ( this.getEventType() )
227        {
228            case TRANSFER_COMPLETED:
229                sb.append( "COMPLETED" );
230                break;
231            case TRANSFER_ERROR:
232                sb.append( "ERROR" );
233                break;
234            case TRANSFER_INITIATED:
235                sb.append( "INITIATED" );
236                break;
237            case TRANSFER_PROGRESS:
238                sb.append( "PROGRESS" );
239                break;
240            case TRANSFER_STARTED:
241                sb.append( "STARTED" );
242                break;
243            default:
244                sb.append( this.getEventType() );
245                break;
246        }
247
248        sb.append( "|" );
249
250        sb.append( this.getWagon().getRepository() ).append( "|" );
251        sb.append( this.getLocalFile() ).append( "|" );
252        sb.append( this.getResource().inspect() );
253        sb.append( "]" );
254
255        return sb.toString();
256    }
257
258    public int hashCode()
259    {
260        final int prime = 31;
261        int result = 1;
262        result = prime * result + eventType;
263        result = prime * result + ( ( exception == null ) ? 0 : exception.hashCode() );
264        result = prime * result + ( ( localFile == null ) ? 0 : localFile.hashCode() );
265        result = prime * result + requestType;
266        result = prime * result + ( ( resource == null ) ? 0 : resource.hashCode() );
267        return result;
268    }
269
270    public boolean equals( Object obj )
271    {
272        if ( this == obj )
273        {
274            return true;
275        }
276        if ( ( obj == null ) || ( getClass() != obj.getClass() ) )
277        {
278            return false;
279        }
280        final TransferEvent other = (TransferEvent) obj;
281        if ( eventType != other.eventType )
282        {
283            return false;
284        }
285        if ( exception == null )
286        {
287            if ( other.exception != null )
288            {
289                return false;
290            }
291        }
292        else if ( !exception.getClass().equals( other.exception.getClass() ) )
293        {
294            return false;
295        }
296        if ( requestType != other.requestType )
297        {
298            return false;
299        }
300        if ( resource == null )
301        {
302            if ( other.resource != null )
303            {
304                return false;
305            }
306        }
307        else if ( !resource.equals( other.resource ) )
308        {
309            return false;
310        }
311        else if ( !source.equals( other.source ) )
312        {
313            return false;
314        }
315        return true;
316    }
317    
318}