package org.jboss.remoting;
import org.jboss.remoting.transport.ClientInvoker;
import org.jboss.system.server.ServerConfig;
import org.jboss.system.server.ServerConfigUtil;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
public class InvokerLocator implements Serializable
{
private static final long serialVersionUID = -2909329895029296248L;
protected String protocol;
protected String host;
protected int port;
protected String path;
protected Map parameters;
private String uri;
private String originalURL;
public static final String DATATYPE = "datatype";
public static final String DATATYPE_CASED = "dataType";
public static final String MARSHALLER = "marshaller";
public static final String UNMARSHALLER = "unmarshaller";
public static final String LOADER_PORT = "loaderport";
public static final String BYVALUE = "byvalue";
public InvokerLocator(String uri)
throws MalformedURLException
{
originalURL = uri;
int i = uri.indexOf("://");
if (i < 0)
{
throw new MalformedURLException();
}
String tmp = uri.substring(i + 3);
this.protocol = uri.substring(0, i);
i = tmp.indexOf("/");
int p = tmp.indexOf(":");
if (p != -1)
{
host = resolveHost(tmp.substring(0, p).trim());
if (i > -1)
{
port = Integer.parseInt(tmp.substring(p + 1, i));
}
else
{
port = Integer.parseInt(tmp.substring(p + 1));
}
}
else
{
if (i > -1)
{
host = resolveHost(tmp.substring(0, i).trim());
}
else
{
host = resolveHost(tmp.substring(0).trim());
}
port = -1;
}
p = tmp.indexOf("?");
if (p != -1)
{
path = tmp.substring(i + 1, p);
String args = tmp.substring(p + 1);
StringTokenizer tok = new StringTokenizer(args, "&");
parameters = new HashMap(tok.countTokens());
while (tok.hasMoreTokens())
{
String token = tok.nextToken();
int eq = token.indexOf("=");
String name = (eq > -1) ? token.substring(0, eq) : token;
String value = (eq > -1) ? token.substring(eq + 1) : "";
parameters.put(name, value);
}
}
else
{
path = "";
}
this.uri = protocol + "://" + this.host + ((port > -1) ? (":" + port) : "") + "/" + path + ((parameters != null) ? "?" : "");
if (parameters != null)
{
Iterator iter = parameters.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
String val = (String) parameters.get(key);
this.uri += key + "=" + val;
if (iter.hasNext())
{
this.uri += "&";
}
}
}
}
private static final String resolveHost(String host)
{
if (host.indexOf("0.0.0.0") != -1)
{
if (System.getProperty(ServerConfig.SERVER_BIND_ADDRESS, "0.0.0.0").equals("0.0.0.0"))
{
host = ServerConfigUtil.fixRemoteAddress(host);
}
else
{
host = host.replaceAll("0\\.0\\.0\\.0", System.getProperty(ServerConfig.SERVER_BIND_ADDRESS));
}
}
try
{
return InetAddress.getByName(host).getHostAddress();
}
catch (Exception ex)
{
return host;
}
}
public InvokerLocator(String protocol, String host, int port, String path, Map parameters)
{
this.protocol = protocol;
this.host = resolveHost(host);
this.port = port;
this.path = path;
this.parameters = parameters;
this.uri = protocol + "://" + this.host + ((port > -1) ? (":" + port) : "") + "/" + path + ((parameters != null) ? "?" : "");
if (parameters != null)
{
Iterator iter = parameters.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
String val = (String) parameters.get(key);
this.uri += key + "=" + val;
if (iter.hasNext())
{
this.uri += "&";
}
}
}
originalURL = uri;
}
public int hashCode()
{
return uri.hashCode();
}
public boolean equals(Object obj)
{
return obj instanceof InvokerLocator && obj.hashCode() == hashCode();
}
public String getLocatorURI()
{
return uri;
}
public String getProtocol()
{
return protocol;
}
public String getHost()
{
return host;
}
public int getPort()
{
return port;
}
public String getPath()
{
return path;
}
public Map getParameters()
{
return parameters;
}
public String toString()
{
return "InvokerLocator [" + uri + "]";
}
public String getOriginalURI()
{
return originalURL;
}
public ClientInvoker narrow() throws Exception
{
return InvokerRegistry.createClientInvoker(this);
}
}