001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.IOException;
018    
019    import java.net.InetAddress;
020    import java.net.InetSocketAddress;
021    import java.net.NetworkInterface;
022    import java.net.ServerSocket;
023    import java.net.Socket;
024    import java.net.SocketException;
025    
026    import java.nio.channels.ServerSocketChannel;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class SocketUtil {
032    
033            public static ServerSocketChannel createServerSocketChannel(
034                            InetAddress bindingInetAddress, int startPort,
035                            ServerSocketConfigurator serverSocketConfigurator)
036                    throws IOException {
037    
038                    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
039    
040                    int port = startPort;
041    
042                    while (true) {
043                            try {
044                                    ServerSocket serverSocket = serverSocketChannel.socket();
045    
046                                    if (serverSocketConfigurator != null) {
047                                            serverSocketConfigurator.configure(serverSocket);
048                                    }
049    
050                                    serverSocket.bind(
051                                            new InetSocketAddress(bindingInetAddress, port));
052    
053                                    return serverSocketChannel;
054                            }
055                            catch (IOException ioe) {
056                                    port++;
057                            }
058                    }
059            }
060    
061            public static BindInfo getBindInfo(String host, int port)
062                    throws IOException {
063    
064                    Socket socket = null;
065    
066                    try {
067                            socket = new Socket(host, port);
068    
069                            InetAddress inetAddress = socket.getLocalAddress();
070                            NetworkInterface networkInterface =
071                                    NetworkInterface.getByInetAddress(inetAddress);
072    
073                            BindInfo bindInfo = new BindInfo();
074    
075                            bindInfo.setInetAddress(inetAddress);
076                            bindInfo.setNetworkInterface(networkInterface);
077    
078                            return bindInfo;
079                    }
080                    finally {
081                            if (socket != null) {
082                                    try {
083                                            socket.close();
084                                    }
085                                    catch (IOException ioe) {
086                                    }
087                            }
088                    }
089            }
090    
091            public static class BindInfo {
092    
093                    public InetAddress getInetAddress() {
094                            return _inetAddress;
095                    }
096    
097                    public NetworkInterface getNetworkInterface() {
098                            return _networkInterface;
099                    }
100    
101                    public void setInetAddress(InetAddress inetAddress) {
102                            _inetAddress = inetAddress;
103                    }
104    
105                    public void setNetworkInterface(NetworkInterface networkInterface) {
106                            _networkInterface = networkInterface;
107                    }
108    
109                    private InetAddress _inetAddress;
110                    private NetworkInterface _networkInterface;
111    
112            }
113    
114            public static interface ServerSocketConfigurator {
115    
116                    public void configure(ServerSocket serverSocket) throws SocketException;
117    
118            }
119    
120    }