1 /***
2 *
3 */
4 package de.cohesion.bssh.impl;
5
6 import java.io.IOException;
7 import java.net.InetAddress;
8 import java.net.UnknownHostException;
9
10 import ch.ethz.ssh2.Connection;
11 import ch.ethz.ssh2.LocalPortForwarder;
12 import de.cohesion.bssh.Member;
13 import de.cohesion.bssh.Result;
14 import de.cohesion.bssh.impl.util.Cache;
15
16 /***
17 * @author schulzs
18 */
19 public class LocalPortForwardCommand extends SSHCommand {
20
21 private final Member remote;
22
23 private final int localPort;
24
25 private LocalPortForwarder forwarder;
26
27 public LocalPortForwardCommand(final Member remote, int localPort,
28 final Cache<Member, Connection> ccache) {
29 super(ccache);
30 this.localPort = localPort;
31 this.remote = remote;
32 }
33
34 @Override
35 protected Result perform(final Member m, final Connection c)
36 throws IOException {
37 if (forwarder != null) {
38 return null;
39 }
40 forwarder = c.createLocalPortForwarder(localPort, remote.getHost()
41 .getHostAddress(), remote.getPort());
42 return null;
43 }
44
45 public Member getProxy() {
46 InetAddress addr = null;
47 try {
48 addr = InetAddress.getByName("localhost");
49 } catch (UnknownHostException uhe) {
50
51 assert false;
52 }
53 return new MemberImpl(addr, remote.getUserName(), localPort, null);
54 }
55
56 public void release() throws IOException {
57 if (forwarder != null) {
58 try {
59 forwarder.close();
60 } finally {
61 forwarder = null;
62 }
63 }
64 }
65
66 }