001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.collab.util;
043:
044: import java.util.*;
045: import java.net.*;
046:
047: /**
048: * Utility fo parsing host:port constructs.
049: *
050: */
051: public class HostPort {
052:
053: private int _port;
054: private String _hostName = null;
055:
056: public HostPort(String s, int defaultPort) {
057: int n = 0;
058: _port = defaultPort;
059: if (s != null) {
060: s = s.trim();
061: n = s.indexOf(':');
062: }
063: if (n > 0) {
064: String sPort = s.substring(n + 1);
065: try {
066: _port = Integer.parseInt(sPort.trim());
067: } catch (NumberFormatException nfEx) {
068: }
069: _hostName = s.substring(0, n).trim();
070: } else if (s != null && s.length() > 0
071: && Character.isDigit(s.charAt(0)) && s.indexOf(".") < 0) {
072: // port number only
073: try {
074: _port = Integer.parseInt(s);
075: } catch (NumberFormatException nfEx) {
076: }
077: } else {
078: // host name, IP address, or null
079: if ("".equals(s))
080: _hostName = null;
081: else
082: _hostName = s;
083: }
084: }
085:
086: public String getHostName() {
087: return (_hostName != null) ? _hostName : "localhost";
088: }
089:
090: public int getPort() {
091: return _port;
092: }
093:
094: /**
095: * This method can be used only when the default port specified was 0
096: * @param port The new port number
097: * @throws IllegalStateException
098: */
099: public void setPort(int port) {
100: if (_port != 0)
101: throw new IllegalStateException();
102: _port = port;
103: }
104:
105: public InetAddress getHost() throws UnknownHostException {
106: return (_hostName != null) ? InetAddress.getByName(_hostName)
107: : null;
108: }
109:
110: public String toString() {
111: return getHostName() + ":" + _port;
112: }
113:
114: public boolean equals(Object o) {
115: if (o instanceof HostPort) {
116: HostPort hp = (HostPort) o;
117: try {
118: return ((_port == hp.getPort()) && ((getHost() == null && hp
119: .getHost() == null) || (getHost() != null && getHost()
120: .equals(hp.getHost()))));
121: } catch (Exception e) {
122: return false;
123: }
124: } else {
125: return this .equals(new HostPort(o.toString(), 0));
126: }
127: }
128:
129: }
|