KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > client > impl > SimpleInternetConnectionDescriptor


1 package com.ubermq.jms.client.impl;
2
3 import com.ubermq.jms.client.*;
4 import java.net.*;
5 import java.util.*;
6
7 /**
8  * A connection descriptor that refers to a single
9  * destination <code>SocketAddress</code> (IP address and port).
10  */

11 public class SimpleInternetConnectionDescriptor
12     implements InternetConnectionDescriptor
13 {
14     private static final long serialVersionUID = 1l;
15     private SocketAddress addr;
16
17     /**
18      * Constructs a connection description from the given URL.
19      * The url is of the form <code>method://host:port</code>.<P>
20      *
21      * @param url an URL
22      * @param defaultPort the port to use if the port is unspecified in
23      * the url.
24      */

25     public SimpleInternetConnectionDescriptor(String JavaDoc url,
26                                               int defaultPort)
27     {
28         // trim leading //
29
url = url.substring(url.lastIndexOf('/') + 1);
30
31         // get host/port combo
32
String JavaDoc host;
33         int port;
34
35         StringTokenizer st = new StringTokenizer(url, ":");
36         if (st.countTokens() >= 2) {
37             host = st.nextToken();
38             port = Integer.valueOf(st.nextToken()).intValue();
39         } else {
40             host = url;
41             port = defaultPort;
42         }
43
44         this.addr = new InetSocketAddress(host, port);
45     }
46
47     /**
48      * Constructs a connection descriptor given a <code>SocketAddress</code>.
49      * @param addr an IP destination
50      */

51     public SimpleInternetConnectionDescriptor(SocketAddress addr)
52     {
53         this.addr = addr;
54     }
55
56     public SocketAddress getAddress() {return addr;}
57
58     public String JavaDoc toString()
59     {
60         return addr.toString();
61     }
62
63     public int hashCode()
64     {
65         return addr.hashCode();
66     }
67
68     public boolean equals(Object JavaDoc o)
69     {
70         if (o instanceof InternetConnectionDescriptor) {
71             return addr.equals(((InternetConnectionDescriptor)o).getAddress());
72         }
73         else return false;
74     }
75 }
76
Popular Tags