1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*******************************************************************************
* Copyright (c) 2004 Composent, Inc. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Composent, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.server.generic.app;
import java.net.InetAddress;
import java.util.*;
import org.eclipse.ecf.provider.generic.TCPServerSOContainer;
public class Connector {
public static final int DEFAULT_PORT = TCPServerSOContainer.DEFAULT_PORT;
public static final int DEFAULT_TIMEOUT = TCPServerSOContainer.DEFAULT_KEEPALIVE;
public static final String DEFAULT_HOSTNAME = TCPServerSOContainer.DEFAULT_HOST;
public static final String DEFAULT_SERVERNAME = TCPServerSOContainer.DEFAULT_NAME;
public static final String DEFAULT_PROTOCOL = TCPServerSOContainer.DEFAULT_PROTOCOL;
int port = DEFAULT_PORT;
int timeout = DEFAULT_TIMEOUT;
String protocol = DEFAULT_PROTOCOL;
String hostname = DEFAULT_HOSTNAME;
boolean discovery = false;
List groups = new ArrayList();
public Connector(String protocol, String host, int port, int timeout, boolean discovery) {
if (protocol != null && !protocol.equals(""))this.protocol = protocol; //$NON-NLS-1$
if (host != null && !host.equals(""))this.hostname = host; //$NON-NLS-1$
else {
try {
InetAddress addr = InetAddress.getLocalHost();
this.hostname = getHostNameForAddressWithoutLookup(addr);
} catch (Exception e) {
this.hostname = "localhost"; //$NON-NLS-1$
}
}
this.port = port;
this.timeout = timeout;
this.discovery = discovery;
}
private String getHostNameForAddressWithoutLookup(InetAddress inetAddress) {
// First get InetAddress.toString(), which returns
// the inet address in this form: "hostName/address".
// If hostname is not resolved the result is: "/address"
// So first we detect the location of the "/" to determine
// whether the host name is there or not
String inetAddressStr = inetAddress.toString();
int slashPos = inetAddressStr.indexOf('/');
if (slashPos == 0)
// no hostname is available so we strip
// off '/' and return address as string
return inetAddressStr.substring(1);
// hostname is there/non-null, so we use it
return inetAddressStr.substring(0, slashPos);
}
public Connector(String protocol, String host, int port, int timeout) {
this(protocol, host, port, timeout, false);
}
public boolean shouldRegisterForDiscovery() {
return discovery;
}
public boolean addGroup(NamedGroup grp) {
if (grp == null)
return false;
for (Iterator i = groups.iterator(); i.hasNext();) {
NamedGroup namedGroup = (NamedGroup) i.next();
if (namedGroup.getName().equals(grp.getName()))
return false;
}
groups.add(grp);
grp.setParent(this);
return true;
}
public String getProtocol() {
return protocol;
}
public String getHostname() {
return hostname;
}
public int getPort() {
return port;
}
public int getTimeout() {
return timeout;
}
public List getGroups() {
return groups;
}
public String getID() {
return getProtocol() + "://" + getHostname() + ":" + getPort(); //$NON-NLS-1$ //$NON-NLS-2$
}
}
|