01: /*
02: * $Id: TcpSocketKey.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.tcp;
12:
13: import org.mule.api.endpoint.ImmutableEndpoint;
14:
15: import java.net.InetAddress;
16: import java.net.InetSocketAddress;
17:
18: /**
19: * This is used to adapt an endpoint so that it can be used as a key for sockets. It must
20: * meet two requirements: (1) implement hash and equals in a way that reflects socket identity
21: * (ie using address and port); (2) allow access to the endpoint for use in the socket factory.
22: * For simplicity we also expose the connector, address and port directly.
23: */
24: public class TcpSocketKey {
25:
26: private InetSocketAddress address;
27: private ImmutableEndpoint endpoint;
28:
29: public TcpSocketKey(ImmutableEndpoint endpoint) {
30: if (!(endpoint.getConnector() instanceof TcpConnector)) {
31: throw new IllegalArgumentException(
32: "Sockets must be keyed via a TCP endpoint");
33: }
34: this .endpoint = endpoint;
35: address = new InetSocketAddress(endpoint.getEndpointURI()
36: .getHost(), endpoint.getEndpointURI().getPort());
37: }
38:
39: public boolean equals(Object obj) {
40: return obj instanceof TcpSocketKey
41: && address.equals(((TcpSocketKey) obj).address);
42: }
43:
44: public int hashCode() {
45: return address.hashCode();
46: }
47:
48: public ImmutableEndpoint getEndpoint() {
49: return endpoint;
50: }
51:
52: public TcpConnector getConnector() {
53: return (TcpConnector) endpoint.getConnector();
54: }
55:
56: public InetAddress getInetAddress() {
57: return address.getAddress();
58: }
59:
60: public int getPort() {
61: return address.getPort();
62: }
63:
64: public String toString() {
65: return getInetAddress() + ":" + getPort();
66: }
67:
68: }
|