KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > InvokerLocator


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.remoting;
10
11 import java.io.Serializable JavaDoc;
12 import java.net.InetAddress JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.UnknownHostException JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19 import org.jboss.remoting.transport.ClientInvoker;
20
21 /**
22  * InvokerLocator is an object that indentifies a specific Invoker on the network, via a unique
23  * locator URI. The locator URI is in the format: <P>
24  * <p/>
25  * <tt>protocol://host[:port][/path[?param=value&param2=value2]]</tt> <P>
26  * <p/>
27  * For example, a http based locator might be: <P>
28  * <p/>
29  * <tt>http://192.168.10.1:8081</tt> <P>
30  * <p/>
31  * An example Socket based locator might be: <P>
32  * <p/>
33  * <tt>socket://192.168.10.1:9999</tt> <P>
34  * <p/>
35  * An example RMI based locator might be: <P>
36  * <p/>
37  * <tt>rmi://localhost</tt> <P>
38  * <p/>
39  * NOTE: the hostname will automatically be resolved to the outside IP address of the local machine
40  * if <tt>localhost</tt> or <tt>127.0.0.1</tt> is used as the hostname in the URI. If it cannot be
41  * determined or resolved, it will use what was passed.
42  *
43  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
44  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
45  * @version $Revision: 1.2 $
46  */

47 public class InvokerLocator implements Serializable JavaDoc
48 {
49    private static final long serialVersionUID = -2909329895029296248L;
50    protected String JavaDoc protocol;
51    protected String JavaDoc host;
52    protected int port;
53    protected String JavaDoc path;
54    protected Map JavaDoc parameters;
55    private String JavaDoc uri;
56    private String JavaDoc originalURL;
57
58    private static final String JavaDoc ANY = "0.0.0.0";
59    private static final String JavaDoc SERVER_BIND_ADDRESS = "jboss.bind.address";
60
61
62    /**
63     * Constant to define the param name to be used when defining the data type.
64     */

65    public static final String JavaDoc DATATYPE = "datatype";
66    public static final String JavaDoc DATATYPE_CASED = "dataType";
67
68    /**
69     * Constant to define the param name to be used when defining the marshaller fully qualified classname
70     */

71    public static final String JavaDoc MARSHALLER = "marshaller";
72    /**
73     * Constant to define the param name to be used when defining the unmarshaller fully qualified classname
74     */

75    public static final String JavaDoc UNMARSHALLER = "unmarshaller";
76
77    /**
78     * Constant to define what port the marshalling loader port resides on.
79     */

80    public static final String JavaDoc LOADER_PORT = "loaderport";
81
82    /**
83     * Constant to define the param name to be used when defining if marshalling should be by value,
84     * which means will be remote client invoker instead of using local client invoker.
85     */

86    public static final String JavaDoc BYVALUE = "byvalue";
87
88    public InvokerLocator(String JavaDoc uri)
89          throws MalformedURLException JavaDoc
90    {
91       originalURL = uri;
92       int i = uri.indexOf("://");
93       if(i < 0)
94       {
95          throw new MalformedURLException JavaDoc();
96       }
97       String JavaDoc 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 JavaDoc args = tmp.substring(p + 1);
130          StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(args, "&");
131          parameters = new HashMap JavaDoc(tok.countTokens());
132          while(tok.hasMoreTokens())
133          {
134             String JavaDoc token = tok.nextToken();
135             int eq = token.indexOf("=");
136             String JavaDoc name = (eq > -1) ? token.substring(0, eq) : token;
137             String JavaDoc value = (eq > -1) ? token.substring(eq + 1) : "";
138             parameters.put(name, value);
139          }
140       }
141       else
142       {
143          path = "";
144       }
145       // rebuild it, since the host probably got resolved and the port changed
146
this.uri = protocol + "://" + this.host + ((port > -1) ? (":" + port) : "") + "/" + path + ((parameters != null) ? "?" : "");
147       if(parameters != null)
148       {
149          Iterator JavaDoc iter = parameters.keySet().iterator();
150          while(iter.hasNext())
151          {
152             String JavaDoc key = (String JavaDoc) iter.next();
153             String JavaDoc val = (String JavaDoc) parameters.get(key);
154             this.uri += key + "=" + val;
155             if(iter.hasNext())
156             {
157                this.uri += "&";
158             }
159          }
160       }
161    }
162
163    /**
164     *
165     */

166    private static final String JavaDoc resolveHost(String JavaDoc 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 JavaDoc ex)
184       {
185          return host;
186       }
187    }
188
189    private static String JavaDoc fixRemoteAddress(String JavaDoc address)
190    {
191       try
192       {
193          if(address == null || ANY.equals(address))
194          {
195             return InetAddress.getLocalHost().getHostName();
196          }
197       }
198       catch(UnknownHostException JavaDoc ignored)
199       {
200       }
201       return address;
202    }
203
204
205    public InvokerLocator(String JavaDoc protocol, String JavaDoc host, int port, String JavaDoc path, Map JavaDoc 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 JavaDoc iter = parameters.keySet().iterator();
217          while(iter.hasNext())
218          {
219             String JavaDoc key = (String JavaDoc) iter.next();
220             String JavaDoc val = (String JavaDoc) 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 JavaDoc obj)
237    {
238       return obj instanceof InvokerLocator && obj.hashCode() == hashCode();
239    }
240
241    /**
242     * return the locator URI, in the format: <P>
243     * <p/>
244     * <tt>protocol://host[:port][/path[?param=value&param2=value2]]</tt>
245     *
246     * @return
247     */

248    public String JavaDoc getLocatorURI()
249    {
250       return uri;
251    }
252
253    public String JavaDoc getProtocol()
254    {
255       return protocol;
256    }
257
258    public String JavaDoc getHost()
259    {
260       return host;
261    }
262
263    public int getPort()
264    {
265       return port;
266    }
267
268    public String JavaDoc getPath()
269    {
270       return path;
271    }
272
273    public Map JavaDoc getParameters()
274    {
275       return parameters;
276    }
277
278    public String JavaDoc toString()
279    {
280       return "InvokerLocator [" + uri + "]";
281    }
282
283    public String JavaDoc getOriginalURI()
284    {
285       return originalURL;
286    }
287
288    /**
289     * narrow this invoker to a specific RemoteClientInvoker instance
290     *
291     * @return
292     * @throws Exception
293     */

294    public ClientInvoker narrow() throws Exception JavaDoc
295    {
296       return InvokerRegistry.createClientInvoker(this);
297    }
298
299 }
300
Popular Tags