KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > slave > ActiveConnection


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.slave;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetSocketAddress JavaDoc;
22 import java.net.Socket JavaDoc;
23 import java.net.SocketAddress JavaDoc;
24
25 import javax.net.SocketFactory;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.SSLSocket;
28
29 import org.apache.log4j.Logger;
30
31 /**
32  * @author mog
33  * @version $Id: ActiveConnection.java,v 1.7 2004/02/10 00:03:16 mog Exp $
34  */

35 public class ActiveConnection extends Connection {
36     private static final Logger logger =
37         Logger.getLogger(ActiveConnection.class);
38     private SSLContext _ctx;
39     private SocketAddress JavaDoc _addr;
40     private Socket JavaDoc _sock;
41     public ActiveConnection(SSLContext ctx, InetSocketAddress JavaDoc addr) {
42         _addr = addr;
43         _ctx = ctx;
44     }
45
46     public Socket JavaDoc connect() throws IOException JavaDoc {
47         if (_ctx != null) {
48             SSLSocket sslsock;
49             sslsock = (SSLSocket) _ctx.getSocketFactory().createSocket();
50             sslsock.connect(_addr, TIMEOUT);
51             sslsock.setUseClientMode(false);
52             sslsock.startHandshake();
53             _sock = sslsock;
54         } else {
55             _sock = SocketFactory.getDefault().createSocket();
56             _sock.connect(_addr, TIMEOUT);
57         }
58         setSockOpts(_sock);
59         Socket JavaDoc sock = _sock;
60         _sock = null;
61         return sock;
62     }
63
64     public void abort() {
65         try {
66             if (_sock != null)
67                 _sock.close();
68         } catch (IOException JavaDoc e) {
69             logger.warn("abort() failed to close() socket", e);
70         }
71     }
72
73 }
74
Popular Tags