001package org.apache.maven.wagon.events; 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 022import java.util.ArrayList; 023import java.util.List; 024 025/** 026 * The class allows registration and removal of event listeners of type 027 * TransferListener and dispatch of those events to those listeners 028 * 029 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 030 * 031 */ 032public final class TransferEventSupport 033{ 034 035 /** 036 * registered listeners 037 */ 038 private final List<TransferListener> listeners = new ArrayList<TransferListener>(); 039 040 /** 041 * Adds the listener to the collection of listeners 042 * who will be notified when any transfer event occurs 043 * in this <code>Wagon</code> object. 044 * <br/> 045 * If listener is <code>null</code>, no exception is thrown and no action is performed 046 * 047 * @param listener the transfer listener 048 * @see #removeTransferListener(org.apache.maven.wagon.events.TransferListener) 049 * @see TransferListener 050 */ 051 public synchronized void addTransferListener( final TransferListener listener ) 052 { 053 if ( listener != null ) 054 { 055 listeners.add( listener ); 056 } 057 } 058 059 /** 060 * Removes the transfer listener from the collection of listeners so 061 * it no longer receives transfer events. 062 * <br/> 063 * If listener is <code>null</code> or specified listener was not added 064 * to this <code>TransferEventSupport</code> object 065 * no exception is thrown and no action is performed 066 * 067 * @param listener the transfer listener 068 * @see #addTransferListener(TransferListener) 069 */ 070 public synchronized void removeTransferListener( final TransferListener listener ) 071 { 072 listeners.remove( listener ); 073 } 074 075 /** 076 * Returns whether the specified instance of transfer 077 * listener was added to the collection of listeners 078 * who will be notified when an transfer event occurs 079 * 080 * @param listener the transfer listener 081 * @return <code>true<code> 082 * if given listener was added to the collection of listeners 083 * <code>false</code> otherwise 084 * @see org.apache.maven.wagon.events.TransferEvent 085 * @see #addTransferListener(TransferListener) 086 */ 087 public synchronized boolean hasTransferListener( final TransferListener listener ) 088 { 089 return listeners.contains( listener ); 090 } 091 092 093 /** 094 * Dispatches the given <code>TransferEvent</code> 095 * to all registered listeners (calls method {@link TransferListener#transferStarted(TransferEvent)} on all of 096 * them}. The Event should be of type {@link TransferEvent#TRANSFER_COMPLETED} 097 * 098 * @param transferEvent the TransferEvent which will be dispatched to listeners 099 */ 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}