Search in sources :

Example 21 with UnknownHostException

use of java.net.UnknownHostException in project hive by apache.

the class HiveSiteHS2ConnectionFileParser method addDefaultHS2Hosts.

private void addDefaultHS2Hosts(Properties props) throws BeelineHS2ConnectionFileParseException {
    String hiveHost = System.getenv("HIVE_SERVER2_THRIFT_BIND_HOST");
    if (hiveHost == null) {
        hiveHost = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST);
    }
    InetAddress serverIPAddress;
    try {
        serverIPAddress = ServerUtils.getHostAddress(hiveHost);
    } catch (UnknownHostException e) {
        throw new BeelineHS2ConnectionFileParseException(e.getMessage(), e);
    }
    int portNum = getPortNum("http".equalsIgnoreCase(HiveConf.getVar(conf, ConfVars.HIVE_SERVER2_TRANSPORT_MODE)));
    props.setProperty("hosts", serverIPAddress.getHostName() + ":" + portNum);
}
Also used : UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Example 22 with UnknownHostException

use of java.net.UnknownHostException in project tomcat by apache.

the class IntrospectionUtils method setProperty.

// setPropertyMethodVoid is not null when used
@SuppressWarnings("null")
public static boolean setProperty(Object o, String name, String value, boolean invokeSetProperty) {
    if (log.isDebugEnabled())
        log.debug("IntrospectionUtils: setProperty(" + o.getClass() + " " + name + "=" + value + ")");
    String setter = "set" + capitalize(name);
    try {
        Method[] methods = findMethods(o.getClass());
        Method setPropertyMethodVoid = null;
        Method setPropertyMethodBool = null;
        // First, the ideal case - a setFoo( String ) method
        for (int i = 0; i < methods.length; i++) {
            Class<?>[] paramT = methods[i].getParameterTypes();
            if (setter.equals(methods[i].getName()) && paramT.length == 1 && "java.lang.String".equals(paramT[0].getName())) {
                methods[i].invoke(o, new Object[] { value });
                return true;
            }
        }
        // Try a setFoo ( int ) or ( boolean )
        for (int i = 0; i < methods.length; i++) {
            boolean ok = true;
            if (setter.equals(methods[i].getName()) && methods[i].getParameterTypes().length == 1) {
                // match - find the type and invoke it
                Class<?> paramType = methods[i].getParameterTypes()[0];
                Object[] params = new Object[1];
                // Try a setFoo ( int )
                if ("java.lang.Integer".equals(paramType.getName()) || "int".equals(paramType.getName())) {
                    try {
                        params[0] = Integer.valueOf(value);
                    } catch (NumberFormatException ex) {
                        ok = false;
                    }
                // Try a setFoo ( long )
                } else if ("java.lang.Long".equals(paramType.getName()) || "long".equals(paramType.getName())) {
                    try {
                        params[0] = Long.valueOf(value);
                    } catch (NumberFormatException ex) {
                        ok = false;
                    }
                // Try a setFoo ( boolean )
                } else if ("java.lang.Boolean".equals(paramType.getName()) || "boolean".equals(paramType.getName())) {
                    params[0] = Boolean.valueOf(value);
                // Try a setFoo ( InetAddress )
                } else if ("java.net.InetAddress".equals(paramType.getName())) {
                    try {
                        params[0] = InetAddress.getByName(value);
                    } catch (UnknownHostException exc) {
                        if (log.isDebugEnabled())
                            log.debug("IntrospectionUtils: Unable to resolve host name:" + value);
                        ok = false;
                    }
                // Unknown type
                } else {
                    if (log.isDebugEnabled())
                        log.debug("IntrospectionUtils: Unknown type " + paramType.getName());
                }
                if (ok) {
                    methods[i].invoke(o, params);
                    return true;
                }
            }
            // save "setProperty" for later
            if ("setProperty".equals(methods[i].getName())) {
                if (methods[i].getReturnType() == Boolean.TYPE) {
                    setPropertyMethodBool = methods[i];
                } else {
                    setPropertyMethodVoid = methods[i];
                }
            }
        }
        // Ok, no setXXX found, try a setProperty("name", "value")
        if (invokeSetProperty && (setPropertyMethodBool != null || setPropertyMethodVoid != null)) {
            Object[] params = new Object[2];
            params[0] = name;
            params[1] = value;
            if (setPropertyMethodBool != null) {
                try {
                    return ((Boolean) setPropertyMethodBool.invoke(o, params)).booleanValue();
                } catch (IllegalArgumentException biae) {
                    //parameter types. lets try the other
                    if (setPropertyMethodVoid != null) {
                        setPropertyMethodVoid.invoke(o, params);
                        return true;
                    } else {
                        throw biae;
                    }
                }
            } else {
                setPropertyMethodVoid.invoke(o, params);
                return true;
            }
        }
    } catch (IllegalArgumentException ex2) {
        log.warn("IAE " + o + " " + name + " " + value, ex2);
    } catch (SecurityException ex1) {
        log.warn("IntrospectionUtils: SecurityException for " + o.getClass() + " " + name + "=" + value + ")", ex1);
    } catch (IllegalAccessException iae) {
        log.warn("IntrospectionUtils: IllegalAccessException for " + o.getClass() + " " + name + "=" + value + ")", iae);
    } catch (InvocationTargetException ie) {
        ExceptionUtils.handleThrowable(ie.getCause());
        log.warn("IntrospectionUtils: InvocationTargetException for " + o.getClass() + " " + name + "=" + value + ")", ie);
    }
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with UnknownHostException

use of java.net.UnknownHostException in project tomcat by apache.

the class IntrospectionUtils method setProperty.

/*
     * Find a method with the right name If found, call the method ( if param is
     * int or boolean we'll convert value to the right type before) - that means
     * you can have setDebug(1).
     */
@SuppressWarnings("null")
public static boolean setProperty(Object o, String name, String value) {
    if (log.isDebugEnabled())
        log.debug("IntrospectionUtils: setProperty(" + o.getClass() + " " + name + "=" + value + ")");
    String setter = "set" + capitalize(name);
    try {
        Method[] methods = findMethods(o.getClass());
        Method setPropertyMethodVoid = null;
        Method setPropertyMethodBool = null;
        // First, the ideal case - a setFoo( String ) method
        for (int i = 0; i < methods.length; i++) {
            Class<?>[] paramT = methods[i].getParameterTypes();
            if (setter.equals(methods[i].getName()) && paramT.length == 1 && "java.lang.String".equals(paramT[0].getName())) {
                methods[i].invoke(o, new Object[] { value });
                return true;
            }
        }
        // Try a setFoo ( int ) or ( boolean )
        for (int i = 0; i < methods.length; i++) {
            boolean ok = true;
            if (setter.equals(methods[i].getName()) && methods[i].getParameterTypes().length == 1) {
                // match - find the type and invoke it
                Class<?> paramType = methods[i].getParameterTypes()[0];
                Object[] params = new Object[1];
                // Try a setFoo ( int )
                if ("java.lang.Integer".equals(paramType.getName()) || "int".equals(paramType.getName())) {
                    try {
                        params[0] = new Integer(value);
                    } catch (NumberFormatException ex) {
                        ok = false;
                    }
                // Try a setFoo ( long )
                } else if ("java.lang.Long".equals(paramType.getName()) || "long".equals(paramType.getName())) {
                    try {
                        params[0] = new Long(value);
                    } catch (NumberFormatException ex) {
                        ok = false;
                    }
                // Try a setFoo ( boolean )
                } else if ("java.lang.Boolean".equals(paramType.getName()) || "boolean".equals(paramType.getName())) {
                    params[0] = Boolean.valueOf(value);
                // Try a setFoo ( InetAddress )
                } else if ("java.net.InetAddress".equals(paramType.getName())) {
                    try {
                        params[0] = InetAddress.getByName(value);
                    } catch (UnknownHostException exc) {
                        if (log.isDebugEnabled())
                            log.debug("IntrospectionUtils: Unable to resolve host name:" + value);
                        ok = false;
                    }
                // Unknown type
                } else {
                    if (log.isDebugEnabled())
                        log.debug("IntrospectionUtils: Unknown type " + paramType.getName());
                }
                if (ok) {
                    methods[i].invoke(o, params);
                    return true;
                }
            }
            // save "setProperty" for later
            if ("setProperty".equals(methods[i].getName())) {
                if (methods[i].getReturnType() == Boolean.TYPE) {
                    setPropertyMethodBool = methods[i];
                } else {
                    setPropertyMethodVoid = methods[i];
                }
            }
        }
        // Ok, no setXXX found, try a setProperty("name", "value")
        if (setPropertyMethodBool != null || setPropertyMethodVoid != null) {
            Object[] params = new Object[2];
            params[0] = name;
            params[1] = value;
            if (setPropertyMethodBool != null) {
                try {
                    return ((Boolean) setPropertyMethodBool.invoke(o, params)).booleanValue();
                } catch (IllegalArgumentException biae) {
                    //parameter types. lets try the other
                    if (setPropertyMethodVoid != null) {
                        setPropertyMethodVoid.invoke(o, params);
                        return true;
                    } else {
                        throw biae;
                    }
                }
            } else {
                setPropertyMethodVoid.invoke(o, params);
                return true;
            }
        }
    } catch (IllegalArgumentException ex2) {
        log.warn("IAE " + o + " " + name + " " + value, ex2);
    } catch (SecurityException ex1) {
        if (log.isDebugEnabled())
            log.debug("IntrospectionUtils: SecurityException for " + o.getClass() + " " + name + "=" + value + ")", ex1);
    } catch (IllegalAccessException iae) {
        if (log.isDebugEnabled())
            log.debug("IntrospectionUtils: IllegalAccessException for " + o.getClass() + " " + name + "=" + value + ")", iae);
    } catch (InvocationTargetException ie) {
        Throwable cause = ie.getCause();
        if (cause instanceof ThreadDeath) {
            throw (ThreadDeath) cause;
        }
        if (cause instanceof VirtualMachineError) {
            throw (VirtualMachineError) cause;
        }
        if (log.isDebugEnabled())
            log.debug("IntrospectionUtils: InvocationTargetException for " + o.getClass() + " " + name + "=" + value + ")", ie);
    }
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 24 with UnknownHostException

use of java.net.UnknownHostException in project metrics by dropwizard.

the class PickledGraphite method connect.

@Override
public void connect() throws IllegalStateException, IOException {
    if (isConnected()) {
        throw new IllegalStateException("Already connected");
    }
    InetSocketAddress address = this.address;
    if (address == null) {
        address = new InetSocketAddress(hostname, port);
    }
    if (address.getAddress() == null) {
        throw new UnknownHostException(address.getHostName());
    }
    this.socket = socketFactory.createSocket(address.getAddress(), address.getPort());
    this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), charset));
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 25 with UnknownHostException

