/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*/
22package org.jboss.as.network;
23
24import java.io.IOException;
25import java.net.DatagramSocket;
26import java.net.InetAddress;
27import java.net.InetSocketAddress;
28import java.net.MulticastSocket;
29import java.net.ServerSocket;
30import java.net.SocketException;
31import java.util.Collections;
32import java.util.List;
33
34import org.jboss.msc.service.ServiceName;
35
36import static org.jboss.as.network.NetworkMessages.MESSAGES;
37
38/**
39 * An encapsulation of socket binding related information.
40 *
41 * @author Emanuel Muckenhuber
42 */
43public final class SocketBinding {
44
45 public static final ServiceName JBOSS_BINDING_NAME = ServiceName.JBOSS.append("binding");
46
47 private final String name;
48 private volatile int port;
49 private volatile boolean isFixedPort;
50 private volatile InetAddress multicastAddress;
51 private volatile int multicastPort;
52 private volatile List<ClientMapping> clientMappings;
53 private final NetworkInterfaceBinding networkInterface;
54 private final SocketBindingManager socketBindings;
55
56 public SocketBinding(final String name, int port, boolean isFixedPort, InetAddress multicastAddress, int multicastPort,
57 final NetworkInterfaceBinding networkInterface, SocketBindingManager socketBindings, List<ClientMapping> clientMappings) {
58 this.name = name;
59 this.port = port;
60 this.isFixedPort = isFixedPort;
61 this.multicastAddress = multicastAddress;
62 this.multicastPort = multicastPort;
63 this.socketBindings = socketBindings;
64 this.networkInterface = networkInterface;
65 this.clientMappings = clientMappings == null ? Collections.<ClientMapping>emptyList() : fixupMappings(clientMappings);
66 }
67
68 private List<ClientMapping> fixupMappings(List<ClientMapping> clientMappings) {
69 for (ClientMapping mapping : clientMappings) {
70 mapping.updatePortIfUnknown(calculatePort());
71 }
72
73 return clientMappings;
74 }
75
76 /**
77 * Return the name of the SocketBinding used in the configuration
78 *
79 * @return the SocketBinding configuration name
80 */
81 public String getName() {
82 return name;
83 }
84
85 /**
86 * Return the resolved {@link InetAddress} for this binding.
87 *
88 * @return the resolve address
89 */
90 public InetAddress getAddress() {
91 return networkInterface != null ? networkInterface.getAddress() : socketBindings.getDefaultInterfaceAddress();
92 }
93
94 /**
95 * Return the {@link NetworkInterfaceBinding} for the default interface.
96 *
97 * @return the network interface binding
98 */
99 public NetworkInterfaceBinding getNetworkInterfaceBinding() {
100 return networkInterface != null ? networkInterface : socketBindings.getDefaultInterfaceBinding();
101 }
102
103 /**
104 * Get the socket binding manager.
105 *
106 * @return the socket binding manger
107 */
108 public SocketBindingManager getSocketBindings() {
109 return socketBindings;
110 }
111
112 private int calculatePort() {
113 int port = this.port;
114 if (port > 0 && isFixedPort == false) {
115 port += socketBindings.getPortOffset();
116 }
117 return port;
118 }
119
120 /**
121 * Get the socket address.
122 *
123 * @return the socket address
124 */
125 public InetSocketAddress getSocketAddress() {
126 int port = calculatePort();
127 return new InetSocketAddress(getAddress(), port);
128 }
129
130 /**
131 * Get the multicast socket address.
132 *
133 * @return the multicast address
134 */
135 public InetSocketAddress getMulticastSocketAddress() {
136 if (multicastAddress == null) {
137 throw MESSAGES.noMulticastBinding(name);
138 }
139 return new InetSocketAddress(multicastAddress, multicastPort);
140 }
141
142 /**
143 * Create and bind a server socket
144 *
145 * @return the server socket
146 * @throws IOException
147 */
148 public ServerSocket createServerSocket() throws IOException {
149 final ServerSocket socket = getServerSocketFactory().createServerSocket(name);
150 socket.bind(getSocketAddress());
151 return socket;
152 }
153
154 /**
155 * Create and bind a server socket.
156 *
157 * @param backlog the backlog
158 * @return the server socket
159 * @throws IOException
160 */
161 public ServerSocket createServerSocket(int backlog) throws IOException {
162 final ServerSocket socket = getServerSocketFactory().createServerSocket(name);
163 socket.bind(getSocketAddress(), backlog);
164 return socket;
165 }
166
167 /**
168 * Create and bind a datagram socket.
169 *
170 * @return the datagram socket
171 * @throws SocketException
172 */
173 public DatagramSocket createDatagramSocket() throws SocketException {
174 return socketBindings.createDatagramSocket(name, getMulticastSocketAddress());
175 }
176
177 /**
178 * Create a multicast socket.
179 *
180 * @return the multicast socket
181 * @throws IOException
182 */
183 // TODO JBAS-8470 automatically joingGroup
184 public MulticastSocket createMulticastSocket() throws IOException {
185 return socketBindings.createMulticastSocket(name, getSocketAddress());
186 }
187
188 /**
189 * Get the {@code ManagedBinding} associated with this {@code SocketBinding}.
190 *
191 * @return the managed binding if bound, <code>null</code> otherwise
192 */
193 public ManagedBinding getManagedBinding() {
194 final SocketBindingManager.NamedManagedBindingRegistry registry = this.socketBindings.getNamedRegistry();
195 return registry.getManagedBinding(name);
196 }
197
198 /**
199 * Check whether this {@code SocketBinding} is bound. All bound sockets
200 * have to be registered at the {@code SocketBindingManager} against which
201 * this check is performed.
202 *
203 * @return true if bound, false otherwise
204 */
205 public boolean isBound() {
206 final SocketBindingManager.NamedManagedBindingRegistry registry = this.socketBindings.getNamedRegistry();
207 return registry.isRegistered(name);
208 }
209
210 public int getPort() {
211 return port;
212 }
213
214 //TODO restrict access
215 public void setPort(int port) {
216 checkNotBound();
217 this.port = port;
218 }
219
220 public boolean isFixedPort() {
221 return isFixedPort;
222 }
223
224 //TODO restrict access
225 public void setFixedPort(boolean fixedPort) {
226 checkNotBound();
227 isFixedPort = fixedPort;
228 }
229
230 public int getMulticastPort() {
231 return multicastPort;
232 }
233
234 //TODO restrict access
235 public void setMulticastPort(int multicastPort) {
236 checkNotBound();
237 this.multicastPort = multicastPort;
238 }
239
240 public InetAddress getMulticastAddress() {
241 return multicastAddress;
242 }
243
244 //TODO restrict access
245 public void setMulticastAddress(InetAddress multicastAddress) {
246 checkNotBound();
247 this.multicastAddress = multicastAddress;
248 }
249
250 public void setClientMappings(List<ClientMapping> clientMappings) {
251 this.clientMappings = clientMappings;
252 }
253
254 public List<ClientMapping> getClientMappings() {
255 return clientMappings;
256 }
257
258 /**
259 * Unlike the {@link #getPort()} method, this method takes into account the port offset, if the port
260 * is <i>not</i> a fixed port and returns the absolute port number which is the sum of the port offset
261 * and the (relative) port
262 * @return
263 */
264 public int getAbsolutePort() {
265 if (this.isFixedPort) {
266 return port;
267 }
268 return this.port + this.socketBindings.getPortOffset();
269 }
270
271 void checkNotBound() {
272 if(isBound()) {
273 throw MESSAGES.cannotChangeWhileBound();
274 }
275 }
276
277 ManagedSocketFactory getSocketFactory() {
278 return socketBindings.getSocketFactory();
279 }
280
281 ManagedServerSocketFactory getServerSocketFactory() {
282 return socketBindings.getServerSocketFactory();
283 }
284
285}