1 28 29 package com.caucho.iiop; 30 31 import com.caucho.util.ByteBuffer; 32 import com.caucho.util.CharBuffer; 33 34 import java.io.IOException ; 35 import java.io.UnsupportedEncodingException ; 36 37 public class IOR { 38 private static final String RMI_VERSION = ":0000000000000000"; 39 40 public static final int TAG_INTERNET_IOP = 0; 41 public static final int TAG_MULTIPLE_COMPONENTS = 1; 42 43 public static final int TAG_ORB_TYPE = 0; 44 public static final int TAG_CODE_SETS = 1; 45 public static final int TAG_POLICIES = 2; 46 public static final int TAG_ALTERNATE_IIOP_ADDRESS = 3; 47 public static final int TAG_ASSOCIATION_OPTIONS = 13; 48 public static final int TAG_SEC_NAME = 14; 49 public static final int TAG_SPKM_1_SEC_MECH = 15; 50 public static final int TAG_SPKM_2_SEC_MECH = 16; 51 public static final int TAG_KerberosV5_SEC_MECH = 17; 52 public static final int TAG_CSI_ECMA_Secret_SEC_MECH = 18; 53 public static final int TAG_CSI_ECMA_Hybrid_SEC_MECH = 19; 54 public static final int TAG_SSL_SEC_TRANS = 20; 55 public static final int TAG_ECMA_Public_SEC_MECH = 21; 56 public static final int TAG_GENERIC_SEC_MECH = 22; 57 public static final int TAG_JAVA_CODEBASE = 25; 58 59 public static final int CS_ISO8859_1 = 0x10020; 61 public static final int CS_UTF16 = 0x10100; 63 String _typeId; 64 int major; 65 int minor; 66 String _host; 67 int port; 68 byte []oid; 69 String uri; 70 71 byte []bytes; 72 73 76 public IOR() 77 { 78 } 79 80 83 public IOR(Class type, String host, int port, String uri) 84 { 85 this("RMI:" + type.getName() + RMI_VERSION, host, port, uri); 86 } 87 88 91 public IOR(String typeId, String host, int port, String uri) 92 { 93 try { 94 _typeId = typeId; 95 this.major = 1; 96 this.minor = 2; 97 _host = host; 98 this.port = port; 99 this.uri = uri; 100 101 oid = uri.getBytes("UTF8"); 102 } catch (UnsupportedEncodingException e) { 103 } 104 } 105 106 109 public String getTypeId() 110 { 111 return _typeId; 112 } 113 114 117 public int getMajor() 118 { 119 return major; 120 } 121 122 125 public int getMinor() 126 { 127 return minor; 128 } 129 130 public void setMinor(int minor) 131 { 132 this.minor = minor; 133 } 134 135 138 public String getHost() 139 { 140 return _host; 141 } 142 143 146 public int getPort() 147 { 148 return port; 149 } 150 151 154 public byte []getOid() 155 { 156 return oid; 157 } 158 159 162 public String getURI() 163 { 164 if (uri == null) { 165 if (oid == null) 166 return null; 167 168 try { 169 uri = new String (oid, 0, oid.length, "UTF8"); 170 } catch (UnsupportedEncodingException e) { 171 } 172 } 173 174 return uri; 175 } 176 177 180 IOR read(IiopReader is) 181 throws IOException 182 { 183 _typeId = is.readString(); 184 int count = is.readInt(); 185 186 for (int i = 0; i < count; i++) { 187 int tag = is.readInt(); 188 189 if (tag != TAG_INTERNET_IOP) 190 throw new RuntimeException ("unsupported iop " + tag); 191 192 int sublen = is.readInt(); 193 194 int topEndian = is.read(); 195 major = is.read(); 196 minor = is.read(); 197 198 _host = is.readString(); 199 port = is.read_short() & 0xffff; 200 201 oid = is.readBytes(); 202 203 uri = null; 204 205 if (minor >= 1) { 206 int tagCount = is.readInt(); 207 for (int j = 0; j < tagCount; j++) { 208 int compType = is.readInt(); 209 210 if (compType == TAG_CODE_SETS) { 211 int len = is.readInt(); 212 int endian = is.readInt(); 213 int charCode = is.readInt(); 214 sublen = is.readInt(); 215 for (int k = 0; k < sublen; k++) 216 is.readInt(); 217 218 int wcharCode = is.readInt(); 219 sublen = is.readInt(); 220 for (int k = 0; k < sublen; k++) 221 is.readInt(); 222 } 223 else { 224 byte []bytes = is.readBytes(); 225 } 226 } 227 } 228 } 229 230 if (count == 0) 231 return null; 232 else 233 return this; 234 } 235 236 239 public void readByteArray(byte []buf, int offset, int length) 240 { 241 int i = 0; 242 243 int strlen = getInt(buf, offset + i); 244 i += 4; 245 246 _typeId = getString(buf, offset + i, strlen); 247 i += strlen; 248 249 i += (4 - i % 4) % 4; 250 int len = getInt(buf, offset + i); 251 i += 4; 252 253 for (int k = 0; k < len; k++) { 254 int tag = getInt(buf, offset + i); 255 i += 4; 256 257 if (tag != TAG_INTERNET_IOP) 258 throw new RuntimeException ("unsupported iop " + tag); 259 260 int sublen = getInt(buf, offset + i); 261 i += 4; 262 263 major = buf[offset + i++] & 0xff; 264 minor = buf[offset + i++] & 0xff; 265 266 i += 2; 267 268 int startOff = offset; 269 strlen = getInt(buf, offset + i); 270 i += 4; 271 272 _host = getString(buf, offset + i, strlen); 273 i += strlen; 274 275 i += i & 1; 276 port = getShort(buf, offset + i); 277 i += 2; 278 279 i += (4 - i % 4) % 4; 280 strlen = getInt(buf, offset + i); 281 i += 4; 282 283 uri = null; 284 oid = new byte[strlen]; 285 for (int j = 0; j < strlen; j++) 286 oid[j] = buf[offset + i + j]; 287 288 i += strlen; 289 i += (4 - i % 4) % 4; 290 } 291 } 292 293 296 private static int getInt(byte []buf, int offset) 297 { 298 return (((buf[offset] & 0xff) << 24) + 299 ((buf[offset + 1] & 0xff) << 16) + 300 ((buf[offset + 2] & 0xff) << 8) + 301 (buf[offset + 3] & 0xff)); 302 } 303 304 307 private static int getShort(byte []buf, int offset) 308 { 309 return (((buf[offset] & 0xff) << 8) + 310 ((buf[offset + 1] & 0xff))); 311 } 312 313 316 private static String getString(byte []buf, int offset, int len) 317 { 318 CharBuffer cb = CharBuffer.allocate(); 319 320 for (int i = 0; i < len - 1; i++) 321 cb.append((char) buf[offset + i]); 322 323 return cb.close(); 324 } 325 326 329 public byte []getByteArray() 330 { 331 if (bytes != null) 332 return bytes; 333 334 ByteBuffer bb = new ByteBuffer(); 335 336 writeString(bb, _typeId); 337 338 align4(bb); 339 bb.addInt(1); 340 341 bb.addInt(TAG_INTERNET_IOP); 342 int offset = bb.size(); 343 bb.addInt(0); 344 345 bb.add(0); bb.add(major); 347 bb.add(minor); 348 349 writeString(bb, _host); 350 351 if ((bb.size() & 0x1) == 1) 352 bb.add(0); 353 bb.addShort(port); 354 355 align4(bb); 356 bb.addInt(oid.length); 357 for (int i = 0; i < oid.length; i++) 358 bb.add(oid[i]); 359 360 if (minor >= 1) { 361 align4(bb); 362 bb.addInt(1); 364 bb.addInt(TAG_CODE_SETS); 365 bb.addInt(20); bb.addInt(0); bb.addInt(CS_ISO8859_1); bb.addInt(0); bb.addInt(CS_UTF16); bb.addInt(0); } 372 373 bb.setInt(offset, bb.size() - offset - 4); 374 375 bytes = bb.getByteArray(); 376 377 return bytes; 378 } 379 380 private void writeString(ByteBuffer bb, String str) 381 { 382 align4(bb); 383 bb.addInt(str.length() + 1); 384 for (int i = 0; i < str.length(); i++) 385 bb.add(str.charAt(i)); 386 bb.add(0); 387 } 388 389 private void align4(ByteBuffer bb) 390 { 391 int len = bb.getLength(); 392 int delta = (4 - len % 4) % 4; 393 for (int i = 0; i < delta; i++) 394 bb.add(0); 395 } 396 397 400 public String toString() 401 { 402 return "IOR:" + _typeId + "//" + _host + ":" + port + "/" + bytesToHex(oid); 403 } 404 405 private static String toHex(int v) 406 { 407 CharBuffer cb = CharBuffer.allocate(); 408 for (int i = 28; i >= 0; i -= 4) { 409 int h = (v >> i) & 0xf; 410 411 if (h >= 10) 412 cb.append((char) ('a' + h - 10)); 413 else 414 cb.append(h); 415 } 416 417 return cb.close(); 418 } 419 420 private static String toStr(int v) 421 { 422 CharBuffer cb = CharBuffer.allocate(); 423 for (int i = 24; i >= 0; i -= 8) { 424 int ch = (v >> i) & 0xff; 425 426 if (ch >= 0x20 && ch < 0x7f) 427 cb.append((char) ch); 428 else 429 break; 430 } 431 432 return cb.close(); 433 } 434 437 private String bytesToHex(byte []bytes) 438 { 439 if (bytes == null) 440 return "null"; 441 442 CharBuffer cb = CharBuffer.allocate(); 443 444 for (int i = 0; i < bytes.length; i++) { 445 int ch1 = (bytes[i] >> 4) & 0xf; 446 int ch2 = bytes[i] & 0xf; 447 448 if (ch1 < 10) 449 cb.append((char) (ch1 + '0')); 450 else 451 cb.append((char) (ch1 + 'a' - 10)); 452 453 if (ch2 < 10) 454 cb.append((char) (ch2 + '0')); 455 else 456 cb.append((char) (ch2 + 'a' - 10)); 457 } 458 459 return cb.close(); 460 } 461 } 462 | Popular Tags |