comparison src/org/tn5250j/framework/transport/SocketConnector.java @ 3:e8d2a24e85c6 tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 12:11:10 -0700
parents
children 3b760b39962a
comparison
equal deleted inserted replaced
2:a01665cb683d 3:e8d2a24e85c6
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
28 import org.tn5250j.TN5250jConstants;
29 import org.tn5250j.tools.logging.TN5250jLogFactory;
30 import org.tn5250j.tools.logging.TN5250jLogger;
31
32 public class SocketConnector {
33
34 String sslType = null;
35
36 TN5250jLogger logger;
37
38 /**
39 * Creates a new instance that creates a plain socket by default.
40 */
41 public SocketConnector() {
42 logger = TN5250jLogFactory.getLogger(getClass());
43 }
44
45 /**
46 * Set the type of SSL connection to use. Specify null or an empty string
47 * to use a plain socket.
48 * @param type The SSL connection type
49 * @see org.tn5250j.framework.transport.SSLConstants
50 */
51 public void setSSLType(String type) {
52 sslType = type;
53 }
54
55 /**
56 * Create a new client Socket to the given destination and port. If an SSL
57 * socket type has not been specified <i>(by setSSLType(String))</i>, then
58 * a plain socket will be created. Otherwise, a new SSL socket of the
59 * specified type will be created.
60 * @param destination
61 * @param port
62 * @return a new client socket, or null if
63 */
64 public Socket createSocket(String destination, int port) {
65
66 Socket socket = null;
67 Exception ex = null;
68
69 if (sslType == null || sslType.trim().length() == 0 ||
70 sslType.toUpperCase().equals(TN5250jConstants.SSL_TYPE_NONE)) {
71 logger.info("Creating Plain Socket");
72 try {
73 // Use Socket Constructor!!! SocketFactory for jdk 1.4
74 socket = new Socket(destination,port);
75 } catch (Exception e) {
76 ex = e;
77 }
78 } else { //SSL SOCKET
79
80 logger.info("Creating SSL ["+sslType+"] Socket");
81
82 SSLInterface sslIf = null;
83
84 String sslImplClassName =
85 "org.tn5250j.framework.transport.SSL.SSLImplementation";
86 try {
87 Class<?> c = Class.forName(sslImplClassName);
88 sslIf = (SSLInterface)c.newInstance();
89 } catch (Exception e) {
90 ex = new Exception("Failed to create SSLInterface Instance. " +
91 "Message is ["+e.getMessage()+"]");
92 }
93
94 if (sslIf != null) {
95 sslIf.init(sslType);
96 socket = sslIf.createSSLSocket(destination,port);
97 }
98 }
99
100 if (ex != null) {
101 logger.error(ex);
102 }
103 if (socket == null) {
104 logger.warn("No socket was created");
105 }
106 return socket;
107 }
108
109
110 }