1 5 package org.h2.util; 6 7 import java.io.IOException ; 8 import java.net.BindException ; 9 import java.net.InetAddress ; 10 import java.net.ServerSocket ; 11 import java.net.Socket ; 12 import java.sql.SQLException ; 13 14 import org.h2.message.Message; 15 import org.h2.security.SecureSocketFactory; 16 17 public class NetUtils { 18 19 public static Socket createLoopbackSocket(int port, boolean ssl) throws IOException , SQLException { 20 InetAddress address = InetAddress.getByName("127.0.0.1"); 21 return createSocket(address, port, ssl); 22 } 23 24 public static Socket createSocket(InetAddress address, int port, boolean ssl) throws IOException , SQLException { 25 if (ssl) { 26 SecureSocketFactory f = SecureSocketFactory.getInstance(); 27 return f.createSocket(address, port); 28 } else { 29 return new Socket (address, port); 30 } 31 } 32 33 public static ServerSocket createServerSocket(int port, boolean ssl) throws SQLException { 34 try { 35 return createServerSocketTry(port, ssl); 36 } catch(SQLException e) { 37 return createServerSocketTry(port, ssl); 39 } 40 } 41 42 private static ServerSocket createServerSocketTry(int port, boolean ssl) throws SQLException { 43 try { 46 if(ssl) { 47 SecureSocketFactory f = SecureSocketFactory.getInstance(); 48 return f.createServerSocket(port); 49 } else { 50 return new ServerSocket (port); 51 } 52 } catch(BindException be) { 53 throw Message.getSQLException(Message.EXCEPTION_OPENING_PORT_1, new String []{""+port}, be); 54 } catch(IOException e) { 55 throw Message.convert(e); 56 } 57 } 58 59 public static boolean isLoopbackAddress(Socket socket) { 60 boolean result = true; 61 result = socket.getInetAddress().isLoopbackAddress(); 63 return result; 65 } 66 67 } 68 | Popular Tags |