001package org.apache.commons.jcs.auxiliary.remote;
002
003import java.util.regex.Matcher;
004import java.util.regex.Pattern;
005
006import org.apache.commons.logging.Log;
007import org.apache.commons.logging.LogFactory;
008
009/*
010 * Licensed to the Apache Software Foundation (ASF) under one
011 * or more contributor license agreements.  See the NOTICE file
012 * distributed with this work for additional information
013 * regarding copyright ownership.  The ASF licenses this file
014 * to you under the Apache License, Version 2.0 (the
015 * "License"); you may not use this file except in compliance
016 * with the License.  You may obtain a copy of the License at
017 *
018 *   http://www.apache.org/licenses/LICENSE-2.0
019 *
020 * Unless required by applicable law or agreed to in writing,
021 * software distributed under the License is distributed on an
022 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
023 * KIND, either express or implied.  See the License for the
024 * specific language governing permissions and limitations
025 * under the License.
026 */
027
028/**
029 * Location of the RMI registry.
030 */
031public final class RemoteLocation
032{
033    /** The logger. */
034    private static final Log log = LogFactory.getLog( RemoteLocation.class );
035
036    /** Pattern for parsing server:port */
037    private static final Pattern SERVER_COLON_PORT = Pattern.compile("(\\S+)\\s*:\\s*(\\d+)");
038
039    /** Host name */
040    private final String host;
041
042    /** Port */
043    private final int port;
044
045    /**
046     * Constructor for the Location object
047     * <p>
048     * @param host
049     * @param port
050     */
051    public RemoteLocation( String host, int port )
052    {
053        this.host = host;
054        this.port = port;
055    }
056
057    /**
058     * @return the host
059     */
060    public String getHost()
061    {
062        return host;
063    }
064
065    /**
066     * @return the port
067     */
068    public int getPort()
069    {
070        return port;
071    }
072
073    /**
074     * @param obj
075     * @return true if the host and port are equal
076     */
077    @Override
078    public boolean equals( Object obj )
079    {
080        if ( obj == this )
081        {
082            return true;
083        }
084        if ( obj == null || !( obj instanceof RemoteLocation ) )
085        {
086            return false;
087        }
088        RemoteLocation l = (RemoteLocation) obj;
089        if ( this.host == null )
090        {
091            return l.host == null && port == l.port;
092        }
093        return host.equals( l.host ) && port == l.port;
094    }
095
096    /**
097     * @return int
098     */
099    @Override
100    public int hashCode()
101    {
102        return host == null ? port : host.hashCode() ^ port;
103    }
104
105    /**
106     * @see java.lang.Object#toString()
107     */
108    @Override
109    public String toString()
110    {
111        StringBuilder sb = new StringBuilder();
112        if (this.host != null)
113        {
114            sb.append(this.host);
115        }
116        sb.append(':').append(this.port);
117
118        return sb.toString();
119    }
120
121    /**
122     * Parse remote server and port from the string representation server:port and store them in
123     * a RemoteLocation object
124     *
125     * @param server the input string
126     * @return the remote location object
127     */
128    public static RemoteLocation parseServerAndPort(final String server)
129    {
130        Matcher match = SERVER_COLON_PORT.matcher(server);
131
132        if (match.find() && match.groupCount() == 2)
133        {
134            RemoteLocation location = new RemoteLocation( match.group(1), Integer.parseInt( match.group(2) ) );
135            return location;
136        }
137        else
138        {
139            log.error("Invalid server descriptor: " + server);
140        }
141
142        return null;
143    }
144}