1 19 package org.netbeans.mdr.util; 20 21 import java.io.*; 22 import java.lang.reflect.Array ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.util.*; 26 import java.lang.reflect.Field ; 27 28 import javax.jmi.reflect.*; 29 import javax.jmi.model.*; 30 31 import org.netbeans.mdr.NBMDRepositoryImpl; 32 import org.netbeans.mdr.handlers.gen.TagSupport; 33 34 import org.netbeans.mdr.storagemodel.*; 35 import org.netbeans.mdr.persistence.MOFID; 36 import org.netbeans.mdr.persistence.Storage; 37 import org.netbeans.mdr.persistence.StorageException; 38 import org.netbeans.mdr.util.DebugException; 39 import org.netbeans.mdr.handlers.*; 40 41 46 public class IOUtils extends Object { 47 48 public static final int T_NULL = 0; 49 public static final int T_STRING = 1; 50 public static final int T_BOOLEAN = 2; 51 public static final int T_MAP = 3; 52 public static final int T_INTEGER = 4; 53 public static final int T_COLLECTION = 5; 54 public static final int T_STRUCT = 6; 55 public static final int T_ENUM = 7; 56 public static final int T_STORABLE = 9; 58 public static final int T_MOF_REFERENCE = 10; 59 public static final int T_LIST_IMMUTABLE = 11; 60 public static final int T_LIST_MUTABLE = 12; 61 public static final int T_LIST_U_IMMUTABLE = 13; 62 public static final int T_LIST_U_MUTABLE = 14; 63 public static final int T_COLL_U_MUTABLE = 15; 64 public static final int T_COLL_MUTABLE = 16; 65 public static final int T_CLASS = 17; 66 public static final int T_FLOAT = 18; 67 public static final int T_DOUBLE = 19; 68 public static final int T_OBJECT = 20; 69 public static final int T_LONG = 21; 70 public static final int T_SHORT = 22; 71 public static final int T_MOFID = 23; 72 public static final int T_ARRAY = 24; 73 public static final int T_BYTE = 25; 74 75 76 public IOUtils() { 77 } 78 79 public static void write(OutputStream outputStream, Object object) throws IOException { 80 write(outputStream, object, null); 81 } 82 83 88 public static void writeInt(OutputStream outputStream, int val) throws IOException { 89 if ((val & 0x7F) == val) { 90 outputStream.write((byte)val); 91 } else { 92 byte [] arr = new byte [5]; 93 arr[1] = (byte)val; 94 arr[2] = (byte)(val >>> 8); 95 if ((val >>> 16) == 0) { 96 arr[0] = (byte)(T_SHORT | 0x80); 97 outputStream.write(arr, 0, 3); 98 } else { 99 arr[3] = (byte)(val >>> 16); 100 arr[4] = (byte)(val >>> 24); 101 arr[0] = (byte)(T_INTEGER | 0x80); 102 outputStream.write(arr, 0, 5); 103 } 104 } 105 } 106 107 public static void writeByte(OutputStream outputStream, byte val) throws IOException { 108 outputStream.write(val); 109 } 110 111 public static void writeLong(OutputStream outputStream, long val) throws IOException { 112 if ((val & 0x7F) == val) { 113 outputStream.write((byte)val); 114 } else if ((val >>> 32) == 0) { 115 outputStream.write(T_INTEGER | 0x80); 116 outputStream.write((byte)val); 117 outputStream.write((byte)(val >>> 8)); 118 outputStream.write((byte)(val >>> 16)); 119 outputStream.write((byte)(val >>> 24)); 120 } else { 121 outputStream.write(T_LONG | 0x80); 122 outputStream.write((byte)val); 123 outputStream.write((byte)(val >>> 8)); 124 outputStream.write((byte)(val >>> 16)); 125 outputStream.write((byte)(val >>> 24)); 126 outputStream.write((byte)(val >>> 32)); 127 outputStream.write((byte)(val >>> 40)); 128 outputStream.write((byte)(val >>> 48)); 129 outputStream.write((byte)(val >>> 56)); 130 } 131 } 132 133 public static void writeBoolean(OutputStream outputStream, boolean val) throws IOException { 134 outputStream.write(val ? 1 : 0); 135 } 136 137 public static void writeString(OutputStream outputStream, String val) throws IOException { 138 if (val == null) { 139 writeInt(outputStream, 0); 140 return; 141 } 142 int len = val.length(); 143 char [] arr = new char[len]; 144 val.getChars(0, len, arr, 0); 145 writeInt(outputStream, len + 1); 146 for (int i = 0; i < len; i++) { 147 char ch = arr[i]; 148 if (ch <= 0x7f) { 149 outputStream.write((byte) ch); 150 } else if (ch <= 0x7ff) { 151 outputStream.write((byte) (0xc0 | (ch >> 6))); 152 outputStream.write((byte) (0x80 | (ch & 0x3f))); 153 } else { 154 outputStream.write((byte) (0xe0 | (ch >> 12))); 155 outputStream.write((byte) (0x80 | ((ch >> 6) & 0x3f))); 156 outputStream.write((byte) (0x80 | (ch & 0x3f))); 157 } 158 } 159 } 160 161 public static void writeMOFID (OutputStream outputStream, MOFID mofId, MdrStorage mdrStorage, MOFID storableID) throws IOException { 162 Storage storage = mdrStorage.getStorageByMofId(storableID); 163 writeMOFID (outputStream, mofId, storage); 164 } 165 166 public static void writeMOFID (OutputStream outputStream, MOFID mofId, Storage storage) throws IOException { 167 try { 168 storage.writeMOFID (outputStream, mofId); 169 }catch (StorageException se) { 170 throw (IOException) Logger.getDefault().annotate(new IOException(se.getMessage()), se); 171 } 172 } 173 174 public static void write(OutputStream outputStream, Object object, StorableBaseObject storable) throws IOException { 175 if (object == null) { 176 outputStream.write(T_NULL); 177 178 } else if (object instanceof String ) { 179 outputStream.write(T_STRING); 180 writeString(outputStream, (String )object); 181 } else if (object instanceof Integer ) { 182 outputStream.write(T_INTEGER); 183 writeInt(outputStream, ((Integer )object).intValue()); 184 } else if (object instanceof Boolean ) { 185 outputStream.write(T_BOOLEAN); 186 writeBoolean(outputStream, ((Boolean ) object).booleanValue()); 187 } else if (object instanceof Float ) { 188 outputStream.write(T_FLOAT); 189 writeInt(outputStream, Float.floatToIntBits(((Float ) object).floatValue())); 190 } else if (object instanceof Double ) { 191 outputStream.write(T_DOUBLE); 192 writeLong(outputStream, Double.doubleToLongBits(((Double ) object).doubleValue())); 193 } else if (object instanceof Long ) { 194 outputStream.write(T_LONG); 195 writeLong(outputStream, ((Long ) object).longValue()); 196 } else if (object instanceof Byte ) { 197 outputStream.write(T_BYTE); 198 writeByte(outputStream, ((Byte )object).byteValue()); 199 } else if (object instanceof Map) { 200 outputStream.write(T_MAP); 201 Map temp = (Map) object; 202 writeInt(outputStream, temp.size()); 203 Map.Entry key; 204 for (Iterator it = temp.entrySet().iterator(); it.hasNext();) { 205 key = (Map.Entry) it.next(); 206 write(outputStream, key.getKey(), storable); 207 write(outputStream, key.getValue(), storable); 208 } 209 } else if (object.getClass().isArray()) { 210 outputStream.write(T_ARRAY); 211 int length = Array.getLength(object); 212 writeInt(outputStream, length); 213 if (object instanceof int[]) { 214 outputStream.write(T_INTEGER); 215 int[] a = (int[]) object; 216 for (int i = 0; i < length; i++) { 217 writeInt(outputStream, a[i]); 218 } 219 } else if (object instanceof byte[]) { 220 outputStream.write(T_BYTE); 221 byte[] a = (byte[]) object; 222 for (int i = 0; i < length; i++) { 223 writeByte(outputStream, a[i]); 224 } 225 } 226 227 } else if (object instanceof AttrCollection) { 228 if (object instanceof AttrUList) { 229 outputStream.write(T_LIST_U_MUTABLE); 230 } else if (object instanceof AttrList) { 231 outputStream.write(T_LIST_MUTABLE); 232 } else if (object instanceof AttrSet) { 233 outputStream.write(T_COLL_U_MUTABLE); 234 } else { 235 outputStream.write(T_COLL_MUTABLE); 236 } 237 ((AttrCollection) object).write(outputStream); 238 239 } else if (object instanceof AttrImmutList) { 240 if (storable == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 241 if (object instanceof AttrImmutUList) { 242 outputStream.write(T_LIST_U_IMMUTABLE); 243 } else { 244 outputStream.write(T_LIST_IMMUTABLE); 245 } 246 ((AttrImmutList) object).write(outputStream, storable); 247 248 } else if (object instanceof Collection) { 249 outputStream.write(T_COLLECTION); 250 Collection col = (Collection) object; 251 writeInt(outputStream, col.size()); 252 for (Iterator it = col.iterator(); it.hasNext();) { 253 write(outputStream, it.next(), storable); 254 } 255 256 } else if (object instanceof RefStruct) { 257 outputStream.write(T_STRUCT); 258 RefStruct struct = (RefStruct) object; 259 MdrStorage storage = storable.getMdrStorage(); 260 if (storage == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 261 writeInt(outputStream, storage.values(storable.getMofId()).indexOf(struct.refTypeName())); 262 List fields = struct.refFieldNames(); 263 writeInt(outputStream, fields.size()); 264 for (Iterator it = fields.iterator(); it.hasNext();) { 265 String field = (String ) it.next(); 266 writeInt(outputStream, storage.values(storable.getMofId()).indexOf(field)); 267 write(outputStream, struct.refGetValue(field), storable); 268 } 269 270 } else if (object instanceof RefEnum) { 271 MdrStorage storage = storable.getMdrStorage(); 272 if (storage == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 273 outputStream.write(T_ENUM); 274 writeInt(outputStream, storage.values(storable.getMofId()).indexOf(object.toString())); 275 276 } else if (object instanceof StorableClass.AttributeDescriptor) { 278 throw new DebugException("AttributeDescriptor should serialized elswhere."); 279 } else if (object instanceof StorableClass.ReferenceDescriptor) { 280 outputStream.write(T_MOF_REFERENCE); 281 writeMOFID (outputStream, ((StorableClass.ReferenceDescriptor) object).getMofId(),storable.getMdrStorage(), storable.getMofId()); 282 writeMOFID (outputStream, ((StorableClass.ReferenceDescriptor) object).getAssociationId(),storable.getMdrStorage(), storable.getMofId()); 283 writeString(outputStream, ((StorableClass.ReferenceDescriptor) object).getEndName()); 284 285 } else if (object instanceof RefObject) { 286 outputStream.write(T_MOFID); 287 writeMOFID (outputStream, ((BaseObjectHandler) object)._getDelegate().getMofId(),storable.getMdrStorage(), storable.getMofId()); 288 289 } else if (object instanceof Class ) { 290 MdrStorage storage = storable.getMdrStorage(); 291 if (storage == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 292 outputStream.write(T_CLASS); 293 writeInt(outputStream, storage.values(storable.getMofId()).indexOf(((Class ) object).getName())); 294 299 } else if (object instanceof MOFID) { 300 outputStream.write (T_MOFID); 301 writeMOFID (outputStream, (MOFID)object, storable.getMdrStorage(), storable.getMofId()); 302 } else if (object instanceof ValueWrapper) { 303 write(outputStream, ((ValueWrapper) object).getValue(), storable); 304 } else if (object instanceof Storable) { 305 outputStream.write(T_STORABLE); 306 write(outputStream, object.getClass(), storable); 307 ((Storable) object).write(outputStream, storable); 308 } else { 309 throw new IOException("Unsupported type of object (object must be serializable): " + object.getClass().getName()); 310 } 311 } 312 313 public static Object read(InputStream inputStream) throws IOException { 314 return read(inputStream, null, null); 315 } 316 317 public static Object read(InputStream inputStream, StorableBaseObject storable) throws IOException { 318 return read(inputStream, storable, null); 319 } 320 321 public static boolean readBoolean(InputStream is) throws IOException { 322 return (is.read() == 1); 323 } 324 325 public static byte readByte(InputStream is) throws IOException { 326 return (byte)is.read(); 327 } 328 329 public static int readInt(InputStream inputStream) throws IOException { 330 int type = inputStream.read(); 331 if (type <= 0x7f) 332 return type; 333 switch (type) { 334 case T_SHORT | 0x80: { 335 int ch1, ch2; 336 ch1 = inputStream.read(); 337 ch2 = inputStream.read(); 338 return (ch2 << 8) | ch1; 339 } 340 case T_INTEGER | 0x80: { 341 int ch1, ch2, ch3, ch4; 342 ch1 = inputStream.read(); 343 ch2 = inputStream.read(); 344 ch3 = inputStream.read(); 345 ch4 = inputStream.read(); 346 return (ch4 << 24) | (ch3 << 16) | (ch2 << 8) | ch1; 347 } 348 default: 349 throw new IOException("Unknown int format: " + type); 350 } 351 } 352 353 public static long readLong(InputStream inputStream) throws IOException { 354 int t = inputStream.read(); 355 if (t <= 0x7f) 356 return t; 357 switch (t) { 358 case T_INTEGER | 0x80: { 359 int ch1, ch2, ch3, ch4; 360 ch1 = inputStream.read(); 361 ch2 = inputStream.read(); 362 ch3 = inputStream.read(); 363 ch4 = inputStream.read(); 364 return (ch4 << 24) | (ch3 << 16) | (ch2 << 8) | ch1; 365 } 366 case T_LONG | 0x80: { 367 int ch1, ch2, ch3, ch4; 368 long v_low,v_high; 369 370 ch1 = inputStream.read(); 371 ch2 = inputStream.read(); 372 ch3 = inputStream.read(); 373 ch4 = inputStream.read(); 374 v_low = (ch4 << 24) | (ch3 << 16) | (ch2 << 8) | ch1; 375 ch1 = inputStream.read(); 376 ch2 = inputStream.read(); 377 ch3 = inputStream.read(); 378 ch4 = inputStream.read(); 379 v_high = ((ch4 << 24) | (ch3 << 16) | (ch2 << 8) | ch1); 380 return (v_high << 32) | (v_low & 0xFFFFFFFFL); 381 } 382 default: 383 throw new IOException("Unknown int format: " + t); 384 } 385 } 386 387 public static String readString(InputStream inputStream) throws IOException { 388 int length = readInt(inputStream); 389 if (length == 0) 390 return null; 391 else if (--length == 0) 392 return ""; 393 StringBuffer sb = new StringBuffer (length); 394 do { 395 int b = inputStream.read() & 0xff; 396 if (b >= 0xe0) { 397 b = (b & 0x0f) << 12; 398 b |= (inputStream.read() & 0x3f) << 6; 399 b |= inputStream.read() & 0x3f; 400 } else if (b >= 0xc0) { 401 b = (b & 0x1f) << 6; 402 b |= inputStream.read() & 0x3f; 403 } 404 sb.append((char)b); 405 } while (--length > 0); 406 return sb.toString().intern(); 407 } 408 409 public static MOFID readMOFID (InputStream inputStream, MdrStorage mdrStorage, MOFID storableID) throws IOException { 410 Storage storage = mdrStorage.getStorageByMofId(storableID); 411 return readMOFID (inputStream, storage); 412 } 413 414 public static MOFID readMOFID (InputStream inputStream, Storage storage) throws IOException { 415 try { 416 return storage.readMOFID (inputStream); 417 } catch (StorageException se) { 418 throw (IOException) Logger.getDefault().annotate(new IOException(se.getMessage()), se); 419 } 420 } 421 422 public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException { 423 int type = inputStream.read(); 424 return read(inputStream, storable, className, type); 425 } 426 427 public static Object read(InputStream inputStream, StorableBaseObject storable, String className, int type) throws IOException { 428 switch (type) { 429 case T_NULL: 430 return null; 431 case T_STRING: 432 return readString(inputStream); 433 case T_INTEGER: 434 return new Integer (readInt(inputStream)); 435 case T_LONG: 436 return new Long (readLong(inputStream)); 437 case T_FLOAT: 438 return new Float (Float.intBitsToFloat(readInt(inputStream))); 439 case T_DOUBLE: 440 return new Double (Double.longBitsToDouble(readLong(inputStream))); 441 case T_BYTE: 442 return new Byte (readByte(inputStream)); 443 case T_BOOLEAN: { 444 return readBoolean(inputStream) ? Boolean.TRUE : Boolean.FALSE; 445 } case T_ARRAY: { 446 int length = readInt(inputStream); 447 int elementType = inputStream.read(); 448 switch (elementType) { 449 case T_INTEGER: 450 int[] result = new int[length]; 451 for (int i = 0; i < length; i++) { 452 result[i] = readInt(inputStream); 453 } 454 return result; 455 case T_BYTE: 456 byte[] res = new byte[length]; 457 for (int i = 0; i < length; i++) { 458 res[i] = readByte(inputStream); 459 } 460 return res; 461 default: 462 throw new DebugException(); 463 } 464 } case T_MAP: { 465 int size = readInt(inputStream); 466 Map result = new HashMap(size); 467 for (int i = 0; i < size; i++) { 468 result.put(read(inputStream, storable), read(inputStream, storable)); 469 } 470 return result; 471 } case T_LIST_IMMUTABLE: 472 case T_LIST_U_IMMUTABLE: { 473 if (storable == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 474 AttrImmutList result; 475 if (type == T_LIST_IMMUTABLE) { 476 result = new AttrImmutList(); 477 } else { 478 result = new AttrImmutUList(); 479 } 480 result.read(inputStream, storable); 481 return result; 482 } case T_LIST_MUTABLE: 483 case T_LIST_U_MUTABLE: 484 case T_COLL_MUTABLE: 485 case T_COLL_U_MUTABLE: { 486 AttrCollection result; 487 if (type == T_LIST_MUTABLE) { 488 result = new AttrList(); 489 } else if (type == T_LIST_U_MUTABLE) { 490 result = new AttrUList(); 491 } else if (type == T_COLL_MUTABLE) { 492 result = new AttrCollection(); 493 } else { 494 result = new AttrSet(); 495 } 496 result.read(inputStream, (StorableFeatured) storable); 497 return result; 498 } case T_COLLECTION: { 499 int size = readInt(inputStream); 500 java.util.List result = new java.util.ArrayList (size); 501 for (int i = 0; i < size; i++) { 502 result.add(read(inputStream, storable)); 503 } 504 return result; 505 } case T_STRUCT: { 506 if (storable == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 507 MdrStorage storage = storable.getMdrStorage(); 508 List qualifiedName = (List) storage.values(storable.getMofId()).resolve(readInt(inputStream)); 509 int size = readInt(inputStream); 510 List fields = new ArrayList(size); 511 Map values = new HashMap(size, 1); 512 for (int i = 0; i < size; i++) { 513 Object field = storage.values(storable.getMofId()).resolve(readInt(inputStream)); 514 fields.add(field); 515 values.put(field, read(inputStream, storable)); 516 } 517 518 Class clazz; 519 520 if (className == null) { 521 StorableObject struct = resolveByFQN(storable, qualifiedName); 522 className = TagSupport.getDataTypeName(struct); 523 } 524 525 try { 526 clazz = BaseObjectHandler.resolveInterface(className); 527 } catch (ClassNotFoundException e) { 528 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 529 } 530 531 return StructImpl.newInstance(clazz, fields, values, qualifiedName); 532 } case T_ENUM: { 533 if (storable == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 534 return EnumResolver.resolveEnum(className, (String ) storable.getMdrStorage().values(storable.getMofId()).resolve(readInt(inputStream))); 535 } case T_MOF_REFERENCE: { 536 if (storable == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 537 return ((StorableClass) storable).new ReferenceDescriptor(readMOFID(inputStream, storable.getMdrStorage(), storable.getMofId()), readMOFID(inputStream, storable.getMdrStorage(), storable.getMofId()), readString(inputStream)); 538 } case T_CLASS: { 539 if (storable == null) Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException()); 540 try { 541 return BaseObjectHandler.resolveInterface((String ) storable.getMdrStorage().values(storable.getMofId()).resolve(readInt(inputStream))); 542 } catch (ClassNotFoundException e) { 543 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 544 } 545 } case T_MOFID: { 546 return readMOFID (inputStream, storable.getMdrStorage (), storable.getMofId()); 547 } case T_STORABLE: { 548 Class cls = (Class ) read(inputStream, storable); 549 try { 550 Method method = cls.getMethod("read", new Class [] {InputStream.class, StorableBaseObject.class}); 551 return method.invoke(null, new Object [] {inputStream, storable}); 552 } catch (NoSuchMethodException e) { 553 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 554 } catch (SecurityException e) { 555 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 556 } catch (IllegalAccessException e) { 557 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 558 } catch (InvocationTargetException e) { 559 if (e.getTargetException() instanceof IOException) { 560 throw (IOException) e.getTargetException(); 561 } 562 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e.getTargetException()); 563 } 564 } default: { 565 throw new IOException("Object type not recognized: " + type); 566 } 567 } 568 } 569 570 private static StorableObject resolveByFQN(StorableBaseObject obj, List qualifiedName) { 571 try { 572 RefPackage pkg = (RefPackage) obj.getMdrStorage().getRepository().getHandler(obj.getMetaObject().getOutermostPackage()); 573 RefObject result = resolveByFQN(pkg , qualifiedName); 574 return (StorableObject) (result == null ? null : ((BaseObjectHandler) result)._getDelegate()); 575 } catch (Exception e) { 576 Logger.getDefault().notify(e); 577 return null; 578 } 579 } 580 581 private static RefObject resolveByFQN(RefPackage pkg, List qualifiedName) { 582 qualifiedName = new ArrayList(qualifiedName); 583 String name = (String ) qualifiedName.remove(0); 584 MofPackage outer = findPackage(pkg, name); 585 if (outer == null) { 586 for (Iterator it = pkg.refAllPackages().iterator(); it.hasNext() && outer == null;) { 587 outer = findPackage((RefPackage) it.next(), name); 588 } 589 } 590 if (outer == null) return null; 591 try { 592 return outer.resolveQualifiedName(qualifiedName); 593 } catch (NameNotResolvedException e) { 594 return null; 595 } 596 } 597 598 private static MofPackage findPackage(RefPackage pkg, String name) { 599 for (Iterator it = ((ModelPackage) pkg).getMofPackage().refAllOfType().iterator(); it.hasNext();) { 600 MofPackage result = (MofPackage) it.next(); 601 if (name.equals(result.getName()) && result.refImmediateComposite() == null) { 602 return result; 603 } 604 } 605 return null; 606 } 607 608 611 public interface Storable { 612 public void write(OutputStream outputStream, StorableBaseObject storable) throws IOException; 613 } 614 615 652 } 653 | Popular Tags |