2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.infrastructure.messaging.util;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectOutputStream;
27 import java.net.InetAddress;
28 import java.net.NetworkInterface;
29 import java.net.Socket;
30 import java.net.UnknownHostException;
31 import java.util.Enumeration;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * The Class MessagingUtils is a class with static methods used in IPC messaging for finding free ports, translating
37 * host names to addresses, serializing objects and flushing object streams.
39 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
41 public final class MessagingUtils {
42 // The port number of the lowest user port, ports 0-1023 are system ports
43 private static final int LOWEST_USER_PORT = 1024;
46 * Port number is an unsigned 16-bit integer, so maximum port is 65535.
48 private static final int MAX_PORT_RANGE = 65535;
50 // Logger for this class
51 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessagingUtils.class);
54 * Private constructor used to prevent sub class instantiation.
56 private MessagingUtils() {
57 // Private constructor to block subclassing
61 * This method searches the availability of the port, if the requested port not available, this method will throw an
64 * @param port the port to check
65 * @return the port verified as being free
66 * @throws RuntimeException on port allocation errors
68 public static int checkPort(final int port) {
69 LOGGER.entry("Checking availability of port {}", port);
71 if (isPortAvailable(port)) {
72 LOGGER.debug("Port {} is available ", port);
75 LOGGER.debug("Port {} is not available", port);
76 throw new IllegalArgumentException("could not allocate requested port: " + port);
80 * This method searches the availability of the port, if the requested port not available,this method will increment
81 * the port number and check the availability of that port, this process will continue until it reaches max port
82 * range which is MAX_PORT_RANGE.
84 * @param port the first port to check
85 * @return the port that was found
86 * @throws RuntimeException on port allocation errors
88 public static int findPort(final int port) {
89 LOGGER.entry("Checking availability of port {}", port);
91 int availablePort = port;
93 while (availablePort <= MAX_PORT_RANGE) {
94 if (isPortAvailable(availablePort)) {
95 LOGGER.debug("Port {} is available ", availablePort);
98 LOGGER.debug("Port {} is not available", availablePort);
101 throw new IllegalArgumentException("could not find free available");
105 * Check if port is available or not.
107 * @param port the port to test
108 * @return true if port is available
110 public static boolean isPortAvailable(final int port) {
111 try (final Socket socket = new Socket("localhost", port)) {
113 } catch (final IOException ignoredException) {
114 LOGGER.trace("Port {} is available", port, ignoredException);
120 * Returns the local host address.
122 * @return the local host address
123 * @throws IllegalStateException if the local host's address cannot be found
125 public static InetAddress getHost() {
127 return InetAddress.getLocalHost();
128 } catch (final UnknownHostException e) {
129 throw new IllegalStateException(e.getMessage(), e);
134 * This method searches the availability of the port, if the requested port not available,this method will increment
135 * the port number and check the availability, this process will continue until it find port available.
137 * @param port the first port to check
138 * @return the port that was found
139 * @throws RuntimeException on port allocation errors
141 public static int allocateAddress(final int port) {
142 if (port < LOWEST_USER_PORT) {
143 throw new IllegalArgumentException("The port " + port + " is already in use");
145 return MessagingUtils.findPort(port);
149 * Get an Internet Address for the local host.
151 * @return an Internet address
152 * @throws UnknownHostException if the address of the local host cannot be found
154 public static InetAddress getLocalHostLanAddress() throws UnknownHostException {
156 InetAddress candidateAddress = null;
157 // Iterate all NICs (network interface cards)...
158 for (final Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
159 .hasMoreElements();) {
160 final NetworkInterface iface = ifaces.nextElement();
161 // Iterate all IP addresses assigned to each card...
162 for (final Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs
163 .hasMoreElements();) {
164 final InetAddress inetAddr = inetAddrs.nextElement();
165 if (!inetAddr.isLoopbackAddress()) {
167 if (inetAddr.isSiteLocalAddress()) {
168 // Found non-loopback site-local address. Return it
171 } else if (candidateAddress == null) {
172 // Found non-loopback address, but not
173 // necessarily site-local.
174 // Store it as a candidate to be returned if
175 // site-local address is not subsequently
177 candidateAddress = inetAddr;
178 // Note that we don't repeatedly assign
179 // non-loopback non-site-local addresses as
181 // only the first. For subsequent iterations,
182 // candidate will be non-null.
187 if (candidateAddress != null) {
188 // We did not find a site-local address, but we found some other
189 // non-loopback address.
190 // Server might have a non-site-local address assigned to its
191 // NIC (or it might be running
192 // IPv6 which deprecates the "site-local" concept).
193 // Return this non-loopback candidate address...
194 return candidateAddress;
196 // At this point, we did not find a non-loopback address.
197 // Fall back to returning whatever InetAddress.getLocalHost()
199 final InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
200 if (jdkSuppliedAddress == null) {
201 throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
203 return jdkSuppliedAddress;
204 } catch (final Exception e) {
205 final UnknownHostException unknownHostException =
206 new UnknownHostException("Failed to determine LAN address: " + e);
207 unknownHostException.initCause(e);
208 throw unknownHostException;
213 * This method serializes the message holder objects.
215 * @param object the object
218 public static byte[] serializeObject(final Object object) {
219 LOGGER.entry(object.getClass().getName());
220 final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
221 ObjectOutputStream oos = null;
223 oos = new ObjectOutputStream(bytesOut);
224 oos.writeObject(object);
225 } catch (final IOException e) {
226 LOGGER.warn("error on object serialization", e);
228 flushAndClose(oos, bytesOut);
230 return bytesOut.toByteArray();
234 * Flush and close an object stream and a byte array output stream.
236 * @param oos the object output stream
237 * @param bytesOut the byte array output stream
239 private static void flushAndClose(final ObjectOutputStream oos, final ByteArrayOutputStream bytesOut) {
245 if (bytesOut != null) {
249 } catch (final IOException e) {
250 LOGGER.error("Failed to close the Srialization operation");