001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.repository;
019:
020: import java.io.File;
021:
022: import org.apache.ivy.core.event.IvyEvent;
023:
024: /**
025: * TransferEvent is used to notify TransferListeners about progress in transfer of resources form/to
026: * the respository This class is LARGELY inspired by org.apache.maven.wagon.events.TransferEvent
027: * released under the following copyright license:
028: *
029: * <pre>
030: *
031: * Copyright 2001-2005 The Apache Software Foundation.
032: *
033: * Licensed under the Apache License, Version 2.0 (the "License");
034: * you may not use this file except in compliance with the License.
035: * You may obtain a copy of the License at
036: *
037: * http://www.apache.org/licenses/LICENSE-2.0
038: *
039: * Unless required by applicable law or agreed to in writing, software
040: * distributed under the License is distributed on an "AS IS" BASIS,
041: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
042: * See the License for the specific language governing permissions and
043: * limitations under the License.
044: *
045: * </pre>
046: *
047: * Orginal class written by Michal Maczka.
048: */
049: public class TransferEvent extends IvyEvent {
050: /**
051: * A transfer was attempted, but has not yet commenced.
052: */
053: public static final int TRANSFER_INITIATED = 0;
054:
055: /**
056: * A transfer was started.
057: */
058: public static final int TRANSFER_STARTED = 1;
059:
060: /**
061: * A transfer is completed.
062: */
063: public static final int TRANSFER_COMPLETED = 2;
064:
065: /**
066: * A transfer is in progress.
067: */
068: public static final int TRANSFER_PROGRESS = 3;
069:
070: /**
071: * An error occured during transfer
072: */
073: public static final int TRANSFER_ERROR = 4;
074:
075: /**
076: * Used to check event type validity: should always be 0 <= type <= LAST_EVENT_TYPE
077: */
078: private static final int LAST_EVENT_TYPE = TRANSFER_ERROR;
079:
080: /**
081: * Indicates GET transfer (from the repository)
082: */
083: public static final int REQUEST_GET = 5;
084:
085: /**
086: * Indicates PUT transfer (to the repository)
087: */
088: public static final int REQUEST_PUT = 6;
089:
090: public static final String TRANSFER_INITIATED_NAME = "transfer-initiated";
091:
092: public static final String TRANSFER_STARTED_NAME = "transfer-started";
093:
094: public static final String TRANSFER_PROGRESS_NAME = "transfer-progress";
095:
096: public static final String TRANSFER_COMPLETED_NAME = "transfer-completed";
097:
098: public static final String TRANSFER_ERROR_NAME = "transfer-error";
099:
100: private Resource resource;
101:
102: private int eventType;
103:
104: private int requestType;
105:
106: private Exception exception;
107:
108: private File localFile;
109:
110: private Repository repository;
111:
112: private long length;
113:
114: private long totalLength;
115:
116: private boolean isTotalLengthSet = false;
117:
118: /**
119: * This attribute is used to store the time at which the event enters a type.
120: * <p>
121: * The array should better be seen as a Map from event type (int) to the time at which the event
122: * entered that type, 0 if it never entered this type.
123: * </p>
124: */
125: private long[] timeTracking = new long[LAST_EVENT_TYPE + 1];
126:
127: public TransferEvent(final Repository repository,
128: final Resource resource, final int eventType,
129: final int requestType) {
130: super (getName(eventType));
131:
132: this .repository = repository;
133: setResource(resource);
134:
135: setEventType(eventType);
136:
137: setRequestType(requestType);
138: }
139:
140: public TransferEvent(final Repository repository,
141: final Resource resource, final Exception exception,
142: final int requestType) {
143: this (repository, resource, TRANSFER_ERROR, requestType);
144:
145: this .exception = exception;
146: }
147:
148: public TransferEvent(final Repository repository,
149: final Resource resource, long length, final int requestType) {
150: this (repository, resource, TRANSFER_PROGRESS, requestType);
151:
152: this .length = length;
153: this .totalLength = length;
154: }
155:
156: private static String getName(int eventType) {
157: switch (eventType) {
158: case TRANSFER_INITIATED:
159: return TRANSFER_INITIATED_NAME;
160: case TRANSFER_STARTED:
161: return TRANSFER_STARTED_NAME;
162: case TRANSFER_PROGRESS:
163: return TRANSFER_PROGRESS_NAME;
164: case TRANSFER_COMPLETED:
165: return TRANSFER_COMPLETED_NAME;
166: case TRANSFER_ERROR:
167: return TRANSFER_ERROR_NAME;
168: default:
169: return null;
170: }
171: }
172:
173: /**
174: * @return Returns the resource.
175: */
176: public Resource getResource() {
177: return resource;
178: }
179:
180: /**
181: * @return Returns the exception.
182: */
183: public Exception getException() {
184: return exception;
185: }
186:
187: /**
188: * Returns the request type.
189: *
190: * @return Returns the request type. The Request type is one of
191: * <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>
192: */
193: public int getRequestType() {
194: return requestType;
195: }
196:
197: /**
198: * Sets the request type
199: *
200: * @param requestType
201: * The requestType to set. The Request type value should be either
202: * <code>TransferEvent.REQUEST_GET<code> or <code>TransferEvent.REQUEST_PUT<code>.
203: * @throws IllegalArgumentException when
204: */
205: protected void setRequestType(final int requestType) {
206: switch (requestType) {
207:
208: case REQUEST_PUT:
209: break;
210: case REQUEST_GET:
211: break;
212:
213: default:
214: throw new IllegalArgumentException("Illegal request type: "
215: + requestType);
216: }
217:
218: this .requestType = requestType;
219: addAttribute("request-type", requestType == REQUEST_GET ? "get"
220: : "put");
221: }
222:
223: /**
224: * @return Returns the eventType.
225: */
226: public int getEventType() {
227: return eventType;
228: }
229:
230: /**
231: * @param eventType
232: * The eventType to set.
233: */
234: protected void setEventType(final int eventType) {
235: checkEventType(eventType);
236: if (this .eventType != eventType) {
237: this .eventType = eventType;
238: timeTracking[eventType] = System.currentTimeMillis();
239: if (eventType > TRANSFER_INITIATED) {
240: addAttribute("total-duration", String
241: .valueOf(getElapsedTime(TRANSFER_INITIATED,
242: eventType)));
243: if (eventType > TRANSFER_STARTED) {
244: addAttribute("duration", String
245: .valueOf(getElapsedTime(TRANSFER_STARTED,
246: eventType)));
247: }
248: }
249: }
250: }
251:
252: /**
253: * @param resource
254: * The resource to set.
255: */
256: protected void setResource(final Resource resource) {
257: this .resource = resource;
258: addAttribute("resource", this .resource.getName());
259: }
260:
261: /**
262: * @return Returns the local file.
263: */
264: public File getLocalFile() {
265: return localFile;
266: }
267:
268: /**
269: * @param localFile
270: * The local file to set.
271: */
272: protected void setLocalFile(File localFile) {
273: this .localFile = localFile;
274: }
275:
276: public long getLength() {
277: return length;
278: }
279:
280: protected void setLength(long length) {
281: this .length = length;
282: }
283:
284: public long getTotalLength() {
285: return totalLength;
286: }
287:
288: protected void setTotalLength(long totalLength) {
289: this .totalLength = totalLength;
290: }
291:
292: public void setException(Exception exception) {
293: this .exception = exception;
294: }
295:
296: public boolean isTotalLengthSet() {
297: return isTotalLengthSet;
298: }
299:
300: public void setTotalLengthSet(boolean isTotalLengthSet) {
301: this .isTotalLengthSet = isTotalLengthSet;
302: }
303:
304: public Repository getRepository() {
305: return repository;
306: }
307:
308: /**
309: * Returns the elapsed time (in ms) between when the event entered one type until it entered
310: * another event time.
311: * <p>
312: * This is especially useful to get the elapsed transfer time:
313: * <pre>
314: * getElapsedTime(TransferEvent.TRANSFER_STARTED, TransferEvent.TRANSFER_COMPLETED);
315: * </pre>
316: * </p>
317: * <p>
318: * Special cases:
319: * <ul>
320: * <li>returns -1 if the event never entered the fromEventType or the toEventType.</li>
321: * <li>returns 0 if the event entered toEventType before fromEventType</li>
322: * </ul>
323: * </p>
324: *
325: * @param fromEventType
326: * the event type constant from which time should be measured
327: * @param toEventType
328: * the event type constant to which time should be measured
329: * @return the elapsed time (in ms) between when the event entered fromEventType until it
330: * entered toEventType.
331: * @throws IllegalArgumentException
332: * if either type is not a known constant event type.
333: */
334: public long getElapsedTime(int fromEventType, int toEventType) {
335: checkEventType(fromEventType);
336: checkEventType(toEventType);
337: long start = timeTracking[fromEventType];
338: long end = timeTracking[toEventType];
339: if (start == 0 || end == 0) {
340: return -1;
341: } else if (end < start) {
342: return 0;
343: } else {
344: return end - start;
345: }
346: }
347:
348: /**
349: * Checks the given event type is a valid event type, throws an {@link IllegalArgumentException}
350: * if it isn't
351: *
352: * @param eventType
353: * the event type to check
354: */
355: private void checkEventType(int eventType) {
356: if (eventType < 0 || eventType > LAST_EVENT_TYPE) {
357: throw new IllegalArgumentException("invalid event type "
358: + eventType);
359: }
360: }
361: }
|