| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class RemoteLocation |
| 32 |
{ |
| 33 |
|
| 34 |
private static final Log log = LogManager.getLog( RemoteLocation.class ); |
| 35 |
|
| 36 |
|
| 37 |
private static final Pattern SERVER_COLON_PORT = Pattern.compile("(\\S+)\\s*:\\s*(\\d+)"); |
| 38 |
|
| 39 |
|
| 40 |
private final String host; |
| 41 |
|
| 42 |
|
| 43 |
private final int port; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public RemoteLocation( final String host, final int port ) |
| 52 |
{ |
| 53 |
this.host = host; |
| 54 |
this.port = port; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public String getHost() |
| 61 |
{ |
| 62 |
return host; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public int getPort() |
| 69 |
{ |
| 70 |
return port; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 98 |
|
| 99 |
@Override |
| 100 |
public int hashCode() |
| 101 |
{ |
| 102 |
return host == null ? port : host.hashCode() ^ port; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
} |