KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > client > UrlScheme


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.client;
19
20 /**
21  * corbaname and corbaloc urls
22  */

23
24 public class UrlScheme
25 {
26     class Addr
27     {
28         Addr(String JavaDoc h, int port, int ver)
29         {
30             _host = h;
31             _port = port;
32             _version = ver;
33         }
34
35         String JavaDoc _host;
36         int _port;
37         int _version;
38     }
39
40     static final int IIOP_1_0 = 0;
41     static final int IIOP_1_1 = 1;
42     static final int IIOP_1_2 = 2;
43
44     private boolean _corbaloc, _corbaname;
45     private java.util.ArrayList JavaDoc _addrList;
46     private String JavaDoc _url;
47     private String JavaDoc _namePrefix;
48     private String JavaDoc _objKey;
49
50     public UrlScheme(String JavaDoc url)
51     {
52         _url = url;
53         _addrList = new java.util.ArrayList JavaDoc();
54     }
55
56     public String JavaDoc getHost(int i)
57     {
58         Addr addr = getEntry(i);
59         return addr._host;
60     }
61
62     public int getPort(int i)
63     {
64         Addr addr = getEntry(i);
65         return addr._port;
66     }
67
68     public int getVersion(int i)
69     {
70         Addr addr = getEntry(i);
71         return addr._version;
72     }
73
74     public String JavaDoc getObjectKey()
75     {
76         return _objKey;
77     }
78
79     public String JavaDoc getNamePrefix()
80     {
81         return _namePrefix;
82     }
83
84     public int getAddressCount()
85     {
86         return _addrList.size();
87     }
88
89     private Addr getEntry(int i)
90     {
91         try
92         {
93             return (Addr)_addrList.get(i);
94         }
95         catch(IndexOutOfBoundsException JavaDoc e)
96         {
97             throw new IllegalArgumentException JavaDoc(_url);
98         }
99     }
100
101     public static boolean canProcess(String JavaDoc url)
102     {
103         return url.startsWith("corbaloc:") || url.startsWith("corbaname:");
104     }
105
106     public boolean process()
107     {
108         boolean handled = false;
109
110         String JavaDoc cloc = "corbaloc:";
111         String JavaDoc cname = "corbaname:";
112         
113         _corbaloc = _url.startsWith(cloc);
114         _corbaname =_url.startsWith(cname);
115
116         if(_corbaloc || _corbaname)
117         {
118             try
119             {
120                 int keySep = _url.indexOf("/");
121                 int nameSep = _url.indexOf("#");
122
123                 if(keySep > nameSep) //we may have "/" in a name
124
{
125                     keySep = -1;
126                 }
127         
128                 int addrStart = _corbaloc ? "corbaloc:".length() : "corbaname:".length();
129                 String JavaDoc addrlist;
130                 
131                 if(keySep != -1)
132                 {
133                     addrlist = _url.substring(addrStart, keySep);
134                 }
135                 else if(nameSep != -1)
136                 {
137                     addrlist = _url.substring(addrStart, nameSep);
138                 }
139                 else
140                 {
141                     addrlist = _url.substring(addrStart);
142                 }
143
144                 readAddrList(addrlist);
145                 readKeyAndName(keySep, nameSep);
146             }
147             catch(Exception JavaDoc e)
148             {
149                 throw new IllegalArgumentException JavaDoc(_url);
150             }
151         }
152
153         return handled;
154     }
155
156     //addr,addr...
157
private void readAddrList(String JavaDoc addrlist)
158     {
159         java.util.StringTokenizer JavaDoc tk = new java.util.StringTokenizer JavaDoc(addrlist, ",");
160         if(tk.countTokens() < 1)
161         {
162             throw new IllegalArgumentException JavaDoc(_url);
163         }
164         
165         while(tk.hasMoreElements())
166         {
167             String JavaDoc addr = tk.nextToken();
168             readAddr(addr);
169         }
170     }
171
172     //supported protocol: iiop
173
//addr <- <":" | "iiop:">[<version><host> [":" <port>]]
174
//version <- <major>.<minor>"@" | empty_string
175

176     private void readAddr(String JavaDoc addr)
177     {
178         String JavaDoc host;
179         int port, ver = IIOP_1_2;
180
181         int addrlen = addr.length();
182
183         if(!addr.startsWith("iiop:") && !addr.startsWith(":"))
184         {
185             throw new IllegalArgumentException JavaDoc(_url);
186         }
187
188         int curindex = addr.indexOf(":") + 1;
189
190         //VERSION
191
if( (curindex + 3) < addrlen && addr.charAt(curindex + 3) == '@' )
192         {
193             if(addr.startsWith("1.0@", curindex))
194             {
195                 ver = IIOP_1_0;
196             }
197             else if(addr.startsWith("1.1@", curindex))
198             {
199                 ver = IIOP_1_1;
200             }
201             else if(addr.startsWith("1.2@", curindex))
202             {
203                 ver = IIOP_1_2;
204             }
205             else
206             {
207                 throw new IllegalArgumentException JavaDoc(_url);
208             }
209             curindex += 4;
210         }
211
212         //defaults
213
host = "localhost";
214         port = 2089;
215
216         //HOST
217
if(curindex < addrlen)
218         {
219             //is it ipv6 address? (Enclosed between '[' and ']')
220
if(addr.charAt(curindex) == '[')
221             {
222                 int lastindex = addr.indexOf(']', curindex);
223                 if(-1 == lastindex)
224                 {
225                     throw new IllegalArgumentException JavaDoc(_url);
226                 }
227                 host = addr.substring(curindex, lastindex + 1);
228                 curindex = lastindex + 1;
229             }
230             else if(addr.charAt(curindex) != ':')
231             {
232                 int i = addr.indexOf(":", curindex);
233                 if(-1 == i)
234                 {
235                     i = addrlen;
236                 }
237                 host = addr.substring(curindex, i);
238                 curindex = i + 1;
239             }
240         }
241
242         //PORT
243
if(curindex < addrlen)
244         {
245             try
246             {
247                 String JavaDoc sport = addr.substring(curindex);
248                 port = new Integer JavaDoc(sport).intValue();
249             }
250             catch(NumberFormatException JavaDoc e)
251             {
252                 throw new IllegalArgumentException JavaDoc(_url);
253             }
254         }
255
256         _addrList.add(new Addr(host, port, ver));
257     }
258
259     //TODO: key may be escaped
260
private void readKeyAndName(int keySep, int nameSep)
261     {
262         _objKey = "NameService";
263         _namePrefix = "";
264
265         if(keySep != -1)
266         {
267             int i = nameSep;
268             if(-1 == i)
269             {
270                 i = _url.length();
271             }
272             _objKey = _url.substring(keySep + 1, i);
273         }
274
275         if(nameSep != -1)
276         {
277             _namePrefix = _url.substring(nameSep + 1);
278         }
279     }
280 }
281
Popular Tags