| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| |
| package org.apache.rocketmq.ons.open.trace.core.utils; |
| |
| import org.apache.rocketmq.remoting.protocol.RemotingSerializable; |
| import java.net.Inet6Address; |
| import java.net.InetAddress; |
| import java.net.NetworkInterface; |
| import java.net.SocketException; |
| import java.net.UnknownHostException; |
| import java.util.ArrayList; |
| import java.util.Enumeration; |
| |
| public class MixUtils { |
| public static String getLocalAddress() { |
| try { |
| |
| Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); |
| ArrayList<String> ipv4Result = new ArrayList<String>(); |
| ArrayList<String> ipv6Result = new ArrayList<String>(); |
| while (enumeration.hasMoreElements()) { |
| final NetworkInterface networkInterface = enumeration.nextElement(); |
| final Enumeration<InetAddress> en = networkInterface.getInetAddresses(); |
| while (en.hasMoreElements()) { |
| final InetAddress address = en.nextElement(); |
| if (!address.isLoopbackAddress()) { |
| if (address instanceof Inet6Address) { |
| ipv6Result.add(normalizeHostAddress(address)); |
| } |
| else { |
| ipv4Result.add(normalizeHostAddress(address)); |
| } |
| } |
| } |
| } |
| |
| |
| if (!ipv4Result.isEmpty()) { |
| for (String ip : ipv4Result) { |
| if (ip.startsWith("127.0") || ip.startsWith("192.168")) { |
| continue; |
| } |
| |
| return ip; |
| } |
| |
| |
| return ipv4Result.get(ipv4Result.size() - 1); |
| } |
| |
| else if (!ipv6Result.isEmpty()) { |
| return ipv6Result.get(0); |
| } |
| |
| final InetAddress localHost = InetAddress.getLocalHost(); |
| return normalizeHostAddress(localHost); |
| } |
| catch (SocketException e) { |
| e.printStackTrace(); |
| } |
| catch (UnknownHostException e) { |
| e.printStackTrace(); |
| } |
| finally { |
| |
| } |
| |
| return null; |
| } |
| |
| |
| public static String normalizeHostAddress(final InetAddress localHost) { |
| if (localHost instanceof Inet6Address) { |
| return "[" + localHost.getHostAddress() + "]"; |
| } |
| else { |
| return localHost.getHostAddress(); |
| } |
| } |
| |
| |
| public static String toJson(final Object obj, boolean prettyFormat) { |
| return RemotingSerializable.toJson(obj, prettyFormat); |
| } |
| |
| |
| public static <T> T fromJson(String json, Class<T> classOfT) { |
| return RemotingSerializable.fromJson(json, classOfT); |
| } |
| |
| |
| public static String replaceNull(String ori) { |
| return ori == null ? "" : ori; |
| } |
| } |