use of java.net.UnknownHostException in project sharding-jdbc by dangdangdotcom.

the class HostNameIdGenerator method initWorkerId.

static void initWorkerId() {
    InetAddress address;
    Long workerId;
    try {
        address = InetAddress.getLocalHost();
    } catch (final UnknownHostException e) {
        throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!");
    }
    String hostName = address.getHostName();
    try {
        workerId = Long.valueOf(hostName.replace(hostName.replaceAll("\\d+$", ""), ""));
    } catch (final NumberFormatException e) {
        throw new IllegalArgumentException(String.format("Wrong hostname:%s, hostname must be end with number!", hostName));
    }
    CommonSelfIdGenerator.setWorkerId(workerId);
}
Also used : UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Aggregations

UnknownHostException (java.net.UnknownHostException)780 InetAddress (java.net.InetAddress)319 IOException (java.io.IOException)229 InetSocketAddress (java.net.InetSocketAddress)88 Test (org.junit.Test)74 SocketException (java.net.SocketException)61 ArrayList (java.util.ArrayList)59 Socket (java.net.Socket)48 SocketTimeoutException (java.net.SocketTimeoutException)38 File (java.io.File)34 URL (java.net.URL)32 HashMap (java.util.HashMap)31 MalformedURLException (java.net.MalformedURLException)30 ConnectException (java.net.ConnectException)27 NetworkInterface (java.net.NetworkInterface)25 Properties (java.util.Properties)24 Inet4Address (java.net.Inet4Address)22 InputStream (java.io.InputStream)20 HttpURLConnection (java.net.HttpURLConnection)20 URI (java.net.URI)20