KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > TransferEvent


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.repository;
7
8 import java.io.File JavaDoc;
9
10 import fr.jayasoft.ivy.Ivy;
11 import fr.jayasoft.ivy.event.IvyEvent;
12
13 /**
14  * TransferEvent is used to notify TransferListeners about progress in transfer
15  * of resources form/to the respository
16  *
17  * This class is LARGELY inspired by org.apache.maven.wagon.events.TransferEvent
18  * released under the following copyright license:
19  *
20  * <pre>
21  *
22  * Copyright 2001-2005 The Apache Software Foundation.
23  *
24  * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
25  * you may not use this file except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  * http://www.apache.org/licenses/LICENSE-2.0
29  *
30  * Unless required by applicable law or agreed to in writing, software
31  * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * </pre>
37  *
38  * Orginal class written by Michal Maczka.
39  *
40  */

41 public class TransferEvent extends IvyEvent {
42     /**
43      * A transfer was attempted, but has not yet commenced.
44      */

45     public static final int TRANSFER_INITIATED = 0;
46
47     /**
48      * A transfer was started.
49      */

50     public final static int TRANSFER_STARTED = 1;
51
52     /**
53      * A transfer is completed.
54      */

55     public final static int TRANSFER_COMPLETED = 2;
56
57     /**
58      * A transfer is in progress.
59      */

60     public final static int TRANSFER_PROGRESS = 3;
61
62     /**
63      * An error occured during transfer
64      */

65     public final static int TRANSFER_ERROR = 4;
66
67     /**
68      * Indicates GET transfer (from the repository)
69      */

70     public final static int REQUEST_GET = 5;
71
72     /**
73      * Indicates PUT transfer (to the repository)
74      */

75     public final static int REQUEST_PUT = 6;
76
77     
78     public static final String JavaDoc TRANSFER_INITIATED_NAME = "transfer-initiated";
79
80     public static final String JavaDoc TRANSFER_STARTED_NAME = "transfer-started";
81
82     public static final String JavaDoc TRANSFER_PROGRESS_NAME = "transfer-progress";
83
84     public static final String JavaDoc TRANSFER_COMPLETED_NAME = "transfer-completed";
85
86     public static final String JavaDoc TRANSFER_ERROR_NAME = "transfer-error";
87
88     private Resource _resource;
89
90     private int _eventType;
91
92     private int _requestType;
93
94     private Exception JavaDoc _exception;
95
96     private File JavaDoc _localFile;
97
98     private Repository _repository;
99     private long _length;
100
101     private long _totalLength;
102     private boolean _isTotalLengthSet = false;
103
104     public TransferEvent(Ivy ivy, final Repository repository, final Resource resource, final int eventType, final int requestType) {
105         super(ivy, getName(eventType));
106         
107         _repository = repository;
108         addAttribute("repository", _repository.getName());
109         _resource = resource;
110         addAttribute("resource", _resource.getName());
111
112         setEventType(eventType);
113
114         setRequestType(requestType);
115         addAttribute("request-type", requestType == REQUEST_GET?"get":"put");
116     }
117
118     public TransferEvent(Ivy ivy, final Repository repository, final Resource resource, final Exception JavaDoc exception, final int requestType) {
119         this(ivy, repository, resource, TRANSFER_ERROR, requestType);
120
121         _exception = exception;
122     }
123
124     public TransferEvent(Ivy ivy, final Repository repository, final Resource resource, long length, final int requestType) {
125         this(ivy, repository, resource, TRANSFER_PROGRESS, requestType);
126
127         _length = length;
128         _totalLength = length;
129     }
130
131     private static String JavaDoc getName(int eventType) {
132         switch (eventType) {
133         case TRANSFER_INITIATED:
134             return TRANSFER_INITIATED_NAME;
135         case TRANSFER_STARTED:
136             return TRANSFER_STARTED_NAME;
137         case TRANSFER_PROGRESS:
138             return TRANSFER_PROGRESS_NAME;
139         case TRANSFER_COMPLETED:
140             return TRANSFER_COMPLETED_NAME;
141         case TRANSFER_ERROR:
142             return TRANSFER_ERROR_NAME;
143         }
144         return null;
145     }
146
147
148     /**
149      * @return Returns the resource.
150      */

151     public Resource getResource() {
152         return _resource;
153     }
154
155     /**
156      * @return Returns the exception.
157      */

158     public Exception JavaDoc getException() {
159         return _exception;
160     }
161
162     /**
163      * Returns the request type.
164      *
165      * @return Returns the request type. The Request type is one of
166      * <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>
167      */

168     public int getRequestType() {
169         return _requestType;
170     }
171
172     /**
173      * Sets the request type
174      *
175      * @param requestType
176      * The requestType to set. The Request type value should be
177      * either
178      * <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>.
179      * @throws IllegalArgumentException when
180      */

181     protected void setRequestType(final int requestType) {
182         switch (requestType) {
183
184         case REQUEST_PUT:
185             break;
186         case REQUEST_GET:
187             break;
188
189         default:
190             throw new IllegalArgumentException JavaDoc("Illegal request type: " + requestType);
191         }
192
193         _requestType = requestType;
194     }
195
196     /**
197      * @return Returns the eventType.
198      */

199     public int getEventType() {
200         return _eventType;
201     }
202
203     /**
204      * @param eventType
205      * The eventType to set.
206      */

207     protected void setEventType(final int eventType) {
208         switch (eventType) {
209
210         case TRANSFER_INITIATED:
211             break;
212         case TRANSFER_STARTED:
213             break;
214         case TRANSFER_COMPLETED:
215             break;
216         case TRANSFER_PROGRESS:
217             break;
218         case TRANSFER_ERROR:
219             break;
220         default:
221             throw new IllegalArgumentException JavaDoc("Illegal event type: " + eventType);
222         }
223
224         this._eventType = eventType;
225     }
226
227     /**
228      * @param _resource
229      * The resource to set.
230      */

231     protected void setResource(final Resource resource) {
232         _resource = resource;
233     }
234
235     /**
236      * @return Returns the local file.
237      */

238     public File JavaDoc getLocalFile() {
239         return _localFile;
240     }
241
242     /**
243      * @param localFile
244      * The local file to set.
245      */

246     protected void setLocalFile(File JavaDoc localFile) {
247         _localFile = localFile;
248     }
249
250
251     public long getLength() {
252         return _length;
253     }
254     
255
256     protected void setLength(long length) {
257         _length = length;
258     }
259
260     public long getTotalLength() {
261         return _totalLength;
262     }
263
264     protected void setTotalLength(long totalLength) {
265         _totalLength = totalLength;
266     }
267
268     public void setException(Exception JavaDoc exception) {
269         _exception = exception;
270     }
271
272     public boolean isTotalLengthSet() {
273         return _isTotalLengthSet;
274     }
275
276     public void setTotalLengthSet(boolean isTotalLengthSet) {
277         _isTotalLengthSet = isTotalLengthSet;
278     }
279     
280     
281     
282 }
283
Popular Tags