1 9 package org.jboss.remoting; 10 11 import java.io.Serializable ; 12 import java.net.InetAddress ; 13 import java.net.MalformedURLException ; 14 import java.net.UnknownHostException ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.StringTokenizer ; 19 import org.jboss.remoting.transport.ClientInvoker; 20 21 47 public class InvokerLocator implements Serializable 48 { 49 private static final long serialVersionUID = -2909329895029296248L; 50 protected String protocol; 51 protected String host; 52 protected int port; 53 protected String path; 54 protected Map parameters; 55 private String uri; 56 private String originalURL; 57 58 private static final String ANY = "0.0.0.0"; 59 private static final String SERVER_BIND_ADDRESS = "jboss.bind.address"; 60 61 62 65 public static final String DATATYPE = "datatype"; 66 public static final String DATATYPE_CASED = "dataType"; 67 68 71 public static final String MARSHALLER = "marshaller"; 72 75 public static final String UNMARSHALLER = "unmarshaller"; 76 77 80 public static final String LOADER_PORT = "loaderport"; 81 82 86 public static final String BYVALUE = "byvalue"; 87 88 public InvokerLocator(String uri) 89 throws MalformedURLException 90 { 91 originalURL = uri; 92 int i = uri.indexOf("://"); 93 if(i < 0) 94 { 95 throw new MalformedURLException (); 96 } 97 String tmp = uri.substring(i + 3); 98 this.protocol = uri.substring(0, i); 99 i = tmp.indexOf("/"); 100 int p = tmp.indexOf(":"); 101 if(p != -1) 102 { 103 host = resolveHost(tmp.substring(0, p).trim()); 104 if(i > -1) 105 { 106 port = Integer.parseInt(tmp.substring(p + 1, i)); 107 } 108 else 109 { 110 port = Integer.parseInt(tmp.substring(p + 1)); 111 } 112 } 113 else 114 { 115 if(i > -1) 116 { 117 host = resolveHost(tmp.substring(0, i).trim()); 118 } 119 else 120 { 121 host = resolveHost(tmp.substring(0).trim()); 122 } 123 port = -1; 124 } 125 p = tmp.indexOf("?"); 126 if(p != -1) 127 { 128 path = tmp.substring(i + 1, p); 129 String args = tmp.substring(p + 1); 130 StringTokenizer tok = new StringTokenizer (args, "&"); 131 parameters = new HashMap (tok.countTokens()); 132 while(tok.hasMoreTokens()) 133 { 134 String token = tok.nextToken(); 135 int eq = token.indexOf("="); 136 String name = (eq > -1) ? token.substring(0, eq) : token; 137 String value = (eq > -1) ? token.substring(eq + 1) : ""; 138 parameters.put(name, value); 139 } 140 } 141 else 142 { 143 path = ""; 144 } 145 this.uri = protocol + "://" + this.host + ((port > -1) ? (":" + port) : "") + "/" + path + ((parameters != null) ? "?" : ""); 147 if(parameters != null) 148 { 149 Iterator iter = parameters.keySet().iterator(); 150 while(iter.hasNext()) 151 { 152 String key = (String ) iter.next(); 153 String val = (String ) parameters.get(key); 154 this.uri += key + "=" + val; 155 if(iter.hasNext()) 156 { 157 this.uri += "&"; 158 } 159 } 160 } 161 } 162 163 166 private static final String resolveHost(String host) 167 { 168 if(host.indexOf("0.0.0.0") != -1) 169 { 170 if(System.getProperty(SERVER_BIND_ADDRESS, "0.0.0.0").equals("0.0.0.0")) 171 { 172 host = fixRemoteAddress(host); 173 } 174 else 175 { 176 host = host.replaceAll("0\\.0\\.0\\.0", System.getProperty(SERVER_BIND_ADDRESS)); 177 } 178 } 179 try 180 { 181 return InetAddress.getByName(host).getHostAddress(); 182 } 183 catch(Exception ex) 184 { 185 return host; 186 } 187 } 188 189 private static String fixRemoteAddress(String address) 190 { 191 try 192 { 193 if(address == null || ANY.equals(address)) 194 { 195 return InetAddress.getLocalHost().getHostName(); 196 } 197 } 198 catch(UnknownHostException ignored) 199 { 200 } 201 return address; 202 } 203 204 205 public InvokerLocator(String protocol, String host, int port, String path, Map parameters) 206 { 207 this.protocol = protocol; 208 this.host = resolveHost(host); 209 this.port = port; 210 this.path = path; 211 this.parameters = parameters; 212 213 this.uri = protocol + "://" + this.host + ((port > -1) ? (":" + port) : "") + "/" + path + ((parameters != null) ? "?" : ""); 214 if(parameters != null) 215 { 216 Iterator iter = parameters.keySet().iterator(); 217 while(iter.hasNext()) 218 { 219 String key = (String ) iter.next(); 220 String val = (String ) parameters.get(key); 221 this.uri += key + "=" + val; 222 if(iter.hasNext()) 223 { 224 this.uri += "&"; 225 } 226 } 227 } 228 originalURL = uri; 229 } 230 231 public int hashCode() 232 { 233 return uri.hashCode(); 234 } 235 236 public boolean equals(Object obj) 237 { 238 return obj instanceof InvokerLocator && obj.hashCode() == hashCode(); 239 } 240 241 248 public String getLocatorURI() 249 { 250 return uri; 251 } 252 253 public String getProtocol() 254 { 255 return protocol; 256 } 257 258 public String getHost() 259 { 260 return host; 261 } 262 263 public int getPort() 264 { 265 return port; 266 } 267 268 public String getPath() 269 { 270 return path; 271 } 272 273 public Map getParameters() 274 { 275 return parameters; 276 } 277 278 public String toString() 279 { 280 return "InvokerLocator [" + uri + "]"; 281 } 282 283 public String getOriginalURI() 284 { 285 return originalURL; 286 } 287 288 294 public ClientInvoker narrow() throws Exception 295 { 296 return InvokerRegistry.createClientInvoker(this); 297 } 298 299 } 300 | Popular Tags |