X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.management%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fmanagement%2Fdiscovery%2FInetAddressUtils.java;fp=bundles%2Forg.simantics.db.management%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fmanagement%2Fdiscovery%2FInetAddressUtils.java;h=0e7411001a19ab753add43a2d0be7ccc80d639ee;hp=fdf116b8e71bf7c14d687ed12af56565c9a98173;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.db.management/src/org/simantics/db/management/discovery/InetAddressUtils.java b/bundles/org.simantics.db.management/src/org/simantics/db/management/discovery/InetAddressUtils.java index fdf116b8e..0e7411001 100644 --- a/bundles/org.simantics.db.management/src/org/simantics/db/management/discovery/InetAddressUtils.java +++ b/bundles/org.simantics.db.management/src/org/simantics/db/management/discovery/InetAddressUtils.java @@ -1,73 +1,73 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.db.management.discovery; - -import java.net.InetSocketAddress; - -/** - * @author Tuukka Lehtonen - */ -public final class InetAddressUtils { - - /** - * @param hostAndPort - * @return - * @throws IllegalArgumentException - */ - public static InetSocketAddress parse(String hostAndPort) throws IllegalArgumentException { - String[] split = hostAndPort.split(":"); - if (split.length != 2) - throw new IllegalArgumentException("address does not contain a port, missing ':' character"); - return new InetSocketAddress(split[0], Integer.parseInt(split[1])); - } - - /** - * @param hostAndPort - * @return - * @throws IllegalArgumentException - */ - public static InetSocketAddress parseUnresolved(String hostAndPort) throws IllegalArgumentException { - String[] split = hostAndPort.split(":"); - if (split.length != 2) - throw new IllegalArgumentException("address does not contain a port, missing ':' character"); - return InetSocketAddress.createUnresolved(split[0], Integer.parseInt(split[1])); - } - - public static String toHostString(InetSocketAddress address) { - if (address == null) return "null"; - return address.getHostName() + ":" + address.getPort(); - } - - /** - * @param hostAndPort - * @return - * @throws IllegalArgumentException - */ - public static InetSocketAddress parseHostAddressPort(String hostAddressPort) throws IllegalArgumentException { - String[] split = hostAddressPort.split(":"); - if (split.length != 2) - throw new IllegalArgumentException("address not in format '[/]
:port', does not contain a port, missing ':' character: " + hostAddressPort); - String[] split2 = split[0].split("/"); - if (split2.length == 1) - return new InetSocketAddress(split[0], Integer.parseInt(split[1])); - if (split2.length == 2) - return new InetSocketAddress(split2[1], Integer.parseInt(split[1])); - throw new IllegalArgumentException("expected address in format '[/]
:port', got " + hostAddressPort); - } - - public static void main(String[] args) { - System.out.println(parseHostAddressPort("/127.0.0.1:1234")); - System.out.println(parseHostAddressPort("localhost/127.0.0.1:1234")); - System.out.println(parseHostAddressPort("127.0.0.1:1234")); - System.out.println(parseHostAddressPort("localhost:1234")); - } -} +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.management.discovery; + +import java.net.InetSocketAddress; + +/** + * @author Tuukka Lehtonen + */ +public final class InetAddressUtils { + + /** + * @param hostAndPort + * @return + * @throws IllegalArgumentException + */ + public static InetSocketAddress parse(String hostAndPort) throws IllegalArgumentException { + String[] split = hostAndPort.split(":"); + if (split.length != 2) + throw new IllegalArgumentException("address does not contain a port, missing ':' character"); + return new InetSocketAddress(split[0], Integer.parseInt(split[1])); + } + + /** + * @param hostAndPort + * @return + * @throws IllegalArgumentException + */ + public static InetSocketAddress parseUnresolved(String hostAndPort) throws IllegalArgumentException { + String[] split = hostAndPort.split(":"); + if (split.length != 2) + throw new IllegalArgumentException("address does not contain a port, missing ':' character"); + return InetSocketAddress.createUnresolved(split[0], Integer.parseInt(split[1])); + } + + public static String toHostString(InetSocketAddress address) { + if (address == null) return "null"; + return address.getHostName() + ":" + address.getPort(); + } + + /** + * @param hostAndPort + * @return + * @throws IllegalArgumentException + */ + public static InetSocketAddress parseHostAddressPort(String hostAddressPort) throws IllegalArgumentException { + String[] split = hostAddressPort.split(":"); + if (split.length != 2) + throw new IllegalArgumentException("address not in format '[/]
:port', does not contain a port, missing ':' character: " + hostAddressPort); + String[] split2 = split[0].split("/"); + if (split2.length == 1) + return new InetSocketAddress(split[0], Integer.parseInt(split[1])); + if (split2.length == 2) + return new InetSocketAddress(split2[1], Integer.parseInt(split[1])); + throw new IllegalArgumentException("expected address in format '[/]
:port', got " + hostAddressPort); + } + + public static void main(String[] args) { + System.out.println(parseHostAddressPort("/127.0.0.1:1234")); + System.out.println(parseHostAddressPort("localhost/127.0.0.1:1234")); + System.out.println(parseHostAddressPort("127.0.0.1:1234")); + System.out.println(parseHostAddressPort("localhost:1234")); + } +}