下面列出了java.net.UnknownHostException#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Resolves the host and port InetSocketAddress the web server is listening to.
* <p>
* This implementation looks for the following 2 properties:
* <ul>
* <li>#SERVER_NAME#.http.hostname</li>
* <li>#SERVER_NAME#.http.port</li>
* </ul>
*
* @return the host and port InetSocketAddress the web server is listening to.
* @throws ServerException thrown if any of the above 2 properties is not defined.
*/
protected InetSocketAddress resolveAuthority() throws ServerException {
String hostnameKey = getName() + HTTP_HOSTNAME;
String portKey = getName() + HTTP_PORT;
String host = System.getProperty(hostnameKey);
String port = System.getProperty(portKey);
if (host == null) {
throw new ServerException(ServerException.ERROR.S13, hostnameKey);
}
if (port == null) {
throw new ServerException(ServerException.ERROR.S13, portKey);
}
try {
InetAddress add = InetAddress.getByName(host);
int portNum = Integer.parseInt(port);
return new InetSocketAddress(add, portNum);
} catch (UnknownHostException ex) {
throw new ServerException(ServerException.ERROR.S14, ex.toString(), ex);
}
}
/**
* Resolves the host and port InetSocketAddress the web server is listening to.
* <p>
* This implementation looks for the following 2 properties:
* <ul>
* <li>#SERVER_NAME#.http.hostname</li>
* <li>#SERVER_NAME#.http.port</li>
* </ul>
*
* @return the host and port InetSocketAddress the web server is listening to.
* @throws ServerException thrown if any of the above 2 properties is not defined.
*/
protected InetSocketAddress resolveAuthority() throws ServerException {
String hostnameKey = getName() + HTTP_HOSTNAME;
String portKey = getName() + HTTP_PORT;
String host = System.getProperty(hostnameKey);
String port = System.getProperty(portKey);
if (host == null) {
throw new ServerException(ServerException.ERROR.S13, hostnameKey);
}
if (port == null) {
throw new ServerException(ServerException.ERROR.S13, portKey);
}
try {
InetAddress add = InetAddress.getByName(host);
int portNum = Integer.parseInt(port);
return new InetSocketAddress(add, portNum);
} catch (UnknownHostException ex) {
throw new ServerException(ServerException.ERROR.S14, ex.toString(), ex);
}
}
protected void recoverServices(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
String name = context.getCurrentAddressValue();
ServiceName svcName = SOCKET_BINDING_CAPABILITY.getCapabilityServiceName(name);
ServiceRegistry registry = context.getServiceRegistry(true);
ServiceController<?> controller = registry.getService(svcName);
if (controller != null) {
// We didn't remove it, we just set reloadRequired
context.revertReloadRequired();
} else {
try {
BindingAddHandler.installBindingService(context, model, name);
}catch (UnknownHostException e) {
throw new OperationFailedException(e.toString());
}
}
}
/**
* Get InetAddress masked with prefixLength. Will never return null.
* @param address the IP address to mask with
* @param prefixLength the prefixLength used to mask the IP
*/
public static InetAddress getNetworkPart(InetAddress address, int prefixLength) {
byte[] array = address.getAddress();
maskRawAddress(array, prefixLength);
InetAddress netPart = null;
try {
netPart = InetAddress.getByAddress(array);
} catch (UnknownHostException e) {
throw new RuntimeException("getNetworkPart error - " + e.toString());
}
return netPart;
}
public void setHost(String host)
{
try
{
InetAddress hostAddress = InetAddress.getByName(host);
if (!hostAddress.isAnyLocalAddress())
{
this.host = hostAddress;
}
}
catch (UnknownHostException e)
{
throw new RuntimeException(e.toString());
}
}
public void configure(String name, Properties props, NameSpace nameSpace)
throws LDAPException {
this.name = name;
this.host = props.getProperty("host");
try {
addr = UniAddress.getByName(host);
} catch (UnknownHostException e) {
throw new LDAPException("Could not lookup host : " + e.toString(),
LDAPException.OPERATIONS_ERROR, "");
}
base = nameSpace.getBase().getDN().toString();
}
public void configure(String name, Properties props, NameSpace nameSpace)
throws LDAPException {
this.name = name;
this.host = props.getProperty("host");
try {
addr = UniAddress.getByName(host);
} catch (UnknownHostException e) {
throw new LDAPException("Could not lookup host : " + e.toString(),
LDAPException.OPERATIONS_ERROR, "");
}
base = nameSpace.getBase().getDN().toString();
}
private void handleBindingReinstall(OperationContext context, String bindingName, ModelNode bindingModel, ServiceName serviceName) throws OperationFailedException {
context.removeService(serviceName);
try {
BindingAddHandler.installBindingService(context, bindingModel, bindingName);
} catch (UnknownHostException e) {
throw new OperationFailedException(e.toString());
}
}
@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
PathAddress address = PathAddress.pathAddress(operation.get(OP_ADDR));
String name = address.getLastElement().getValue();
try {
installBindingService(context, model, name);
} catch (UnknownHostException e) {
throw new OperationFailedException(e.toString());
}
}
/**
* Connect to MongoDB Host.
*
* @throws DBException
*/
public void connect() throws DBException {
try {
LOGGER.debug("Initializing MongoDB client");
mongoClient = new MongoClient(this.host, this.port);
} catch (UnknownHostException e) {
throw new DBException(e.toString());
}
}
@Test
public void itMakesFriendlyErrorForHostNotFound () throws Exception {
final UnknownHostException uhe = new UnknownHostException("Unable to resolve host \"successwhale-api.herokuapp.com\": No address associated with hostname");
final SuccessWhaleException swe = new SuccessWhaleException("Failed to fetch feed 'somefeed' from 'someurl': " + uhe.toString(), uhe);
assertEquals("Network error: Unable to resolve host \"successwhale-api.herokuapp.com\": No address associated with hostname",
swe.friendlyMessage());
}