View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote;
2   
3   import java.util.regex.Matcher;
4   import java.util.regex.Pattern;
5   
6   import org.apache.commons.jcs3.log.Log;
7   import org.apache.commons.jcs3.log.LogManager;
8   
9   /*
10   * Licensed to the Apache Software Foundation (ASF) under one
11   * or more contributor license agreements.  See the NOTICE file
12   * distributed with this work for additional information
13   * regarding copyright ownership.  The ASF licenses this file
14   * to you under the Apache License, Version 2.0 (the
15   * "License"); you may not use this file except in compliance
16   * with the License.  You may obtain a copy of the License at
17   *
18   *   http://www.apache.org/licenses/LICENSE-2.0
19   *
20   * Unless required by applicable law or agreed to in writing,
21   * software distributed under the License is distributed on an
22   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23   * KIND, either express or implied.  See the License for the
24   * specific language governing permissions and limitations
25   * under the License.
26   */
27  
28  /**
29   * Location of the RMI registry.
30   */
31  public final class RemoteLocation
32  {
33      /** The logger. */
34      private static final Log log = LogManager.getLog( RemoteLocation.class );
35  
36      /** Pattern for parsing server:port */
37      private static final Pattern SERVER_COLON_PORT = Pattern.compile("(\\S+)\\s*:\\s*(\\d+)");
38  
39      /** Host name */
40      private final String host;
41  
42      /** Port */
43      private final int port;
44  
45      /**
46       * Constructor for the Location object
47       * <p>
48       * @param host
49       * @param port
50       */
51      public RemoteLocation( final String host, final int port )
52      {
53          this.host = host;
54          this.port = port;
55      }
56  
57      /**
58       * @return the host
59       */
60      public String getHost()
61      {
62          return host;
63      }
64  
65      /**
66       * @return the port
67       */
68      public int getPort()
69      {
70          return port;
71      }
72  
73      /**
74       * @param obj
75       * @return true if the host and port are equal
76       */
77      @Override
78      public boolean equals( final Object obj )
79      {
80          if ( obj == this )
81          {
82              return true;
83          }
84          if (!(obj instanceof RemoteLocation))
85          {
86              return false;
87          }
88          final RemoteLocation l = (RemoteLocation) obj;
89          if ( this.host == null )
90          {
91              return l.host == null && port == l.port;
92          }
93          return host.equals( l.host ) && port == l.port;
94      }
95  
96      /**
97       * @return int
98       */
99      @Override
100     public int hashCode()
101     {
102         return host == null ? port : host.hashCode() ^ port;
103     }
104 
105     /**
106      * @see Object#toString()
107      */
108     @Override
109     public String toString()
110     {
111         final 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         final Matcher match = SERVER_COLON_PORT.matcher(server);
131 
132         if (match.find() && match.groupCount() == 2)
133         {
134             return new RemoteLocation( match.group(1), Integer.parseInt( match.group(2) ) );
135         }
136         log.error("Invalid server descriptor: {0}", server);
137 
138         return null;
139     }
140 }