KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > ftp > FTPDataSession


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.ftp;
18
19 import java.net.*;
20 import java.io.*;
21
22 /**
23  * FTP Data Session Class
24  * <p>
25  * A data connection is made when a PORT or PASV FTP command is received on the main control
26  * session.
27  * <p>
28  * The PORT command will actively connect to the specified address/port on the client. The PASV
29  * command will create a listening socket and wait for the client to connect.
30  *
31  * @author GKSpencer
32  */

33 public class FTPDataSession implements Runnable JavaDoc
34 {
35
36     // FTP session that this data connection is associated with
37

38     private FTPSrvSession m_cmdSess;
39
40     // Connection details for active connection
41

42     private InetAddress m_clientAddr;
43     private int m_clientPort;
44
45     // Local port to use
46

47     private int m_localPort;
48
49     // Active data session socket
50

51     private Socket m_activeSock;
52
53     // Passive data session socket
54

55     private ServerSocket m_passiveSock;
56
57     // Adapter to bind the passive socket to
58

59     private InetAddress m_bindAddr;
60
61     // Transfer in progress and abort file transfer flags
62

63     private boolean m_transfer;
64     private boolean m_abort;
65
66     // Send/receive data byte count
67

68     private long m_bytCount;
69
70     /**
71      * Class constructor
72      * <p>
73      * Create a data connection that listens for an incoming connection.
74      *
75      * @param sess FTPSrvSession
76      * @exception IOException
77      */

78     protected FTPDataSession(FTPSrvSession sess) throws IOException
79     {
80
81         // Set the associated command session
82

83         m_cmdSess = sess;
84
85         // Create a server socket to listen for the incoming connection
86

87         m_passiveSock = new ServerSocket(0, 1, null);
88     }
89
90     /**
91      * Class constructor
92      * <p>
93      * Create a data connection that listens for an incoming connection on the specified network
94      * adapter and local port.
95      *
96      * @param sess FTPSrvSession
97      * @param localPort int
98      * @param addr InetAddress
99      * @exception IOException
100      */

101     protected FTPDataSession(FTPSrvSession sess, int localPort, InetAddress bindAddr) throws IOException
102     {
103
104         // Set the associated command session
105

106         m_cmdSess = sess;
107
108         // Create a server socket to listen for the incoming connection on the specified network
109
// adapter
110

111         m_localPort = localPort;
112         m_passiveSock = new ServerSocket(localPort, 1, bindAddr);
113     }
114
115     /**
116      * Class constructor
117      * <p>
118      * Create a data connection that listens for an incoming connection on the specified network
119      * adapter.
120      *
121      * @param sess FTPSrvSession
122      * @param addr InetAddress
123      * @exception IOException
124      */

125     protected FTPDataSession(FTPSrvSession sess, InetAddress bindAddr) throws IOException
126     {
127
128         // Set the associated command session
129

130         m_cmdSess = sess;
131
132         // Create a server socket to listen for the incoming connection on the specified network
133
// adapter
134

135         m_passiveSock = new ServerSocket(0, 1, bindAddr);
136     }
137
138     /**
139      * Class constructor
140      * <p>
141      * Create a data connection to the specified client address and port.
142      *
143      * @param sess FTPSrvSession
144      * @param addr InetAddress
145      * @param port int
146      */

147     protected FTPDataSession(FTPSrvSession sess, InetAddress addr, int port)
148     {
149
150         // Set the associated command session
151

152         m_cmdSess = sess;
153
154         // Save the client address/port details, the actual connection will be made later when
155
// the client requests/sends a file
156

157         m_clientAddr = addr;
158         m_clientPort = port;
159     }
160
161     /**
162      * Class constructor
163      * <p>
164      * Create a data connection to the specified client address and port, using the specified local
165      * port.
166      *
167      * @param sess FTPSrvSession
168      * @param localPort int
169      * @param addr InetAddress
170      * @param port int
171      */

172     protected FTPDataSession(FTPSrvSession sess, int localPort, InetAddress addr, int port)
173     {
174
175         // Set the associated command session
176

177         m_cmdSess = sess;
178
179         // Save the local port
180

181         m_localPort = localPort;
182
183         // Save the client address/port details, the actual connection will be made later when
184
// the client requests/sends a file
185

186         m_clientAddr = addr;
187         m_clientPort = port;
188     }
189
190     /**
191      * Return the associated command session
192      *
193      * @return FTPSrvSession
194      */

195     public final FTPSrvSession getCommandSession()
196     {
197         return m_cmdSess;
198     }
199
200     /**
201      * Return the local port
202      *
203      * @return int
204      */

205     public final int getLocalPort()
206     {
207         if (m_passiveSock != null)
208             return m_passiveSock.getLocalPort();
209         else if (m_activeSock != null)
210             return m_activeSock.getLocalPort();
211         return -1;
212     }
213
214     /**
215      * Return the port that was allocated to the data session
216      *
217      * @return int
218      */

219     protected final int getAllocatedPort()
220     {
221         return m_localPort;
222     }
223
224     /**
225      * Return the passive server socket address
226      *
227      * @return InetAddress
228      */

229     public final InetAddress getPassiveAddress()
230     {
231         if (m_passiveSock != null)
232         {
233
234             // Get the server socket local address
235

236             InetAddress addr = m_passiveSock.getInetAddress();
237             if (addr.getHostAddress().compareTo("0.0.0.0") == 0)
238             {
239                 try
240                 {
241                     addr = InetAddress.getLocalHost();
242                 }
243                 catch (UnknownHostException ex)
244                 {
245                 }
246             }
247             return addr;
248         }
249         return null;
250     }
251
252     /**
253      * Return the passive server socket port
254      *
255      * @return int
256      */

257     public final int getPassivePort()
258     {
259         if (m_passiveSock != null)
260             return m_passiveSock.getLocalPort();
261         return -1;
262     }
263
264     /**
265      * Determine if a file transfer is active
266      *
267      * @return boolean
268      */

269     public final boolean isTransferActive()
270     {
271         return m_transfer;
272     }
273
274     /**
275      * Abort an in progress file transfer
276      */

277     public final void abortTransfer()
278     {
279         m_abort = true;
280     }
281
282     /**
283      * Return the transfer byte count
284      *
285      * @return long
286      */

287     public final synchronized long getTransferByteCount()
288     {
289         return m_bytCount;
290     }
291
292     /**
293      * Return the data socket connected to the client
294      *
295      * @return Socket
296      * @exception IOException
297      */

298     public final Socket getSocket() throws IOException
299     {
300
301         // Check for a passive connection, get the incoming socket connection
302

303         if (m_passiveSock != null)
304             m_activeSock = m_passiveSock.accept();
305         else
306         {
307             if (m_localPort != 0)
308             {
309
310                 // Use the specified local port
311

312                 m_activeSock = new Socket(m_clientAddr, m_clientPort, null, m_localPort);
313             }
314             else
315                 m_activeSock = new Socket(m_clientAddr, m_clientPort);
316         }
317
318         // Set the socket to close immediately
319

320         m_activeSock.setSoLinger(false, 0);
321         m_activeSock.setTcpNoDelay(true);
322
323         // Return the data socket
324

325         return m_activeSock;
326     }
327
328     /**
329      * Close the data connection
330      */

331     public final void closeSession()
332     {
333
334         // If the data connection is active close it
335

336         if (m_activeSock != null)
337         {
338             try
339             {
340                 m_activeSock.close();
341             }
342             catch (Exception JavaDoc ex)
343             {
344             }
345             m_activeSock = null;
346         }
347
348         // Close the listening socket for a passive connection
349

350         if (m_passiveSock != null)
351         {
352             try
353             {
354                 m_passiveSock.close();
355             }
356             catch (Exception JavaDoc ex)
357             {
358             }
359             m_passiveSock = null;
360         }
361     }
362
363     /**
364      * Run a file send/receive in a seperate thread
365      */

366     public void run()
367     {
368     }
369 }
370
Popular Tags