001    package 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    
023    import java.io.File;
024    import 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     */
032    public 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    
145                case REQUEST_PUT:
146                    break;
147                case REQUEST_GET:
148                    break;
149    
150                default :
151                    throw new IllegalArgumentException( "Illegal request type: " + requestType );
152            }
153    
154            this.requestType = requestType;
155        }
156    
157        /**
158         * @return Returns the eventType.
159         */
160        public int getEventType()
161        {
162            return eventType;
163        }
164    
165        /**
166         * @param eventType The eventType to set.
167         */
168        public void setEventType( final int eventType )
169        {
170            switch ( eventType )
171            {
172    
173                case TRANSFER_INITIATED:
174                    break;
175                case TRANSFER_STARTED:
176                    break;
177                case TRANSFER_COMPLETED:
178                    break;
179                case TRANSFER_PROGRESS:
180                    break;
181                case TRANSFER_ERROR:
182                    break;
183                default :
184                    throw new IllegalArgumentException( "Illegal event type: " + eventType );
185            }
186    
187            this.eventType = eventType;
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 long getTransferredBytes()
207        {
208            return transferredBytes;
209        }
210    
211        public void setTransferredBytes( long transferredBytes )
212        {
213            this.transferredBytes = transferredBytes;
214        }
215    
216        public byte[] getDataBuffer()
217        {
218            return dataBuffer;
219        }
220    
221        public void setDataBuffer( byte[] dataBuffer )
222        {
223            this.dataBuffer = dataBuffer;
224        }
225    
226        public int getDataOffset()
227        {
228            return dataOffset;
229        }
230    
231        public void setDataOffset( int dataOffset )
232        {
233            this.dataOffset = dataOffset;
234        }
235    
236        public int getDataLength()
237        {
238            return dataLength;
239        }
240    
241        public void setDataLength( int dataLength )
242        {
243            this.dataLength = dataLength;
244        }
245    
246        public String toString()
247        {
248            StringBuffer sb = new StringBuffer();
249    
250            sb.append( "TransferEvent[" );
251    
252            switch ( this.getRequestType() )
253            {
254                case REQUEST_GET:
255                    sb.append( "GET" );
256                    break;
257                case REQUEST_PUT:
258                    sb.append( "PUT" );
259                    break;
260                default:
261                    sb.append( this.getRequestType() );
262                    break;
263            }
264    
265            sb.append( "|" );
266            switch ( this.getEventType() )
267            {
268                case TRANSFER_COMPLETED:
269                    sb.append( "COMPLETED" );
270                    break;
271                case TRANSFER_ERROR:
272                    sb.append( "ERROR" );
273                    break;
274                case TRANSFER_INITIATED:
275                    sb.append( "INITIATED" );
276                    break;
277                case TRANSFER_PROGRESS:
278                    sb.append( "PROGRESS" );
279                    break;
280                case TRANSFER_STARTED:
281                    sb.append( "STARTED" );
282                    break;
283                default:
284                    sb.append( this.getEventType() );
285                    break;
286            }
287    
288            sb.append( "|" );
289            sb.append( this.getLocalFile() ).append( "|" );
290            sb.append( "]" );
291    
292            return sb.toString();
293        }
294    
295        public int hashCode()
296        {
297            final int prime = 31;
298            int result = 1;
299            result = prime * result + eventType;
300            result = prime * result + ( ( exception == null ) ? 0 : exception.hashCode() );
301            result = prime * result + ( ( localFile == null ) ? 0 : localFile.hashCode() );
302            result = prime * result + requestType;
303            return result;
304        }
305    
306        public boolean equals( Object obj )
307        {
308            if ( this == obj )
309            {
310                return true;
311            }
312            if ( ( obj == null ) || ( getClass() != obj.getClass() ) )
313            {
314                return false;
315            }
316            final ArtifactTransferEvent other = (ArtifactTransferEvent) obj;
317            if ( eventType != other.eventType )
318            {
319                return false;
320            }
321            if ( exception == null )
322            {
323                if ( other.exception != null )
324                {
325                    return false;
326                }
327            }
328            else if ( !exception.getClass().equals( other.exception.getClass() ) )
329            {
330                return false;
331            }
332            if ( requestType != other.requestType )
333            {
334                return false;
335            }
336            else if ( !source.equals( other.source ) )
337            {
338                return false;
339            }
340            return true;
341        }
342        
343    }