KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > IIOPAddress


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

22
23 import java.net.*;
24
25 import org.jacorb.orb.dns.DNSLookup;
26 import org.apache.avalon.framework.configuration.*;
27 import org.apache.avalon.framework.logger.Logger;
28
29 /**
30  * @author Andre Spiegel
31  * @version $Id: IIOPAddress.java,v 1.15 2004/08/25 11:37:11 simon.mcqueen Exp $
32  */

33 public class IIOPAddress
34     implements Configurable
35 {
36     private String JavaDoc hostname = null; // dns name
37
private String JavaDoc ip = null; // dotted decimal
38
private int port; // 0 .. 65536
39

40     private org.jacorb.config.Configuration configuration;
41     private boolean configured = true;
42     private DNSLookup lookup;
43     private Logger logger;
44
45
46     /**
47      * Creates a new IIOPAddress for <code>host</code> and <code>port</code>.
48      * @param host either a DNS name, or a textual representation of a
49      * numeric IP address (dotted decimal)
50      * @param port the port number represented as an integer, in the range
51      * 0..65535. As a special convenience, a negative number is
52      * converted by adding 65536 to it; this helps using values that were
53      * previously stored in a Java <code>short</code>.
54      */

55     public IIOPAddress(String JavaDoc host, int port)
56     {
57         if (host.length() == 0 )
58             throw new IllegalArgumentException JavaDoc();
59
60         lookup = new DNSLookup();
61
62         if (isIP(host))
63             this.ip = host;
64         else
65             this.hostname = host;
66
67         if (port < 0)
68             this.port = port + 65536;
69         else
70             this.port = port;
71
72         
73     }
74     
75     public void configure(Configuration configuration)
76         throws ConfigurationException
77     {
78         this.configuration = (org.jacorb.config.Configuration)configuration;
79         logger = this.configuration.getNamedLogger("jacorb.iiop.address");
80         lookup.configure(configuration);
81         configured = true;
82     }
83
84
85     public static IIOPAddress read(org.omg.CORBA.portable.InputStream JavaDoc in)
86     {
87         String JavaDoc host = in.read_string();
88         short port = in.read_ushort();
89         IIOPAddress addr = new IIOPAddress(host, port);
90         return addr;
91     }
92     
93     /**
94      * Returns true if host is a numeric IP address.
95      */

96     private static boolean isIP(String JavaDoc host)
97     {
98         int index = 0;
99         int numberStart = 0;
100         int length = host.length();
101         char ch = ' ';
102         
103         for (int i = 0; i < 4; i++)
104         {
105             while (true)
106             {
107                 if (index >= length)
108                     break;
109                 ch = host.charAt(index);
110                 if (ch == '.')
111                     break;
112                 if (ch < '0' || ch > '9')
113                     return false;
114                 index++;
115             }
116             if (index >= length && i == 3
117                 && (index - numberStart) <= 3 && (index-numberStart) > 0)
118             {
119                 return true;
120             }
121             else if (ch == '.' && (index - numberStart) <= 3
122                                && (index - numberStart) > 0)
123             {
124                 index++;
125                 numberStart = index;
126             }
127             else
128                 return false;
129         }
130         return false;
131     }
132
133     /**
134      * Returns the host part of this IIOPAddress, as a numeric IP address in
135      * dotted decimal form. If the numeric IP address was specified when
136      * this object was created, then that address is returned. Otherwise,
137      * this method performs a DNS lookup on the hostname.
138      */

139     public String JavaDoc getIP()
140     {
141         if (ip == null)
142         {
143             try
144             {
145                 ip = InetAddress.getByName(hostname).getHostAddress();
146             }
147             catch (UnknownHostException ex)
148             {
149                 throw new RuntimeException JavaDoc("could not resolve hostname: "
150                                             + hostname);
151             }
152         }
153         return ip;
154     }
155
156     /**
157      * Returns the host part of this IIOPAddress, as a DNS hostname.
158      * If the DNS name was specified when this IIOPAddress was created,
159      * then that name is returned. Otherwise, this method performs a
160      * reverse DNS lookup on the IP address.
161      */

162     public String JavaDoc getHostname()
163     {
164         if (hostname == null)
165         {
166             if (!configured)
167             {
168                 throw new Error JavaDoc("Unconfigured IIOPAddress!");
169             }
170             
171             hostname = lookup.inverseLookup(ip);
172             if (hostname == null)
173                 hostname = ip;
174         }
175         return hostname;
176     }
177
178     /**
179      * Returns the port number of this address, represented as an integer
180      * in the range 0..65535.
181      */

182     public int getPort()
183     {
184         return port;
185     }
186     
187     public boolean equals(Object JavaDoc other)
188     {
189         if (other instanceof IIOPAddress)
190         {
191             IIOPAddress x = (IIOPAddress)other;
192             if (this.port == x.port)
193                 return this.getIP().equals(x.getIP());
194             else
195                 return false;
196         }
197         else
198             return false;
199     }
200     
201     public int hashCode()
202     {
203         return this.getIP().hashCode() + port;
204     }
205     
206     public String JavaDoc toString()
207     {
208         if (hostname != null)
209             return hostname + ":" + port;
210         else
211             return ip + ":" + port;
212     }
213     
214     public byte[] toCDR()
215     {
216         CDROutputStream out = new CDROutputStream();
217         out.beginEncapsulatedArray();
218         out.write_string(ip);
219         out.write_ushort((short)port);
220         return out.getBufferCopy();
221     }
222     
223     /**
224     * Method for use by the PrintIOR utility. Previously it called
225     * getHostname() which may or may not have returned what was
226     * actually encoded in the IOR. This is of limited use for
227     * debugging purposes. This method attempts to return the string
228     * that this address was actually constructed with (i.e. what the
229     * IOR actually contains as its host string).
230     * @return Host name or IP address or both if the original host string
231     * cannot be determined.
232     */

233     public String JavaDoc getOriginalHost()
234     {
235         if (hostname == null)
236         {
237             return ip;
238         }
239         else if (ip == null)
240         {
241             return hostname;
242         }
243         else
244         {
245             return hostname + " / " + ip;
246         }
247     }
248 }
249
Popular Tags