View Javadoc

1   // Putter.java
2   // $Id: Putter.java,v 1.7 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.InputStream;
24  import java.util.Enumeration;
25  import java.util.Vector;
26  
27  /***
28   * implements a FTP-style data connection server thread for PUTing files/data.
29   * <P>
30   * This class mainly serves as a superclass to ActivePutter and PassivePutter.
31   */
32  public class Putter extends Thread implements ConnectionEventSource,
33  		TransferEventSource {
34  
35  	public static final int BUFFER_SIZE = 1024;
36  
37  	protected InputStream istream;
38  
39  	protected boolean cancelled = false;
40  
41  	protected Vector connectionListeners;
42  
43  	protected Vector transferListeners;
44  
45  	protected char mode;
46  
47  	protected char type;
48  
49  	/***
50  	 * default constructor
51  	 */
52  	public Putter() {
53  		super();
54  		connectionListeners = new Vector();
55  		transferListeners = new Vector();
56  		this.mode = FtpClientProtocol.MODE_STREAM;
57  		this.type = FtpClientProtocol.TYPE_IMAGE;
58  	}
59  
60  	//
61  	// public methods
62  	//
63  
64  	public synchronized void start() {
65  		this.cancelled = false; // Reset cancelled flag
66  		super.start();
67  	}
68  
69  	/***
70  	 * set the InputStream to use for data input
71  	 * 
72  	 * @param istream
73  	 *            the InputStream to read data from
74  	 */
75  	public synchronized void setInputStream(InputStream istream) {
76  		this.istream = istream;
77  	}
78  
79  	/***
80  	 * set the mode value. valid mode settings (MODE_*) can be found in the
81  	 * FtpClientProtocol class.
82  	 * 
83  	 * @param mode
84  	 *            the new mode setting
85  	 */
86  	public synchronized void setMode(char mode) {
87  		this.mode = mode;
88  	}
89  
90  	/***
91  	 * set the type value. valid type settings (TYPE_*) can be found in the
92  	 * FtpClientProtocol class.
93  	 * 
94  	 * @param type
95  	 *            the new type setting
96  	 */
97  	public synchronized void setType(char type) {
98  		this.type = type;
99  	}
100 
101 	/***
102 	 * cancel a running transfer sets a flag and calls interrupt() can only be
103 	 * called once
104 	 */
105 	public void cancel() {
106 		if (!cancelled) {
107 			cancelled = true;
108 			interrupt();
109 		}
110 	}
111 
112 	/***
113 	 * add a ConnectionListener to the list of connectionListeners
114 	 * 
115 	 * @param listener
116 	 *            the ConnectionListener to add to the list
117 	 */
118 	public void addConnectionListener(ConnectionListener listener) {
119 		connectionListeners.addElement(listener);
120 	}
121 
122 	/***
123 	 * add a set of ConnectionListeners to the list of connectionListeners
124 	 * 
125 	 * @param listeners
126 	 *            the ConnectionListeners to add to the list
127 	 */
128 	public void addConnectionListeners(Vector listeners) {
129 		Enumeration e = listeners.elements();
130 		while (e.hasMoreElements()) {
131 			ConnectionListener listener = (ConnectionListener) e.nextElement();
132 			connectionListeners.addElement(listener);
133 		}
134 	}
135 
136 	/***
137 	 * De-register a ConnectionListener with the event source
138 	 * 
139 	 * @param listener
140 	 *            the ConnectionListener to remove from the list of listeners
141 	 */
142 	public void removeConnectionListener(ConnectionListener listener) {
143 		connectionListeners.removeElement(listener);
144 	}
145 
146 	/***
147 	 * register a TransferListener to the list of transfer listeners. Each
148 	 * transfer listener registered with the event source will be notified when
149 	 * a transfer event occurs.
150 	 * 
151 	 * @param listener
152 	 *            the TransferListener to register with the event source
153 	 */
154 	public void addTransferListener(TransferListener listener) {
155 		transferListeners.addElement(listener);
156 	}
157 
158 	/***
159 	 * add a set of TransferListeners to the list of transfer listeners
160 	 * 
161 	 * @param listeners
162 	 *            the TransferListeners to add to the list
163 	 */
164 	public void addTransferListeners(Vector listeners) {
165 		Enumeration e = listeners.elements();
166 		while (e.hasMoreElements()) {
167 			TransferListener listener = (TransferListener) e.nextElement();
168 			transferListeners.addElement(listener);
169 		}
170 	}
171 
172 	/***
173 	 * De-register a TransferListener with the event source.
174 	 * 
175 	 * @param listener
176 	 *            the TransferListener to de-register with the event source
177 	 */
178 	public void removeTransferListener(TransferListener listener) {
179 		transferListeners.removeElement(listener);
180 	}
181 
182 	/***
183 	 * signal that a connection has been opened
184 	 * 
185 	 * @param event
186 	 *            the event to distribute to each ConnectionListener
187 	 */
188 	protected void signalConnectionOpened(ConnectionEvent event) {
189 		Enumeration listeners = connectionListeners.elements();
190 		while (listeners.hasMoreElements()) {
191 			ConnectionListener listener = (ConnectionListener) listeners
192 					.nextElement();
193 			listener.connectionOpened(event);
194 		}
195 	}
196 
197 	/***
198 	 * signal that a connection has been closed
199 	 * 
200 	 * @param event
201 	 *            the event to distribute to each ConnectionListener
202 	 */
203 	protected void signalConnectionClosed(ConnectionEvent event) {
204 		Enumeration listeners = connectionListeners.elements();
205 		while (listeners.hasMoreElements()) {
206 			ConnectionListener listener = (ConnectionListener) listeners
207 					.nextElement();
208 			listener.connectionClosed(event);
209 		}
210 	}
211 
212 	/***
213 	 * signal that a connection has encountered an error
214 	 * 
215 	 * @param exception
216 	 *            the exception that was thrown
217 	 */
218 	protected void signalConnectionFailed(Exception exception) {
219 		Enumeration listeners = connectionListeners.elements();
220 		while (listeners.hasMoreElements()) {
221 			ConnectionListener listener = (ConnectionListener) listeners
222 					.nextElement();
223 			listener.connectionFailed(exception);
224 		}
225 	}
226 
227 	/***
228 	 * signal that a transfer has started
229 	 */
230 	protected void signalTransferStarted() {
231 		Enumeration listeners = transferListeners.elements();
232 		while (listeners.hasMoreElements()) {
233 			TransferListener listener = (TransferListener) listeners
234 					.nextElement();
235 			listener.transferStarted();
236 		}
237 	}
238 
239 	/***
240 	 * signal that a transfer has completed
241 	 */
242 	protected void signalTransferCompleted() {
243 		Enumeration listeners = transferListeners.elements();
244 		while (listeners.hasMoreElements()) {
245 			TransferListener listener = (TransferListener) listeners
246 					.nextElement();
247 			listener.transferCompleted();
248 		}
249 	}
250 
251 	/***
252 	 * signal that a transfer has completed
253 	 * 
254 	 * @param amount
255 	 *            the amount of data (in octets) that has been transfered
256 	 */
257 	protected void signalTransfered(long amount) {
258 		Enumeration listeners = transferListeners.elements();
259 		while (listeners.hasMoreElements()) {
260 			TransferListener listener = (TransferListener) listeners
261 					.nextElement();
262 			listener.transfered(amount);
263 		}
264 	}
265 
266 }
267 
268 // Putter.java