1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.objectweb.openccm.explorer.util.ior; 26 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import org.omg.CORBA.StringHolder ; 30 31 public final class IorPrinter { 32 private static final char CHARS[] = { ';', '/', ':', '?', '@', '&', '=', '+', '$', ',', '-', '_', '.', '!', '~', '*', '`', '(', ')' }; 33 private static final char NOT_ESCAPED[] = Sort(CHARS); 34 private static char[] Sort(char chars[]) { 35 Arrays.sort(chars); 36 return chars; 37 } 38 39 private boolean little_endian_; 40 private String type_id_; 42 private ArrayList iprofiles_ = new ArrayList (); 44 private ArrayList profiles_ = new ArrayList (); 46 private static boolean IsPrintableCharacter(char c) { 48 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || Arrays.binarySearch(NOT_ESCAPED, c) >= 0; 49 } 50 51 private byte[] GetBytes(String str, int offset) { 52 int nbytes = str.length() - offset; 53 54 if ((nbytes & 1) == 1) { 55 throw new org.omg.CORBA.INV_OBJREF ("Bad string length"); 56 } 57 58 nbytes /= 2; 59 byte bytes[] = new byte[nbytes]; 60 61 for (int i = 0, j = 4; i < nbytes; i++, j += 2) { 62 String bstr = str.substring(j, j + 2); 63 64 try { 65 bytes[i] = (byte)Integer.parseInt(bstr, 16); 66 } catch (NumberFormatException exc) { 67 throw new org.omg.CORBA.INV_OBJREF ("Bad byte value: " + bstr); 68 } 69 } 70 71 return bytes; 72 } 73 74 private void Read_ior(String ior) { 78 Reset(); 79 byte bytes[] = GetBytes(ior, 4); 80 81 try { 82 ModestInputStream in = new ModestInputStream(bytes); 83 little_endian_ = in.GetEndian(); 84 type_id_ = in.ReadString(); 85 int nprofiles = in.ReadULong(); 86 87 for (int i = 0; i < nprofiles; i++) { 88 int tag = in.ReadULong(); 89 byte profile_data[] = in.ReadOctetArray(); 90 91 if (tag == org.omg.IOP.TAG_INTERNET_IOP.value) { 92 InternetProfile iprofile = new InternetProfile(); 93 iprofile.Read_ior(profile_data); 94 iprofiles_.add(iprofile); 95 profiles_.add(iprofile); 96 } else { 97 org.omg.IOP.TaggedProfile profile = new org.omg.IOP.TaggedProfile (); 98 profile.tag = tag; 99 profile.profile_data = profile_data; 100 profiles_.add(profile); 101 } 102 } 103 } catch (ArrayIndexOutOfBoundsException exc) { 104 throw new org.omg.CORBA.INV_OBJREF ("Invalid IOR (too short?)"); 105 } 106 } 107 108 private void Read_corbaloc(String ior) { 109 Reset(); 110 int index = ior.indexOf(':') + 1; 111 boolean lastaddr = false; 113 114 do { 115 int ind = index + 2, addrend = ior.indexOf(',', ind); 116 117 if (addrend == - 1) { 118 addrend = ior.indexOf('/', ind); 119 lastaddr = true; 120 } 121 122 if (addrend == - 1) { 123 throw new org.omg.CORBA.INV_OBJREF ("Bad reference: no '/'<key_string>"); 124 } 125 InternetProfile profile = new InternetProfile(); 126 127 profile.Read_corbaloc(ior.substring(index, addrend)); 128 iprofiles_.add(profile); 129 profiles_.add(profile); 130 index = addrend + 1; 131 } while (!lastaddr); 132 byte objkey[] = index < ior.length() ? Decode(ior.substring(index)) : new byte[0]; 133 134 for (int i = 0; i < iprofiles_.size(); i++) { 135 ((InternetProfile)iprofiles_.get(i)).object_key = objkey; 136 } 138 139 type_id_ = "IDL:omg.org/CORBA/Object:1.0"; 140 } 141 142 private boolean CheckIfCorbalocPossible(StringHolder strh) { 146 if (iprofiles_.size() != profiles_.size()) { 148 if (strh != null) { 149 strh.value = "Additional profiles present in reference"; 150 } 151 152 return false; 153 } 154 InternetProfile previous = null; 157 158 for (int i = 0; i < iprofiles_.size(); i++) { 159 InternetProfile iprofile = (InternetProfile)iprofiles_.get(i); 160 161 if (iprofile.components != null && iprofile.components.length > 0) { 162 if (strh != null) { 163 strh.value = "Components present in Internet Profiles"; 164 } 165 166 return false; 167 } 168 169 if (i > 0) { 170 if (!Arrays.equals(iprofile.object_key, previous.object_key)) { 171 if (strh != null) { 172 strh.value = "Different object keys in Internet Profiles"; 173 } 174 175 return false; 176 } 177 } 178 179 previous = iprofile; 180 } 181 182 return true; 183 } 184 185 private String Write_corbaloc() { 186 StringBuffer sb = new StringBuffer (512); 187 byte objkey[] = null; 188 sb.append("corbaloc:"); 189 190 for (int i = 0; i < iprofiles_.size(); i++) { 191 InternetProfile profile = (InternetProfile)iprofiles_.get(i); 192 193 if (i == 0) { 194 objkey = profile.object_key; 195 } else { 196 sb.append(','); 197 } 198 199 profile.Write_corbaloc(sb); 200 } 201 202 sb.append('/'); 203 sb.append(Encode(objkey)); 204 return sb.toString(); 205 } 206 207 public IorPrinter() { 211 } 212 213 public IorPrinter(String ior) { 214 215 Read(ior); 216 } 217 218 public synchronized void Reset() { 222 little_endian_ = false; 223 type_id_ = null; 224 iprofiles_.clear(); 225 profiles_.clear(); 226 } 227 228 public synchronized boolean IsLittleEndian() { 229 return little_endian_; 230 } 231 232 public synchronized void SetLittleEndian(boolean le) { 233 little_endian_ = le; 234 } 235 236 public synchronized String getTypeId() { 237 return type_id_; 238 } 239 240 public synchronized void SetTypeId(String type_id) { 241 type_id_ = type_id; 242 } 243 244 public synchronized int GetInternetProfilesCount() { 245 return iprofiles_.size(); 246 } 247 248 public synchronized InternetProfile GetInternetProfile(int index) { 249 return new InternetProfile((InternetProfile)iprofiles_.get(index)); 250 } 251 252 public synchronized void SetInternetProfile(int index, InternetProfile iprofile) { 253 InternetProfile iprof = (InternetProfile)iprofiles_.get(index); 254 iprof.Set(iprofile); 255 } 256 257 public synchronized void AddInternetProfile(InternetProfile iprofile) { 258 iprofile = new InternetProfile(iprofile); 259 iprofiles_.add(iprofile); 260 profiles_.add(iprofile); 261 } 262 263 public synchronized void InsertInternetProfile(int index, InternetProfile iprofile) { 264 iprofile = new InternetProfile(iprofile); 265 iprofiles_.add(index, iprofile); 266 267 if (index > 0) { 268 Object object = iprofiles_.get(index - 1); 269 index = profiles_.indexOf(object) + 1; 270 } 271 272 profiles_.add(index, iprofile); 273 } 274 275 public synchronized void RemoveInternetProfile(int index) { 276 Object object = iprofiles_.remove(index); 277 profiles_.remove(object); 278 } 279 280 public synchronized void Read(String ior) { 284 ior = ior.trim(); 285 int len = ior.length(); 286 287 if (len < 4) { 288 throw new org.omg.CORBA.INV_OBJREF ("Object reference too short"); 290 } else if (ior.substring(0, 4).compareToIgnoreCase("IOR:") == 0) { 291 Read_ior(ior); 292 } else if (len < 9) { 293 throw new org.omg.CORBA.INV_OBJREF ("Object reference too short or invalid"); 295 } else if (ior.substring(0, 9).compareToIgnoreCase("corbaloc:") == 0) { 296 Read_corbaloc(ior); 297 } else { 298 throw new org.omg.CORBA.INV_OBJREF ("Object reference should start with \"IOR:\" or \"corbaloc:\""); 299 } 300 } 301 302 public synchronized String toString_ior() { 307 ModestOutputStream out = new ModestOutputStream(little_endian_); 308 out.WriteString(type_id_); 309 out.WriteULong(profiles_.size()); 310 311 for (int i = 0; i < profiles_.size(); i++) { 312 Object object = profiles_.get(i); 313 314 if (object instanceof InternetProfile) { 315 out.WriteULong(org.omg.IOP.TAG_INTERNET_IOP.value); 316 out.WriteOctetArray(((InternetProfile)object).Write_ior(little_endian_)); 317 } else { 318 org.omg.IOP.TaggedProfile profile = (org.omg.IOP.TaggedProfile )object; 319 out.WriteULong(profile.tag); 320 out.WriteOctetArray(profile.profile_data); 321 } 322 } 323 byte bytes[] = out.GetBytes(); 324 String str = "IOR:"; 325 326 for (int i = 0; i < bytes.length; i++) { 327 String hexstr = Integer.toHexString((int)bytes[i] & 0xFF); 328 329 if (hexstr.length() == 1) { 330 hexstr = '0' + hexstr; 331 } 332 333 str += hexstr; 334 } 335 336 return str; 337 } 338 339 public synchronized String toString_corbaloc() throws UnsupportedOperationException { 340 StringHolder strh = new StringHolder (); 341 342 if (!CheckIfCorbalocPossible(strh)) { 343 throw new UnsupportedOperationException (strh.value); 344 } 345 346 return Write_corbaloc(); 347 } 348 349 public synchronized String toString() { 353 return CheckIfCorbalocPossible(null) ? toString_corbaloc() : toString_ior(); 354 } 355 356 public synchronized String Describe() { 360 StringBuffer sb = new StringBuffer (512); 361 sb.append("byte order: " + (little_endian_ ? "little" : "big") + " endian; type_id: " + type_id_ + ';'); 362 363 for (int i = 0; i < iprofiles_.size(); i++) { 364 InternetProfile profile = (InternetProfile)iprofiles_.get(i); 365 sb.append(" [IIOPv. " + profile.major + '.' + profile.minor + "; " + "host: " + profile.host + ':' + profile.port + "; object key: "); 366 byte object_key[] = profile.object_key; 367 368 for (int j = 0; j < object_key.length; j++) { 369 String hexstr = Integer.toHexString((int)object_key[j] & 0xFF); 370 sb.append(' '); 371 372 if (hexstr.length() == 1) { 373 sb.append('0'); 374 } 375 376 sb.append(hexstr); 377 } 378 379 sb.append(']'); 380 } 381 382 return sb.toString(); 383 } 384 385 public static byte[] Decode(String str) { 389 byte bytes[] = new byte[str.length()]; 390 int blen = 0; 391 392 for (int i = 0, len = str.length(); i < len; ) { 393 char c = str.charAt(i++); 394 395 if (c == '%') { 396 bytes[blen++] = (byte)Integer.parseInt(str.substring(i, i + 2), 16); 397 i += 2; 398 } else { 399 bytes[blen++] = (byte)c; 400 } 401 } 402 403 if (blen < bytes.length) { 404 byte b[] = new byte[blen]; 405 System.arraycopy(bytes, 0, b, 0, b.length); 406 bytes = b; 407 } 408 409 return bytes; 410 } 411 412 public static String Encode(byte bytes[]) { 413 StringBuffer sb = new StringBuffer (3 * bytes.length); 414 415 for (int i = 0; i < bytes.length; ) { 416 char c = (char)bytes[i++]; 417 418 if (IsPrintableCharacter(c)) { 419 sb.append(c); 420 } else { 421 String hex = Integer.toHexString((int)c & 0xFF); 422 sb.append('%'); 423 424 if (hex.length() == 1) { 425 sb.append('0'); 426 } 427 428 sb.append(hex); 429 } 430 } 431 432 return sb.toString(); 433 } 434 435 public static void printIOR(String iorstr) { 436 try { 437 IorPrinter ior = new IorPrinter(iorstr); 438 System.out.println(ior.Describe()); 439 } catch (Throwable exc) { 440 exc.printStackTrace(); 441 } 442 } 443 444 public String getPort() { 445 InternetProfile profile = (InternetProfile)iprofiles_.get(0); 446 int port = profile.port; 447 448 if (port < 0) { 449 port += 65536; 450 } 451 452 return "" + port; 453 } 454 455 public String getHost() { 456 InternetProfile profile = (InternetProfile)iprofiles_.get(0); 457 return profile.host; 458 } 459 460 public String getVersion() { 461 InternetProfile profile = (InternetProfile)iprofiles_.get(0); 462 return "" + (int)profile.major + "." + (int)profile.minor; 463 } 464 465 public void print() { 466 InternetProfile profile = (InternetProfile)iprofiles_.get(0); 467 System.out.println("------IOR components-----"); 468 System.out.println("TypeId: " + getTypeId()); 469 System.out.println("IIOP Version: " + "" + (int)profile.major + "." + (int)profile.minor); 470 System.out.println("Host: " + profile.host); 471 System.out.println("Port: " + profile.port); 472 } 473 474 public String getProfileId(long tag) { 475 if (tag == 0) { 476 return "TAG_INTERNET_IOP"; 477 } else { 478 return "TAG_MULTIPLE_COMPONENTS"; 479 } 480 } 481 482 private String toHex(byte bs[]) { 483 String hex = ""; 484 485 for (int i = 0; i < bs.length; i++) { 486 int n1 = (bs[i] & 0xff) / 16; 487 int n2 = (bs[i] & 0xff) % 16; 488 char c1 = (char)(n1 > 9 ? ('A' + (n1 - 10)) : ('0' + n1)); 489 char c2 = (char)(n2 > 9 ? ('A' + (n2 - 10)) : ('0' + n2)); 490 hex += ((i > 0) ? " " : "") + c1 + c2; 491 } 492 493 return hex; 494 } 495 } 496 | Popular Tags |