1 18 package org.apache.geronimo.interop.rmi.iiop.client; 19 20 23 24 public class UrlScheme 25 { 26 class Addr 27 { 28 Addr(String h, int port, int ver) 29 { 30 _host = h; 31 _port = port; 32 _version = ver; 33 } 34 35 String _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 _addrList; 46 private String _url; 47 private String _namePrefix; 48 private String _objKey; 49 50 public UrlScheme(String url) 51 { 52 _url = url; 53 _addrList = new java.util.ArrayList (); 54 } 55 56 public String 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 getObjectKey() 75 { 76 return _objKey; 77 } 78 79 public String 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 e) 96 { 97 throw new IllegalArgumentException (_url); 98 } 99 } 100 101 public static boolean canProcess(String url) 102 { 103 return url.startsWith("corbaloc:") || url.startsWith("corbaname:"); 104 } 105 106 public boolean process() 107 { 108 boolean handled = false; 109 110 String cloc = "corbaloc:"; 111 String 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) { 125 keySep = -1; 126 } 127 128 int addrStart = _corbaloc ? "corbaloc:".length() : "corbaname:".length(); 129 String 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 e) 148 { 149 throw new IllegalArgumentException (_url); 150 } 151 } 152 153 return handled; 154 } 155 156 private void readAddrList(String addrlist) 158 { 159 java.util.StringTokenizer tk = new java.util.StringTokenizer (addrlist, ","); 160 if(tk.countTokens() < 1) 161 { 162 throw new IllegalArgumentException (_url); 163 } 164 165 while(tk.hasMoreElements()) 166 { 167 String addr = tk.nextToken(); 168 readAddr(addr); 169 } 170 } 171 172 176 private void readAddr(String addr) 177 { 178 String 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 (_url); 186 } 187 188 int curindex = addr.indexOf(":") + 1; 189 190 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 (_url); 208 } 209 curindex += 4; 210 } 211 212 host = "localhost"; 214 port = 2089; 215 216 if(curindex < addrlen) 218 { 219 if(addr.charAt(curindex) == '[') 221 { 222 int lastindex = addr.indexOf(']', curindex); 223 if(-1 == lastindex) 224 { 225 throw new IllegalArgumentException (_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 if(curindex < addrlen) 244 { 245 try 246 { 247 String sport = addr.substring(curindex); 248 port = new Integer (sport).intValue(); 249 } 250 catch(NumberFormatException e) 251 { 252 throw new IllegalArgumentException (_url); 253 } 254 } 255 256 _addrList.add(new Addr(host, port, ver)); 257 } 258 259 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 |