source: other-projects/trunk/gs3-release-maker/tasks/sshtaskdef/src/mindbright/ssh/SSHSocketFactory.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.8 KB
Line 
1/******************************************************************************
2 *
3 * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4 * www.mindbright.se, [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *****************************************************************************
17 * $Author: mats $
18 * $Date: 1999/08/11 11:55:03 $
19 * $Name: rel1-2-1 $
20 *****************************************************************************/
21package mindbright.ssh;
22
23import java.net.*;
24import java.io.*;
25import java.util.Hashtable;
26import java.util.Enumeration;
27import java.util.Vector;
28
29public class SSHSocketFactory extends SSHClientUserAdaptor {
30
31 public final static int NOLISTENPORT = -1;
32
33 protected SSHAuthenticator authenticator;
34 protected Hashtable clientCache;
35
36 public SSHSocketFactory(String host, int port, SSHAuthenticator authenticator) {
37 super(host, port);
38 this.authenticator = authenticator;
39 this.clientCache = new Hashtable();
40 }
41
42 public SSHSocketFactory(String host, SSHAuthenticator authenticator) {
43 this(host, SSH.DEFAULTPORT, authenticator);
44 }
45
46 protected synchronized SSHClient getClient() {
47 Enumeration e = clientCache.keys();
48 if(!e.hasMoreElements())
49 return null;
50 return (SSHClient)e.nextElement();
51 }
52
53 protected void finalize() throws IOException {
54 Enumeration e = clientCache.keys();
55 while(e.hasMoreElements()) {
56 SSHClient client = (SSHClient)e.nextElement();
57 client.waitForExit(1000);
58 }
59 }
60
61 protected synchronized void addClient(SSHClient client) {
62 clientCache.put(client, new Vector());
63 }
64
65 protected synchronized void registerPseudoUser(SSHClient client, SSHSocketImpl impl) {
66 Vector users = (Vector)clientCache.get(client);
67 users.addElement(impl);
68 }
69
70 protected synchronized void closePseudoAll(SSHClient client) {
71 Vector users = (Vector)clientCache.get(client);
72 if(users == null)
73 return;
74 SSHSocketImpl u;
75 while(users.size() > 0) {
76 u = (SSHSocketImpl)users.elementAt(0);
77 try {
78 u.close();
79 } catch (IOException e) {
80 // !!!
81 }
82 users.removeElementAt(0);
83 }
84 clientCache.remove(client);
85 }
86
87 protected synchronized void closePseudoUser(SSHClient client, SSHSocketImpl u) {
88 Vector users = (Vector)clientCache.get(client);
89 for(int i = 0; i < users.size(); i++) {
90 if(users.elementAt(i) == u) {
91 users.removeElementAt(i);
92 client.delRef();
93 break;
94 }
95 }
96 if(users.size() == 0)
97 clientCache.remove(client);
98 }
99
100 protected SSHClient createSSHClient(int listenPort) throws IOException {
101 SSHClient client;
102 if(listenPort != NOLISTENPORT || ((client = getClient()) == null)) {
103 client = new SSHClient(authenticator, this);
104 // !!! This indicates that we are going to use this client to implement a SSHServerSocket
105 //
106 if(listenPort != NOLISTENPORT)
107 client.addRemotePortForward(listenPort, InetAddress.getLocalHost().getHostAddress(),
108 listenPort, "general");
109 client.bootSSH(false);
110 client.startExitMonitor();
111 addClient(client);
112 }
113 return client;
114 }
115
116 protected SSHSocketImpl createSocketImpl(SSHClient client, boolean acceptor) throws IOException {
117 SSHSocketImpl impl = new SSHSocketImpl();
118
119 impl.setFactory(this);
120 try {
121 impl.create(client, acceptor);
122 } catch (IOException e) {
123 impl.close();
124 throw e;
125 }
126
127 registerPseudoUser(client, impl);
128 return impl;
129 }
130
131 public SSHSocket createSocket(String address, int port) throws IOException {
132 SSHSocketImpl impl = createSocketImpl(createSSHClient(NOLISTENPORT), false);
133 SSHSocket sock = new SSHSocket(impl);
134 try {
135 impl.connect(address, port);
136 } catch (IOException e) {
137 impl.close();
138 throw e;
139 }
140 return sock;
141 }
142
143 public SSHServerSocket createServerSocket(int port, int backlog) throws IOException {
144 SSHSocketImpl impl = createSocketImpl(createSSHClient(port), true);
145 SSHServerSocket sock = new SSHServerSocket(impl);
146 if (port < 0 || port > 0xFFFF)
147 throw new IllegalArgumentException("Port value out of range: " + port);
148
149 impl.setFactory(this);
150 try {
151 impl.bind(InetAddress.getLocalHost(), port);
152 impl.listen(backlog);
153 } catch (IOException e) {
154 impl.close();
155 throw e;
156 }
157 sock.setSocketFactory(this);
158 return sock;
159 }
160
161 public SSHServerSocket createServerSocket(int port) throws IOException {
162 return createServerSocket(port, SSHServerSocket.DEFAULT_BACKLOG);
163 }
164
165 public void setServer(String host, int port) {
166 sshHost = host;
167 sshPort = port;
168 }
169
170 public void setServer(String host) {
171 setServer(host, SSH.DEFAULTPORT);
172 }
173
174 public void setAuthenticator(SSHAuthenticator authenticator) {
175 this.authenticator = authenticator;
176 }
177
178 // SSHClientUser interface "extensions"
179 //
180 public void open(SSHClient client) {
181 // !!! Might want to handle something here, I'm too tired to think of it now (also :-)...
182 }
183
184 public void connected(SSHClient client) {
185 // !!! Might want to handle something here, I'm too tired to think of it now...
186 }
187
188 public void disconnected(SSHClient client, boolean graceful) {
189 closePseudoAll(client);
190 }
191
192 public void report(String msg) {
193 SSH.log("Embedded SSHClient report: " + msg);
194 }
195
196 public void alert(String msg) {
197 SSH.log("Embedded SSHClient alert: " + msg);
198 }
199
200}
Note: See TracBrowser for help on using the repository browser.