KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > HostService


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 /**
26  * Often a target service may be repesented by a hostname (or IP address) and
27  * a port.
28  * <p>
29  * This object may be contructed either by provided a host name and port
30  * seperately or by providing a string in the format <b>[host]:[port]</b>.
31  *
32  * @author Brett Smith <brett@3sp.com>
33  */

34 public class HostService {
35     
36     // Private instance variables
37

38     private String JavaDoc hostname;
39     private int port;
40
41
42     /**
43      * Constructor
44      *
45      * @param hostname host
46      * @param port port
47      */

48     public HostService(String JavaDoc hostname, int port) {
49         super();
50         this.hostname = hostname;
51         this.port = port;
52     }
53
54     /**
55      * Constructor. Takes a string in the format <b>[host]:[port]</b>.
56      *
57      * @param uri uri in the format <b>[host]:[port]</b>
58      */

59     public HostService(String JavaDoc uri) {
60         int i = uri.indexOf(':');
61         if (i != -1) {
62             String JavaDoc addr = uri.substring(0, i);
63             if (addr.indexOf('/') > 0)
64                 addr = addr.substring(addr.indexOf('/') + 1);
65             uri = uri.substring(i + 1);
66             if (addr.length() > 0) {
67                 hostname = addr;
68             }
69         }
70
71         try {
72             port = Integer.parseInt(uri);
73         }
74         catch(NumberFormatException JavaDoc nfe) {
75             hostname = uri;
76         }
77     }
78
79     /**
80      * Get the hostname / IP address
81      *
82      * @return hostname / IP address
83      */

84     public String JavaDoc getHost() {
85         return hostname;
86     }
87
88     /**
89      * Set the hostname / IP address
90      *
91      * @param hostname hostname / IP address
92      */

93     public void setHost(String JavaDoc hostname) {
94         this.hostname = hostname;
95     }
96
97     /**
98      * Get the port
99      *
100      * @return port.
101      */

102     public int getPort() {
103         return port;
104     }
105
106     /**
107      * Set the port
108      *
109      * @param port port.
110      */

111     public void setPort(int port) {
112         this.port = port;
113     }
114
115     /**
116      * Get the hostname as an {@link InetAddress}.
117      *
118      * @return address
119      * @throws UnknownHostException if hostname is not a valid host
120      */

121     public InetAddress JavaDoc getAddress() throws UnknownHostException JavaDoc {
122         return InetAddress.getByName(getHost());
123     }
124     
125     /**
126      * Return a string representation of this host / port
127      *
128      * @return string representation
129      */

130     public String JavaDoc toString() {
131         return getHost() + ":" + getPort();
132     }
133 }
134
Popular Tags