View Javadoc

1   // Getter.java
2   // $Id: Getter.java,v 1.6 2007/02/21 00:07:50 sjardine Exp $
3   //
4   // Copyright 2000, Joe Phillips <jaiger@innovationsw.com>
5   // Copyright 2001, 2002 Innovation Software Group, LLC - http://www.innovationsw.com
6   //
7   // This library is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU Library General Public
9   // License as published by the Free Software Foundation; either
10  // version 2 of the License, or (at your option) any later version.
11  //
12  // This library is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  // Library General Public License for more details.
16  //
17  // You should have received a copy of the GNU Library General Public
18  // License along with this library; if not, write to the Free
19  // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  //
21  package gnu.inet.ftp;
22  
23  import java.io.OutputStream;
24  import java.util.Enumeration;
25  import java.util.Vector;
26  
27  /***
28   * This class serves as a superclass to the ActiveGetter and PassiveGetter
29   * classes, providing a common interface and data members.
30   * <P>
31   * This class is used internally to the FtpClient class.
32   */
33  public class Getter extends Thread implements ConnectionEventSource,
34  		TransferEventSource {
35  
36  	public static final int BUFFER_SIZE = 1024;
37  
38  	protected OutputStream ostream;
39  
40  	protected boolean cancelled = false;
41  
42  	protected Vector connectionListeners;
43  
44  	protected Vector transferListeners;
45  
46  	protected char mode;
47  
48  	protected char type;
49  
50  	/***
51  	 * set the Getter initial state with debugging disabled.
52  	 */
53  	public Getter() {
54  		super();
55  		this.cancelled = false;
56  		this.connectionListeners = new Vector();
57  		this.transferListeners = new Vector();
58  		this.mode = FtpClientProtocol.MODE_STREAM;
59  		this.type = FtpClientProtocol.TYPE_IMAGE;
60  	}
61  
62  	//
63  	// public methods
64  	//
65  
66  	/***
67  	 * set the OutputStream to be used for data storage
68  	 * 
69  	 * @param ostream
70  	 *            the OutputStream to write data to
71  	 */
72  	public synchronized void setOutputStream(OutputStream ostream) {
73  		this.ostream = ostream;
74  	}
75  
76  	/***
77  	 * set the mode value
78  	 * 
79  	 * @param mode
80  	 *            the new mode value. Valid values (MODE_*) can be found in the
81  	 *            FtpClientProtocol class.
82  	 * @see FtpClientProtocol
83  	 */
84  	public synchronized void setMode(char mode) {
85  		this.mode = mode;
86  	}
87  
88  	/***
89  	 * set the type value
90  	 * 
91  	 * @param type
92  	 *            the new type value. Valid values (TYPE_*) can be found in the
93  	 *            FtpClientProtocol class.
94  	 * @see FtpClientProtocol
95  	 */
96  	public synchronized void setType(char type) {
97  		this.type = type;
98  	}
99  
100 	/***
101 	 * cancel a running transfer sets a flag and calls interrupt() can only be
102 	 * called once
103 	 */
104 	public void cancel() {
105 		if (!cancelled) {
106 			cancelled = true;
107 			interrupt();
108 		}
109 	}
110 
111 	public synchronized void start() {
112 		this.cancelled = false; // Reset cancelled flag here and not in run!
113 		super.start();
114 	}
115 
116 	/***
117 	 * add a ConnectionListener to the list of connectionListeners
118 	 * 
119 	 * @param listener
120 	 *            the ConnectionListener to add to the list
121 	 */
122 	public void addConnectionListener(ConnectionListener listener) {
123 		connectionListeners.addElement(listener);
124 	}
125 
126 	/***
127 	 * add a set of ConnectionListener to the list of connectionListeners
128 	 * 
129 	 * @param listeners
130 	 *            the ConnectionListeners to add to the list
131 	 */
132 	public void addConnectionListeners(Vector listeners) {
133 		Enumeration e = listeners.elements();
134 		while (e.hasMoreElements()) {
135 			ConnectionListener listener = (ConnectionListener) e.nextElement();
136 			connectionListeners.addElement(listener);
137 		}
138 	}
139 
140 	/***
141 	 * De-register a ConnectionListener from the event source. The listener
142 	 * should not receive any more events.
143 	 * 
144 	 * @param listener
145 	 *            the ConnectionListener to de-register
146 	 */
147 	public void removeConnectionListener(ConnectionListener listener) {
148 		connectionListeners.removeElement(listener);
149 	}
150 
151 	/***
152 	 * add a TransferListener to the list of transfer listeners. All
153 	 * TransferListeners registered with this Getter will be notified when a
154 	 * transfer event occurs.
155 	 * 
156 	 * @param listener
157 	 *            the TransferListener to add to the list
158 	 */
159 	public void addTransferListener(TransferListener listener) {
160 		transferListeners.addElement(listener);
161 	}
162 
163 	/***
164 	 * add a set of TransferListener to the list of transfer listeners
165 	 * 
166 	 * @param listeners
167 	 *            the TransferListeners to add to the list
168 	 */
169 	public void addTransferListeners(Vector listeners) {
170 		Enumeration e = listeners.elements();
171 		while (e.hasMoreElements()) {
172 			TransferListener listener = (TransferListener) e.nextElement();
173 			transferListeners.addElement(listener);
174 		}
175 	}
176 
177 	/***
178 	 * De-register a TransferListener from the event source. The listener should
179 	 * not receive any more events.
180 	 * 
181 	 * @param listener
182 	 *            the TransferListener to de-register
183 	 */
184 	public void removeTransferListener(TransferListener listener) {
185 		transferListeners.removeElement(listener);
186 	}
187 
188 	/***
189 	 * signal that a connection has been opened
190 	 * 
191 	 * @param event
192 	 *            the event to distribute to each ConnectionListener
193 	 */
194 	protected void signalConnectionOpened(ConnectionEvent event) {
195 		Enumeration listeners = connectionListeners.elements();
196 		while (listeners.hasMoreElements()) {
197 			ConnectionListener listener = (ConnectionListener) listeners
198 					.nextElement();
199 			listener.connectionOpened(event);
200 		}
201 	}
202 
203 	/***
204 	 * signal that a connection has been closed
205 	 * 
206 	 * @param event
207 	 *            the event to distribute to each ConnectionListener
208 	 */
209 	protected void signalConnectionClosed(ConnectionEvent event) {
210 		Enumeration listeners = connectionListeners.elements();
211 		while (listeners.hasMoreElements()) {
212 			ConnectionListener listener = (ConnectionListener) listeners
213 					.nextElement();
214 			listener.connectionClosed(event);
215 		}
216 	}
217 
218 	/***
219 	 * signal that a connection has encountered an error
220 	 * 
221 	 * @param exception
222 	 *            the exception that was thrown
223 	 */
224 	protected void signalConnectionFailed(Exception exception) {
225 		Enumeration listeners = connectionListeners.elements();
226 		while (listeners.hasMoreElements()) {
227 			ConnectionListener listener = (ConnectionListener) listeners
228 					.nextElement();
229 			listener.connectionFailed(exception);
230 		}
231 	}
232 
233 	/***
234 	 * signal that a transfer has begun
235 	 */
236 	protected void signalTransferStarted() {
237 		Enumeration listeners = transferListeners.elements();
238 		while (listeners.hasMoreElements()) {
239 			TransferListener listener = (TransferListener) listeners
240 					.nextElement();
241 			listener.transferStarted();
242 		}
243 	}
244 
245 	/***
246 	 * signal that a transfer has completed
247 	 */
248 	protected void signalTransferCompleted() {
249 		Enumeration listeners = transferListeners.elements();
250 		while (listeners.hasMoreElements()) {
251 			TransferListener listener = (TransferListener) listeners
252 					.nextElement();
253 			listener.transferCompleted();
254 		}
255 	}
256 
257 	/***
258 	 * signal that a transfer has completed
259 	 * 
260 	 * @param amount
261 	 *            the amount of data (in octets) transfered
262 	 */
263 	protected void signalTransfered(long amount) {
264 		Enumeration listeners = transferListeners.elements();
265 		while (listeners.hasMoreElements()) {
266 			TransferListener listener = (TransferListener) listeners
267 					.nextElement();
268 			listener.transfered(amount);
269 		}
270 	}
271 
272 }
273 
274 // Getter.java