1 package org.jacorb.orb; 2 3 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 33 public class IIOPAddress 34 implements Configurable 35 { 36 private String hostname = null; private String ip = null; private int port; 40 private org.jacorb.config.Configuration configuration; 41 private boolean configured = true; 42 private DNSLookup lookup; 43 private Logger logger; 44 45 46 55 public IIOPAddress(String host, int port) 56 { 57 if (host.length() == 0 ) 58 throw new IllegalArgumentException (); 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 in) 86 { 87 String host = in.read_string(); 88 short port = in.read_ushort(); 89 IIOPAddress addr = new IIOPAddress(host, port); 90 return addr; 91 } 92 93 96 private static boolean isIP(String 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 139 public String 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 ("could not resolve hostname: " 150 + hostname); 151 } 152 } 153 return ip; 154 } 155 156 162 public String getHostname() 163 { 164 if (hostname == null) 165 { 166 if (!configured) 167 { 168 throw new Error ("Unconfigured IIOPAddress!"); 169 } 170 171 hostname = lookup.inverseLookup(ip); 172 if (hostname == null) 173 hostname = ip; 174 } 175 return hostname; 176 } 177 178 182 public int getPort() 183 { 184 return port; 185 } 186 187 public boolean equals(Object 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 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 233 public String 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 |