001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }