1 package org.apache.maven.wagon.events;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26 * The class allows registration and removal of event listeners of type
27 * TransferListener and dispatch of those events to those listeners
28 *
29 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
30 *
31 */
32 public final class TransferEventSupport
33 {
34
35 /**
36 * registered listeners
37 */
38 private final List<TransferListener> listeners = new ArrayList<TransferListener>();
39
40 /**
41 * Adds the listener to the collection of listeners
42 * who will be notified when any transfer event occurs
43 * in this <code>Wagon</code> object.
44 * <br/>
45 * If listener is <code>null</code>, no exception is thrown and no action is performed
46 *
47 * @param listener the transfer listener
48 * @see #removeTransferListener(org.apache.maven.wagon.events.TransferListener)
49 * @see TransferListener
50 */
51 public synchronized void addTransferListener( final TransferListener listener )
52 {
53 if ( listener != null )
54 {
55 listeners.add( listener );
56 }
57 }
58
59 /**
60 * Removes the transfer listener from the collection of listeners so
61 * it no longer receives transfer events.
62 * <br/>
63 * If listener is <code>null</code> or specified listener was not added
64 * to this <code>TransferEventSupport</code> object
65 * no exception is thrown and no action is performed
66 *
67 * @param listener the transfer listener
68 * @see #addTransferListener(TransferListener)
69 */
70 public synchronized void removeTransferListener( final TransferListener listener )
71 {
72 listeners.remove( listener );
73 }
74
75 /**
76 * Returns whether the specified instance of transfer
77 * listener was added to the collection of listeners
78 * who will be notified when an transfer event occurs
79 *
80 * @param listener the transfer listener
81 * @return <code>true<code>
82 * if given listener was added to the collection of listeners
83 * <code>false</code> otherwise
84 * @see org.apache.maven.wagon.events.TransferEvent
85 * @see #addTransferListener(TransferListener)
86 */
87 public synchronized boolean hasTransferListener( final TransferListener listener )
88 {
89 return listeners.contains( listener );
90 }
91
92
93 /**
94 * Dispatches the given <code>TransferEvent</code>
95 * to all registered listeners (calls method {@link TransferListener#transferStarted(TransferEvent)} on all of
96 * them}. The Event should be of type {@link TransferEvent#TRANSFER_COMPLETED}
97 *
98 * @param transferEvent the TransferEvent which will be dispatched to listeners
99 */
100 public synchronized void fireTransferStarted( final TransferEvent transferEvent )
101 {
102 for ( TransferListener listener : listeners )
103 {
104 listener.transferStarted( transferEvent );
105 }
106 }
107
108 /**
109 * Dispatches the given <code>TransferEvent</code>
110 * to all registered listeners (calls method {@link TransferListener#transferProgress(TransferEvent, byte[], int)}
111 * on all of them). The Event should be of type {@link TransferEvent#TRANSFER_PROGRESS}.
112 *
113 * @param transferEvent the TransferEvent which will be dispatched to listeners
114 * @param buffer the buffer containing the additional content
115 * @param length the length of the content in the buffer
116 */
117 public synchronized void fireTransferProgress( final TransferEvent transferEvent, byte[] buffer, int length )
118 {
119 for ( TransferListener listener : listeners )
120 {
121 listener.transferProgress( transferEvent, buffer, length );
122
123 }
124 }
125
126 /**
127 * Dispatches the given <code>TransferEvent</code>
128 * to all registered listeners (calls method {@link TransferListener#transferCompleted(TransferEvent)} on all of
129 * them}. The Event should be of type {@link TransferEvent#TRANSFER_COMPLETED}
130 *
131 * @param transferEvent the TransferEvent which will be dispatched to listeners
132 */
133 public synchronized void fireTransferCompleted( final TransferEvent transferEvent )
134 {
135 for ( TransferListener listener : listeners )
136 {
137 listener.transferCompleted( transferEvent );
138
139 }
140 }
141
142 /**
143 * Dispatches the given <code>TransferEvent</code>
144 * to all registered listeners (calls method {@link TransferListener#transferError(TransferEvent)} on all of them.
145 * The Event should be of type {@link TransferEvent#TRANSFER_ERROR} and it is expected that
146 * {@link TransferEvent#getException()} } method will return not null value
147 *
148 * @param transferEvent the TransferEvent which will be dispatched to listeners
149 */
150 public synchronized void fireTransferError( final TransferEvent transferEvent )
151 {
152 for ( TransferListener listener : listeners )
153 {
154 listener.transferError( transferEvent );
155
156 }
157 }
158
159 /**
160 * Dispatches the given debug message
161 * to all registered listeners (calls method {@link TransferListener#debug(String)} on all of them.
162 *
163 * @param message the debug message which will be dispatched to listeners
164 */
165 public synchronized void fireDebug( final String message )
166 {
167
168 for ( TransferListener listener : listeners )
169 {
170 listener.debug( message );
171
172 }
173 }
174
175 /**
176 * Dispatches the given <code>TransferEvent</code>
177 * to all registered listeners (calls method {@link TransferListener#transferInitiated(TransferEvent)} on all of
178 * them. The Event should be of type {@link TransferEvent#TRANSFER_INITIATED}.
179 *
180 * @param transferEvent the TransferEvent which will be dispatched to listeners
181 */
182 public synchronized void fireTransferInitiated( final TransferEvent transferEvent )
183 {
184 for ( TransferListener listener : listeners )
185 {
186 listener.transferInitiated( transferEvent );
187 }
188 }
189 }