View Javadoc

1   /***
2    * 
3    */
4   package de.cohesion.bssh.impl;
5   
6   import java.net.InetAddress;
7   import java.net.UnknownHostException;
8   
9   import de.cohesion.bssh.Member;
10  
11  /***
12   * @author schulzs
13   * 
14   */
15  public class MemberImpl implements Member {
16  
17  	private final InetAddress address;
18  
19  	private final String userName;
20  
21  	private final int port;
22  
23  	private final Member gateway;
24  
25  	public static Member parse(final String source, final Member gateway)
26  			throws UnknownHostException {
27  		String userName = source.substring(0, source.indexOf("@")).trim();
28  
29  		int to;
30  		int port;
31  		if (source.contains(":")) {
32  			/* With port. */
33  			to = source.lastIndexOf(':');
34  			port = Integer.parseInt(source.substring(to).trim());
35  		} else {
36  			/* Without port. */
37  			to = source.length();
38  			port = 22;
39  		}
40  
41  		InetAddress address = InetAddress.getByName(source.substring(
42  				source.indexOf("@") + 1, to).trim());
43  
44  		return new MemberImpl(address, userName, port, gateway);
45  	}
46  
47  	public MemberImpl(final InetAddress address, final String userName,
48  			final int port, final Member gateway) {
49  		this.address = address;
50  		this.port = port;
51  		this.userName = userName;
52  		this.gateway = gateway;
53  	}
54  
55  	public InetAddress getHost() {
56  		return address;
57  	}
58  
59  	public String getUserName() {
60  		return userName;
61  	}
62  
63  	public Member getGateway() {
64  		return gateway;
65  	}
66  
67  	public int getPort() {
68  		return port;
69  	}
70  
71  	@Override
72  	public String toString() {
73  		StringBuilder b = new StringBuilder();
74  		b.append(this.getUserName() + "@" + this.getHost() + ":"
75  				+ this.getPort());
76  		Member gateway = this.getGateway();
77  		if (gateway != null) {
78  			b.append(" (Gateway: ");
79  			b.append(gateway);
80  			b.append(")");
81  		}
82  		return b.toString();
83  	}
84  
85  }