Java tutorial
/** * Copyright (C) 2012 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package nl.minbzk.dwr.zoeken.enricher.util; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.rmi.server.RMISocketFactory; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * An RMI socket factory which only binds to a specific host (can be 0.0.0.0). * * @author Jasper van Veghel <jasper@seajas.com> */ public class RestrictedHostSocketFactory extends RMISocketFactory { /** * The logger. */ private static final Logger logger = LoggerFactory.getLogger(RestrictedHostSocketFactory.class); /** * The default backlog. */ private static final Integer DEFAULT_BACKLOG = 8192; /** * The specific hostname. */ private final String hostname; /** * Total socket count. */ private AtomicInteger totalSocketsCreated = new AtomicInteger(0); /** * Default constructor. * * @param hostname */ public RestrictedHostSocketFactory(final String hostname) { super(); java.util.logging.Logger tcpLogger = java.util.logging.Logger.getLogger("sun.rmi.transport.tcp"); tcpLogger.setLevel(Level.SEVERE); this.hostname = hostname; if (!StringUtils.isEmpty(hostname) && !hostname.equals("0.0.0.0")) System.setProperty("java.rmi.server.hostname", hostname); } /** * Create a server socket which only binds to the specified hostname. * * @param port * @return ServerSocket * @throws IOException */ @Override public ServerSocket createServerSocket(final int port) throws IOException { InetAddress inetAddress = null; if (logger.isInfoEnabled()) logger.info( "Creating (server-side) socket #" + totalSocketsCreated.incrementAndGet() + " on port " + port); if (!StringUtils.isEmpty(hostname) && !hostname.equals("0.0.0.0")) inetAddress = InetAddress.getByName(hostname); ServerSocket result = new ServerSocket(port, DEFAULT_BACKLOG, inetAddress); result.setReuseAddress(true); return result; } /** * Create a new client socket. * * @param host * @param port * @return Socket * @throws IOException */ @Override public Socket createSocket(final String host, final int port) throws IOException { if (logger.isInfoEnabled()) logger.info("Creating (client-side) socket #" + totalSocketsCreated.incrementAndGet() + " to host '" + host + "' and port " + port); return new Socket(host, port); } /** * Implement this so that JMX / RMI does not open too many ports. * * @return int */ @Override public boolean equals(final Object o) { return o instanceof RestrictedHostSocketFactory; } /** * Implement this so that JMX / RMI does not open too many ports. * * @return int */ @Override public int hashCode() { return 1; } }