3
|
1
|
|
2 /**
|
|
3 * @(#)SocketConnector.java
|
|
4 * @author Stephen M. Kennedy
|
|
5 *
|
|
6 * Copyright: Copyright (c) 2001
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2, or (at your option)
|
|
11 * any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this software; see the file COPYING. If not, write to
|
|
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
21 * Boston, MA 02111-1307 USA
|
|
22 *
|
|
23 */
|
|
24 package org.tn5250j.framework.transport;
|
|
25
|
|
26 import java.net.Socket;
|
|
27
|
10
|
28 import com.five_ten_sg.connectbot.service.TerminalBridge;
|
|
29 import com.five_ten_sg.connectbot.service.TerminalManager;
|
|
30
|
8
|
31 import org.tn5250j.framework.transport.SSL.SSLImplementation;
|
3
|
32 import org.tn5250j.TN5250jConstants;
|
|
33 import org.tn5250j.tools.logging.TN5250jLogFactory;
|
|
34 import org.tn5250j.tools.logging.TN5250jLogger;
|
|
35
|
|
36 public class SocketConnector {
|
|
37
|
|
38 String sslType = null;
|
|
39
|
|
40 TN5250jLogger logger;
|
|
41
|
|
42 /**
|
|
43 * Creates a new instance that creates a plain socket by default.
|
|
44 */
|
|
45 public SocketConnector() {
|
|
46 logger = TN5250jLogFactory.getLogger(getClass());
|
|
47 }
|
|
48
|
|
49 /**
|
|
50 * Set the type of SSL connection to use. Specify null or an empty string
|
8
|
51 * to use a plain socket.
|
3
|
52 * @param type The SSL connection type
|
|
53 * @see org.tn5250j.framework.transport.SSLConstants
|
|
54 */
|
|
55 public void setSSLType(String type) {
|
|
56 sslType = type;
|
|
57 }
|
|
58
|
|
59 /**
|
|
60 * Create a new client Socket to the given destination and port. If an SSL
|
|
61 * socket type has not been specified <i>(by setSSLType(String))</i>, then
|
8
|
62 * a plain socket will be created. Otherwise, a new SSL socket of the
|
3
|
63 * specified type will be created.
|
|
64 * @param destination
|
|
65 * @param port
|
8
|
66 * @return a new client socket, or null if
|
3
|
67 */
|
10
|
68 public Socket createSocket(String destination, int port, TerminalBridge bridge, TerminalManager manager) {
|
3
|
69
|
|
70 Socket socket = null;
|
|
71 Exception ex = null;
|
8
|
72
|
|
73 if (sslType == null || sslType.trim().length() == 0 ||
|
3
|
74 sslType.toUpperCase().equals(TN5250jConstants.SSL_TYPE_NONE)) {
|
|
75 logger.info("Creating Plain Socket");
|
|
76 try {
|
|
77 // Use Socket Constructor!!! SocketFactory for jdk 1.4
|
|
78 socket = new Socket(destination,port);
|
|
79 } catch (Exception e) {
|
|
80 ex = e;
|
|
81 }
|
|
82 } else { //SSL SOCKET
|
|
83
|
8
|
84 logger.info("Creating SSL ["+sslType+"] Socket");
|
|
85
|
3
|
86 SSLInterface sslIf = null;
|
8
|
87
|
3
|
88 try {
|
10
|
89 sslIf = (SSLInterface) new SSLImplementation(bridge, manager);
|
3
|
90 } catch (Exception e) {
|
|
91 ex = new Exception("Failed to create SSLInterface Instance. " +
|
|
92 "Message is ["+e.getMessage()+"]");
|
|
93 }
|
8
|
94
|
3
|
95 if (sslIf != null) {
|
|
96 sslIf.init(sslType);
|
10
|
97 socket = sslIf.createSSLSocket(destination, port);
|
3
|
98 }
|
|
99 }
|
|
100
|
|
101 if (ex != null) {
|
|
102 logger.error(ex);
|
|
103 }
|
|
104 if (socket == null) {
|
|
105 logger.warn("No socket was created");
|
|
106 }
|
|
107 return socket;
|
|
108 }
|
8
|
109
|
|
110
|
3
|
111 } |