Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TransferListenerSupport |
|
| 1.7777777777777777;1.778 |
1 | /* | |
2 | * Copyright 2004-2007 the original author or authors. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.maven.wagon; | |
17 | ||
18 | import java.util.HashSet; | |
19 | import java.util.Set; | |
20 | ||
21 | import org.apache.maven.wagon.Wagon; | |
22 | import org.apache.maven.wagon.events.TransferEvent; | |
23 | import org.apache.maven.wagon.events.TransferListener; | |
24 | import org.apache.maven.wagon.resource.Resource; | |
25 | ||
26 | /** | |
27 | * Support for sending messages to Maven transfer listeners. Automates the collection of listeners and the iteration | |
28 | * over that collection when an event is fired. | |
29 | * | |
30 | * @author Ben Hale | |
31 | */ | |
32 | class TransferListenerSupport { | |
33 | ||
34 | private Wagon wagon; | |
35 | ||
36 | 0 | private Set<TransferListener> listeners = new HashSet<TransferListener>(); |
37 | ||
38 | /** | |
39 | * Creates a new instance | |
40 | * | |
41 | * @param wagon | |
42 | * The wagon that events will come from | |
43 | */ | |
44 | 0 | public TransferListenerSupport(Wagon wagon) { |
45 | 0 | this.wagon = wagon; |
46 | 0 | } |
47 | ||
48 | /** | |
49 | * Adds a listener to the collection | |
50 | * | |
51 | * @param listener | |
52 | * The listener to add | |
53 | */ | |
54 | public synchronized void addListener(TransferListener listener) { | |
55 | 0 | if (listener.getClass().equals(org.apache.maven.wagon.observers.Debug.class)) { |
56 | // This class clutters up the console with a bunch of incorrect junk about the transfer | |
57 | // Timing information is zero'd out | |
58 | 0 | return; |
59 | } | |
60 | 0 | listeners.add(listener); |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Removes a listener from the collection | |
65 | * | |
66 | * @param listener | |
67 | * The listener to remove | |
68 | */ | |
69 | public synchronized void removeListener(TransferListener listener) { | |
70 | 0 | listeners.remove(listener); |
71 | 0 | } |
72 | ||
73 | /** | |
74 | * Whether the collection already contains a listener | |
75 | * | |
76 | * @param listener | |
77 | * The listener to check for | |
78 | * @return whether the collection contains the listener | |
79 | */ | |
80 | public synchronized boolean hasListener(TransferListener listener) { | |
81 | 0 | return listeners.contains(listener); |
82 | } | |
83 | ||
84 | /** | |
85 | * Sends a transfer initated event to all listeners | |
86 | * | |
87 | * @param resource | |
88 | * The resource being transfered | |
89 | * @param requestType | |
90 | * GET or PUT request | |
91 | * @see TransferEvent#TRANSFER_INITIATED | |
92 | */ | |
93 | public synchronized void fireTransferInitiated(Resource resource, int requestType) { | |
94 | 0 | TransferEvent event = new TransferEvent(wagon, resource, TransferEvent.TRANSFER_INITIATED, requestType); |
95 | 0 | for (TransferListener listener : listeners) { |
96 | 0 | listener.transferInitiated(event); |
97 | } | |
98 | 0 | } |
99 | ||
100 | /** | |
101 | * Sends a transfer started event to all listeners | |
102 | * | |
103 | * @param resource | |
104 | * The resource being transfered | |
105 | * @param requestType | |
106 | * GET or PUT request | |
107 | * @see TransferEvent#TRANSFER_STARTED | |
108 | */ | |
109 | public synchronized void fireTransferStarted(Resource resource, int requestType) { | |
110 | 0 | TransferEvent event = new TransferEvent(wagon, resource, TransferEvent.TRANSFER_STARTED, requestType); |
111 | 0 | for (TransferListener listener : listeners) { |
112 | 0 | listener.transferStarted(event); |
113 | } | |
114 | 0 | } |
115 | ||
116 | public synchronized void fireTransferProgress(Resource resource, int requestType, byte[] buffer, int length) { | |
117 | 0 | TransferEvent event = new TransferEvent(wagon, resource, TransferEvent.TRANSFER_PROGRESS, requestType); |
118 | 0 | for (TransferListener listener : listeners) { |
119 | 0 | listener.transferProgress(event, buffer, length); |
120 | } | |
121 | 0 | } |
122 | ||
123 | /** | |
124 | * Sends a transfer completed event to all listeners | |
125 | * | |
126 | * @param resource | |
127 | * The resource being transfered | |
128 | * @param requestType | |
129 | * GET or PUT request | |
130 | * @see TransferEvent#TRANSFER_COMPLETED | |
131 | */ | |
132 | public synchronized void fireTransferCompleted(Resource resource, int requestType) { | |
133 | 0 | TransferEvent event = new TransferEvent(wagon, resource, TransferEvent.TRANSFER_COMPLETED, requestType); |
134 | 0 | for (TransferListener listener : listeners) { |
135 | 0 | listener.transferCompleted(event); |
136 | } | |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * Sends a transfer error event to all listeners | |
141 | * | |
142 | * @param resource | |
143 | * The resource being transfered | |
144 | * @param requestType | |
145 | * GET or PUT request | |
146 | * @param e | |
147 | * The transfer error | |
148 | */ | |
149 | public synchronized void fireTransferError(Resource resource, int requestType, Exception e) { | |
150 | 0 | TransferEvent event = new TransferEvent(wagon, resource, e, requestType); |
151 | 0 | for (TransferListener listener : listeners) { |
152 | 0 | listener.transferError(event); |
153 | } | |
154 | 0 | } |
155 | } |