PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/network/src/main/java/org/jboss/as/network/SocketBinding.java

#
Java | 285 lines | 144 code | 37 blank | 104 comment | 11 complexity | 739bd1b56ad200fdd361a622d1e00f4b MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2010, Red Hat Inc., and individual contributors as indicated
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.network;
  23. import java.io.IOException;
  24. import java.net.DatagramSocket;
  25. import java.net.InetAddress;
  26. import java.net.InetSocketAddress;
  27. import java.net.MulticastSocket;
  28. import java.net.ServerSocket;
  29. import java.net.SocketException;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import org.jboss.msc.service.ServiceName;
  33. import static org.jboss.as.network.NetworkMessages.MESSAGES;
  34. /**
  35. * An encapsulation of socket binding related information.
  36. *
  37. * @author Emanuel Muckenhuber
  38. */
  39. public final class SocketBinding {
  40. public static final ServiceName JBOSS_BINDING_NAME = ServiceName.JBOSS.append("binding");
  41. private final String name;
  42. private volatile int port;
  43. private volatile boolean isFixedPort;
  44. private volatile InetAddress multicastAddress;
  45. private volatile int multicastPort;
  46. private volatile List<ClientMapping> clientMappings;
  47. private final NetworkInterfaceBinding networkInterface;
  48. private final SocketBindingManager socketBindings;
  49. public SocketBinding(final String name, int port, boolean isFixedPort, InetAddress multicastAddress, int multicastPort,
  50. final NetworkInterfaceBinding networkInterface, SocketBindingManager socketBindings, List<ClientMapping> clientMappings) {
  51. this.name = name;
  52. this.port = port;
  53. this.isFixedPort = isFixedPort;
  54. this.multicastAddress = multicastAddress;
  55. this.multicastPort = multicastPort;
  56. this.socketBindings = socketBindings;
  57. this.networkInterface = networkInterface;
  58. this.clientMappings = clientMappings == null ? Collections.<ClientMapping>emptyList() : fixupMappings(clientMappings);
  59. }
  60. private List<ClientMapping> fixupMappings(List<ClientMapping> clientMappings) {
  61. for (ClientMapping mapping : clientMappings) {
  62. mapping.updatePortIfUnknown(calculatePort());
  63. }
  64. return clientMappings;
  65. }
  66. /**
  67. * Return the name of the SocketBinding used in the configuration
  68. *
  69. * @return the SocketBinding configuration name
  70. */
  71. public String getName() {
  72. return name;
  73. }
  74. /**
  75. * Return the resolved {@link InetAddress} for this binding.
  76. *
  77. * @return the resolve address
  78. */
  79. public InetAddress getAddress() {
  80. return networkInterface != null ? networkInterface.getAddress() : socketBindings.getDefaultInterfaceAddress();
  81. }
  82. /**
  83. * Return the {@link NetworkInterfaceBinding} for the default interface.
  84. *
  85. * @return the network interface binding
  86. */
  87. public NetworkInterfaceBinding getNetworkInterfaceBinding() {
  88. return networkInterface != null ? networkInterface : socketBindings.getDefaultInterfaceBinding();
  89. }
  90. /**
  91. * Get the socket binding manager.
  92. *
  93. * @return the socket binding manger
  94. */
  95. public SocketBindingManager getSocketBindings() {
  96. return socketBindings;
  97. }
  98. private int calculatePort() {
  99. int port = this.port;
  100. if (port > 0 && isFixedPort == false) {
  101. port += socketBindings.getPortOffset();
  102. }
  103. return port;
  104. }
  105. /**
  106. * Get the socket address.
  107. *
  108. * @return the socket address
  109. */
  110. public InetSocketAddress getSocketAddress() {
  111. int port = calculatePort();
  112. return new InetSocketAddress(getAddress(), port);
  113. }
  114. /**
  115. * Get the multicast socket address.
  116. *
  117. * @return the multicast address
  118. */
  119. public InetSocketAddress getMulticastSocketAddress() {
  120. if (multicastAddress == null) {
  121. throw MESSAGES.noMulticastBinding(name);
  122. }
  123. return new InetSocketAddress(multicastAddress, multicastPort);
  124. }
  125. /**
  126. * Create and bind a server socket
  127. *
  128. * @return the server socket
  129. * @throws IOException
  130. */
  131. public ServerSocket createServerSocket() throws IOException {
  132. final ServerSocket socket = getServerSocketFactory().createServerSocket(name);
  133. socket.bind(getSocketAddress());
  134. return socket;
  135. }
  136. /**
  137. * Create and bind a server socket.
  138. *
  139. * @param backlog the backlog
  140. * @return the server socket
  141. * @throws IOException
  142. */
  143. public ServerSocket createServerSocket(int backlog) throws IOException {
  144. final ServerSocket socket = getServerSocketFactory().createServerSocket(name);
  145. socket.bind(getSocketAddress(), backlog);
  146. return socket;
  147. }
  148. /**
  149. * Create and bind a datagram socket.
  150. *
  151. * @return the datagram socket
  152. * @throws SocketException
  153. */
  154. public DatagramSocket createDatagramSocket() throws SocketException {
  155. return socketBindings.createDatagramSocket(name, getMulticastSocketAddress());
  156. }
  157. /**
  158. * Create a multicast socket.
  159. *
  160. * @return the multicast socket
  161. * @throws IOException
  162. */
  163. // TODO JBAS-8470 automatically joingGroup
  164. public MulticastSocket createMulticastSocket() throws IOException {
  165. return socketBindings.createMulticastSocket(name, getSocketAddress());
  166. }
  167. /**
  168. * Get the {@code ManagedBinding} associated with this {@code SocketBinding}.
  169. *
  170. * @return the managed binding if bound, <code>null</code> otherwise
  171. */
  172. public ManagedBinding getManagedBinding() {
  173. final SocketBindingManager.NamedManagedBindingRegistry registry = this.socketBindings.getNamedRegistry();
  174. return registry.getManagedBinding(name);
  175. }
  176. /**
  177. * Check whether this {@code SocketBinding} is bound. All bound sockets
  178. * have to be registered at the {@code SocketBindingManager} against which
  179. * this check is performed.
  180. *
  181. * @return true if bound, false otherwise
  182. */
  183. public boolean isBound() {
  184. final SocketBindingManager.NamedManagedBindingRegistry registry = this.socketBindings.getNamedRegistry();
  185. return registry.isRegistered(name);
  186. }
  187. public int getPort() {
  188. return port;
  189. }
  190. //TODO restrict access
  191. public void setPort(int port) {
  192. checkNotBound();
  193. this.port = port;
  194. }
  195. public boolean isFixedPort() {
  196. return isFixedPort;
  197. }
  198. //TODO restrict access
  199. public void setFixedPort(boolean fixedPort) {
  200. checkNotBound();
  201. isFixedPort = fixedPort;
  202. }
  203. public int getMulticastPort() {
  204. return multicastPort;
  205. }
  206. //TODO restrict access
  207. public void setMulticastPort(int multicastPort) {
  208. checkNotBound();
  209. this.multicastPort = multicastPort;
  210. }
  211. public InetAddress getMulticastAddress() {
  212. return multicastAddress;
  213. }
  214. //TODO restrict access
  215. public void setMulticastAddress(InetAddress multicastAddress) {
  216. checkNotBound();
  217. this.multicastAddress = multicastAddress;
  218. }
  219. public void setClientMappings(List<ClientMapping> clientMappings) {
  220. this.clientMappings = clientMappings;
  221. }
  222. public List<ClientMapping> getClientMappings() {
  223. return clientMappings;
  224. }
  225. /**
  226. * Unlike the {@link #getPort()} method, this method takes into account the port offset, if the port
  227. * is <i>not</i> a fixed port and returns the absolute port number which is the sum of the port offset
  228. * and the (relative) port
  229. * @return
  230. */
  231. public int getAbsolutePort() {
  232. if (this.isFixedPort) {
  233. return port;
  234. }
  235. return this.port + this.socketBindings.getPortOffset();
  236. }
  237. void checkNotBound() {
  238. if(isBound()) {
  239. throw MESSAGES.cannotChangeWhileBound();
  240. }
  241. }
  242. ManagedSocketFactory getSocketFactory() {
  243. return socketBindings.getSocketFactory();
  244. }
  245. ManagedServerSocketFactory getServerSocketFactory() {
  246. return socketBindings.getServerSocketFactory();
  247. }
  248. }