本文整理了Java中brooklyn.util.net.Networking
类的一些代码示例,展示了Networking
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Networking
类的具体详情如下:
包路径:brooklyn.util.net.Networking
类名称:Networking
暂无
代码示例来源:origin: io.brooklyn/brooklyn-core
@Override
public InetAddress apply(String input) {
return Networking.getInetAddressWithFixedName(input);
}
});
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
/** As {@link #isPrivateSubnet(InetAddress)} but taking a string; sepcifically local-only address ARE treated as private.
* does not require the string to be resolvable, and things which aren't resolvable are treated as private
* unless they are known to be local-only */
public static boolean isPrivateSubnet(String hostnameOrIp) {
Preconditions.checkNotNull(hostnameOrIp, "hostnameOrIp");
try {
InetAddress ia = getInetAddressWithFixedName(hostnameOrIp);
return isPrivateSubnet(ia);
} catch (Exception e) {
log.debug("Networking cannot resolve "+hostnameOrIp+": assuming it IS a private address");
return true;
}
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
public static void checkPortsValid(@SuppressWarnings("rawtypes") Map ports) {
for (Object ppo : ports.entrySet()) {
Map.Entry<?,?> pp = (Map.Entry<?,?>)ppo;
Object val = pp.getValue();
if(val == null){
throw new IllegalArgumentException("port for "+pp.getKey()+" is null");
}else if (!(val instanceof Integer)) {
throw new IllegalArgumentException("port "+val+" for "+pp.getKey()+" is not an integer ("+val.getClass()+")");
}
checkPortValid((Integer)val, ""+pp.getKey());
}
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
public static InetAddress getInetAddressWithFixedName(int ip1, int ip2, int ip3, int ip4, int ip5, int ip6) {
return getInetAddressWithFixedName(asByteArray(ip1, ip2, ip3, ip4, ip5, ip6));
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
/** As {@link #isLocalOnly(InetAddress)} but taking a string;
* does not require the string to be resolvable, and generally treats non-resolvable hostnames as NOT local-only
* (although they are treated as private by {@link #isPrivateSubnet(String)}),
* although certain well-known hostnames are recognised as local-only */
public static boolean isLocalOnly(String hostnameOrIp) {
Preconditions.checkNotNull(hostnameOrIp, "hostnameOrIp");
if ("127.0.0.1".equals(hostnameOrIp)) return true;
if ("localhost".equals(hostnameOrIp)) return true;
if ("localhost.localdomain".equals(hostnameOrIp)) return true;
try {
InetAddress ia = getInetAddressWithFixedName(hostnameOrIp);
return isLocalOnly(ia);
} catch (Exception e) {
log.debug("Networking cannot resolve "+hostnameOrIp+": assuming it is not a local-only address, but it is a private address");
return false;
}
}
代码示例来源:origin: io.brooklyn/brooklyn-software-nosql
@Override
public void customize() {
Map ports = ImmutableMap.of("port", getServerPort());
Networking.checkPortsValid(ports);
String command = String.format("mkdir -p %s", getDataDirectory());
newScript(CUSTOMIZING)
.updateTaskAndFailOnNonZeroResultCode()
.body.append(command).execute();
String templateUrl = entity.getConfig(MongoDBServer.MONGODB_CONF_TEMPLATE_URL);
if (!Strings.isNullOrEmpty(templateUrl)) copyTemplate(templateUrl, getConfFile());
}
代码示例来源:origin: io.brooklyn/brooklyn-core
/** returns URL to get properties for the given address (assuming localhost if address is on a subnet) */
public String getLookupUrlFor(InetAddress address) {
if (Networking.isPrivateSubnet(address)) return getLookupUrlForLocalhost();
return getLookupUrlForPublicIp(address.getHostAddress());
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
/** returns the first port available on the local machine >= the port supplied */
public static int nextAvailablePort(int port) {
while (!isPortAvailable(port)) port++;
return port;
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
/**
* Gets an InetAddress using the given hostname or IP. If it is an IPv4 address, then this is equivalent
* to {@link getInetAddressWithFixedName(byte[])}. If it is a hostname, then this hostname will be used
* in the returned InetAddress.
*/
public static InetAddress getInetAddressWithFixedName(String hostnameOrIp) {
try {
if (isValidIp4(hostnameOrIp)) {
byte[] ip = new byte[4];
String[] parts = hostnameOrIp.split("\\.");
assert parts.length == 4 : "val="+hostnameOrIp+"; split="+Arrays.toString(parts)+"; length="+parts.length;
for (int i = 0; i < parts.length; i++) {
ip[i] = (byte)Integer.parseInt(parts[i]);
}
return InetAddress.getByAddress(hostnameOrIp, ip);
} else {
return InetAddress.getByName(hostnameOrIp);
}
} catch (UnknownHostException e) {
throw Throwables.propagate(e);
}
}
代码示例来源:origin: io.brooklyn/brooklyn-locations-jclouds
protected Optional<String> getPrivateAddress() {
if (truth(node.getPrivateAddresses())) {
Iterator<String> pi = node.getPrivateAddresses().iterator();
while (pi.hasNext()) {
String p = pi.next();
// disallow local only addresses
if (Networking.isLocalOnly(p)) continue;
// other things may be public or private, but either way, return it
return Optional.of(p);
}
}
return Optional.absent();
}
代码示例来源:origin: io.brooklyn/brooklyn-software-nosql
@Override
public void customize() {
log.info("Customizing {} (Cluster {})", entity, getClusterName());
Networking.checkPortsValid(getPortMap());
newScript(CUSTOMIZING).execute();
// Copy the configuration files across
String configFileContents = processTemplate(getCouchDBConfigTemplateUrl());
String destinationConfigFile = String.format("%s/%s", getRunDir(), getCouchDBConfigFileName());
getMachine().copyTo(new ByteArrayInputStream(configFileContents.getBytes()), destinationConfigFile);
String uriFileContents = processTemplate(getCouchDBUriTemplateUrl());
String destinationUriFile = String.format("%s/couch.uri", getRunDir());
getMachine().copyTo(new ByteArrayInputStream(uriFileContents.getBytes()), destinationUriFile);
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
public static InetAddress getInetAddressWithFixedName(int ip1, int ip2, int ip3, int ip4) {
return getInetAddressWithFixedName(asByteArray(ip1, ip2, ip3, ip4));
}
代码示例来源:origin: io.brooklyn/brooklyn-core
/** returns URL to get properties for the given address (assuming localhost if address is on a subnet) */
public String getPropertiesLookupUrlFor(InetAddress address) {
if (Networking.isPrivateSubnet(address)) return getPropertiesLookupUrlForLocalhost();
return getPropertiesLookupUrlForPublicIp(address.getHostAddress());
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
public static boolean isPortAvailable(int port) {
try {
return isPortAvailable(InetAddress.getByName("localhost"), port);
} catch (UnknownHostException e) {
if (!loggedLocalhostNotAvailable) {
loggedLocalhostNotAvailable = true;
log.warn("localhost unavailable during port availability check for "+port+": "+e+"; ignoring, but this may be a sign of network misconfiguration");
}
return isPortAvailable(null, port);
}
}
public static boolean isPortAvailable(InetAddress localAddress, int port) {
代码示例来源:origin: io.brooklyn/brooklyn-locations-jclouds
@Override
public String getSubnetIp() {
Optional<String> privateAddress = getPrivateAddress();
if (privateAddress.isPresent()) {
return privateAddress.get();
}
String hostname = jcloudsParent.getPublicHostname(node, getRawLocalConfigBag());
if (hostname != null && !Networking.isValidIp4(hostname)) {
try {
return InetAddress.getByName(hostname).getHostAddress();
} catch (UnknownHostException e) {
LOG.debug("Cannot resolve IP for hostname {} of machine {} (so returning hostname): {}", new Object[] {hostname, this, e});
}
}
return hostname;
}
代码示例来源:origin: io.brooklyn/brooklyn-software-messaging
@Override
public void customize() {
Networking.checkPortsValid(MutableMap.of("amqpPort", getAmqpPort()));
newScript(CUSTOMIZING)
.body.append(
format("cp -R %s/* .", getExpandedInstallDir())
)
.execute();
}
代码示例来源:origin: io.brooklyn/brooklyn-utils-common
/** returns the netmask for this cidr; e.g. for a /24 cidr returns 255.255.255.0 */
public InetAddress netmask() {
final byte[] netmaskBytes = new byte[] { 0, 0, 0, 0 };
int lengthLeft = length;
int i=0;
while (lengthLeft>0) {
if (lengthLeft>=8) {
netmaskBytes[i] = (byte)255;
} else {
netmaskBytes[i] = (byte) ( (1 << lengthLeft) - 1 );
}
lengthLeft -= 8;
i++;
}
return Networking.getInetAddressWithFixedName(netmaskBytes);
}
代码示例来源:origin: io.brooklyn/brooklyn-core
if (Networking.isPrivateSubnet(extAddress)) extAddress = InetAddress.getByName(UtraceHostGeoLookup.getLocalhostExternalIp());
代码示例来源:origin: io.brooklyn/brooklyn-core
/** checks the actual availability of the port on localhost, ie by binding to it; cf {@link Networking#isPortAvailable(int)} */
public static boolean checkPortAvailable(InetAddress localAddress, int portNumber) {
if (portNumber<1024) {
if (LOG.isDebugEnabled()) LOG.debug("Skipping system availability check for privileged localhost port "+portNumber);
return true;
}
return Networking.isPortAvailable(portNumber);
}
public static int obtainPort(PortRange range) {
代码示例来源:origin: io.brooklyn/brooklyn-locations-jclouds
/** returns the hostname (or sometimes IP) for use by peers in the same subnet,
* defaulting to public hostname if nothing special
* <p>
* for use e.g. in clouds like amazon where other machines
* in the same subnet need to use a different IP
*/
@Override
public String getSubnetHostname() {
String publicHostname = jcloudsParent.getPublicHostname(node, getRawLocalConfigBag());
if ("aws-ec2".equals(jcloudsParent.getProvider())) {
// prefer hostname over IP for aws (resolves to private ip in subnet, and to public from outside)
if (!Networking.isValidIp4(publicHostname)) {
return publicHostname; // assume it's a hostname; could check for ip6!
}
}
Optional<String> privateAddress = getPrivateAddress();
return privateAddress.isPresent() ? privateAddress.get() : publicHostname;
}
内容来源于网络,如有侵权,请联系作者删除!