1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64:
65:
81: public class IOR
82: {
83:
87: public static class CodeSets_profile
88: {
89: public CodeSets_profile()
90: {
91: int[] supported = CharSets_OSF.getSupportedCharSets();
92:
93: narrow.native_set = CharSets_OSF.NATIVE_CHARACTER;
94: narrow.conversion = supported;
95:
96: wide.native_set = CharSets_OSF.NATIVE_WIDE_CHARACTER;
97: wide.conversion = supported;
98: }
99:
100:
103: public static class CodeSet_component
104: {
105:
108: public int[] conversion;
109:
110:
113: public int native_set;
114:
115:
118: public void read(org.omg.CORBA.portable.InputStream in)
119: {
120: native_set = in.read_ulong();
121: conversion = ULongSeqHelper.read(in);
122: }
123:
124:
127: public String toString()
128: {
129: StringBuffer b = new StringBuffer();
130: b.append("native " + name(native_set));
131: if (conversion != null && conversion.length > 0)
132: {
133: b.append(" conversion ");
134: for (int i = 0; i < conversion.length; i++)
135: {
136: b.append(name(conversion[i]));
137: b.append(' ');
138: }
139: }
140: b.append(' ');
141: return b.toString();
142: }
143:
144:
147: public String toStringFormatted()
148: {
149: StringBuffer b = new StringBuffer();
150: b.append("\n Native set " + name(native_set));
151: if (conversion != null && conversion.length > 0)
152: {
153: b.append("\n Other supported sets:\n ");
154: for (int i = 0; i < conversion.length; i++)
155: {
156: b.append(name(conversion[i]));
157: b.append(' ');
158: }
159: }
160: b.append("\n");
161: return b.toString();
162: }
163:
164:
165:
168: public void write(org.omg.CORBA.portable.OutputStream out)
169: {
170: out.write_long(native_set);
171: ULongSeqHelper.write(out, conversion);
172: }
173:
174: private String name(int set)
175: {
176: return "0x" + Integer.toHexString(set) + " ("
177: + CharSets_OSF.getName(set) + ") ";
178: }
179: }
180:
181:
184: public static final int TAG_CODE_SETS = 1;
185:
186:
189: public CodeSet_component narrow = new CodeSet_component();
190:
191:
194: public CodeSet_component wide = new CodeSet_component();
195:
196:
200: public CodeSetServiceContext negotiated;
201:
202:
207: public void read(AbstractCdrInput profile)
208: {
209: BufferredCdrInput encapsulation = profile.read_encapsulation();
210: narrow.read(encapsulation);
211: wide.read(encapsulation);
212: }
213:
214:
217: public String toString()
218: {
219: return "Narrow char: " + narrow + ", Wide char: " + wide;
220: }
221:
222:
227: public void write(AbstractCdrOutput profile)
228: {
229: AbstractCdrOutput encapsulation = profile.createEncapsulation();
230: narrow.write(encapsulation);
231: wide.write(encapsulation);
232: try
233: {
234: encapsulation.close();
235: }
236: catch (IOException ex)
237: {
238: throw new InternalError();
239: }
240: }
241: }
242:
243:
246: public class Internet_profile
247: {
248:
251: public static final int TAG_INTERNET_IOP = 0;
252:
253:
256: public String host;
257:
258:
261: public Version version = new Version(1, 2);
262:
263:
266: public int port;
267:
268:
272: public CodeSets_profile CodeSets = new CodeSets_profile();
273:
274:
278: ArrayList components = new ArrayList();
279:
280:
283: public String toString()
284: {
285: StringBuffer b = new StringBuffer();
286: b.append(host);
287: b.append(":");
288: b.append(port);
289: b.append(" (v");
290: b.append(version);
291: b.append(")");
292: if (components.size() > 0)
293: b.append(" " + components.size() + " extra components.");
294: return b.toString();
295: }
296:
297:
300: public void write(AbstractCdrOutput out)
301: {
302: try
303: {
304:
305:
306: AbstractCdrOutput b = out.createEncapsulation();
307:
308: version.write(b);
309: b.write_string(host);
310:
311: b.write_ushort((short) (port & 0xFFFF));
312:
313:
314: b.write_long(key.length);
315: b.write(key);
316:
317:
318: b.write_long(1 + components.size());
319:
320: b.write_long(CodeSets_profile.TAG_CODE_SETS);
321: CodeSets.write(b);
322:
323: TaggedComponent t;
324:
325: for (int i = 0; i < components.size(); i++)
326: {
327: t = (TaggedComponent) components.get(i);
328: TaggedComponentHelper.write(b, t);
329: }
330:
331: b.close();
332: }
333: catch (Exception e)
334: {
335: MARSHAL m = new MARSHAL("Unable to write Internet profile.");
336: m.minor = Minor.IOR;
337: m.initCause(e);
338: throw m;
339: }
340: }
341: }
342:
343:
347: public static final int FAILED = 10;
348:
349:
352: public Internet_profile Internet = new Internet_profile();
353:
354:
357: public String Id;
358:
359:
362: public byte[] key;
363:
364:
368: ArrayList profiles = new ArrayList();
369:
370:
376: public boolean Big_Endian = true;
377:
378:
381: public IOR()
382: {
383: }
384:
385:
396: public static IOR parse(String stringified_reference)
397: throws BAD_PARAM
398: {
399: try
400: {
401: if (!stringified_reference.startsWith("IOR:"))
402: throw new BAD_PARAM("The string refernce must start with IOR:",
403: FAILED, CompletionStatus.COMPLETED_NO);
404:
405: IOR r = new IOR();
406:
407: ByteArrayOutputStream buf = new ByteArrayOutputStream();
408: String x = stringified_reference;
409: x = x.substring(x.indexOf(":") + 1);
410:
411: char cx;
412:
413: for (int i = 0; i < x.length(); i = i + 2)
414: {
415: cx = (char) Integer.parseInt(x.substring(i, i + 2), 16);
416: buf.write(cx);
417: }
418:
419: BufferredCdrInput cdr = new BufferredCdrInput(buf.toByteArray());
420:
421: r._read(cdr);
422: return r;
423: }
424: catch (Exception ex)
425: {
426: ex.printStackTrace();
427: throw new BAD_PARAM(ex + " while parsing " + stringified_reference,
428: FAILED, CompletionStatus.COMPLETED_NO);
429: }
430: }
431:
432:
438: public void _read(AbstractCdrInput c)
439: throws IOException, BAD_PARAM
440: {
441: int endian;
442:
443: endian = c.read_long();
444: if (endian != 0)
445: {
446: Big_Endian = false;
447: c.setBigEndian(false);
448: }
449: _read_no_endian(c);
450: }
451:
452:
466: public void _read_no_endian(AbstractCdrInput c)
467: throws IOException, BAD_PARAM
468: {
469: Id = c.read_string();
470:
471: int n_profiles = c.read_long();
472:
473: if (n_profiles == 0)
474: {
475: Id = null;
476: Internet = null;
477: return;
478: }
479:
480: for (int i = 0; i < n_profiles; i++)
481: {
482: int tag = c.read_long();
483: BufferredCdrInput profile = c.read_encapsulation();
484:
485: if (tag == Internet_profile.TAG_INTERNET_IOP)
486: {
487: Internet = new Internet_profile();
488: Internet.version = Version.read_version(profile);
489: Internet.host = profile.read_string();
490: Internet.port = profile.gnu_read_ushort();
491:
492: key = profile.read_sequence();
493:
494:
495: int n_components = 0;
496:
497: try
498: {
499: if (Internet.version.since_inclusive(1, 1))
500: n_components = profile.read_long();
501:
502: for (int t = 0; t < n_components; t++)
503: {
504: int ctag = profile.read_long();
505:
506: if (ctag == CodeSets_profile.TAG_CODE_SETS)
507: {
508: Internet.CodeSets.read(profile);
509: }
510: else
511: {
512:
513:
514: TaggedComponent pc = new TaggedComponent();
515: pc.tag = ctag;
516: pc.component_data = profile.read_sequence();
517: Internet.components.add(pc);
518: }
519: }
520: }
521: catch (Unexpected ex)
522: {
523: ex.printStackTrace();
524: }
525: }
526: else
527: {
528:
529: TaggedProfile p = new TaggedProfile();
530: p.tag = tag;
531: p.profile_data = profile.buffer.getBuffer();
532:
533: profiles.add(p);
534: }
535: }
536: }
537:
538:
542: public void _write(AbstractCdrOutput out)
543: {
544:
545: out.write(0);
546: _write_no_endian(out);
547: }
548:
549:
555: public static void write_null(AbstractCdrOutput out)
556: {
557:
558: out.write_string("");
559:
560:
561: out.write_long(0);
562: }
563:
564:
568: public void _write_no_endian(AbstractCdrOutput out)
569: {
570:
571: out.write_string(Id);
572:
573: out.write_long(1 + profiles.size());
574:
575:
576: out.write_long(Internet_profile.TAG_INTERNET_IOP);
577: Internet.write(out);
578:
579:
580: TaggedProfile tp;
581:
582: for (int i = 0; i < profiles.size(); i++)
583: {
584: tp = (TaggedProfile) profiles.get(i);
585: TaggedProfileHelper.write(out, tp);
586: }
587: }
588:
589:
592: public String toString()
593: {
594: StringBuffer b = new StringBuffer();
595: b.append(Id);
596: b.append(" at ");
597: b.append(Internet);
598:
599: if (!Big_Endian)
600: b.append(" (Little endian) ");
601:
602: b.append(" Key ");
603:
604: for (int i = 0; i < key.length; i++)
605: {
606: b.append(Integer.toHexString(key[i] & 0xFF));
607: }
608:
609: b.append(" ");
610: b.append(Internet.CodeSets);
611:
612: return b.toString();
613: }
614:
615:
619: public String toStringFormatted()
620: {
621: StringBuffer b = new StringBuffer();
622: b.append("\nObject Id:\n ");
623: b.append(Id);
624: b.append("\nObject is accessible at:\n ");
625: b.append(Internet);
626:
627: if (Big_Endian)
628: b.append("\n Big endian encoding");
629: else
630: b.append("\n Little endian encoding.");
631:
632: b.append("\nObject Key\n ");
633:
634: for (int i = 0; i < key.length; i++)
635: {
636: b.append(Integer.toHexString(key[i] & 0xFF));
637: }
638:
639: b.append("\nSupported code sets:");
640: b.append("\n Wide:");
641: b.append(Internet.CodeSets.wide.toStringFormatted());
642: b.append(" Narrow:");
643: b.append(Internet.CodeSets.wide.toStringFormatted());
644:
645: return b.toString();
646: }
647:
648:
653: public String toStringifiedReference()
654: {
655: BufferedCdrOutput out = new BufferedCdrOutput();
656:
657: _write(out);
658:
659: StringBuffer b = new StringBuffer("IOR:");
660:
661: byte[] binary = out.buffer.toByteArray();
662: String s;
663:
664: for (int i = 0; i < binary.length; i++)
665: {
666: s = Integer.toHexString(binary[i] & 0xFF);
667: if (s.length() == 1)
668: b.append('0');
669: b.append(s);
670: }
671:
672: return b.toString();
673: }
674:
675:
681: public void add_ior_component(TaggedComponent tagged_component)
682: {
683:
684: Internet.components.add(tagged_component);
685:
686:
687: for (int i = 0; i < profiles.size(); i++)
688: {
689: TaggedProfile profile = (TaggedProfile) profiles.get(i);
690: addComponentTo(profile, tagged_component);
691: }
692: }
693:
694:
703: public void add_ior_component_to_profile(TaggedComponent tagged_component,
704: int profile_id)
705: {
706: if (profile_id == TAG_INTERNET_IOP.value)
707:
708: Internet.components.add(tagged_component);
709: else
710: {
711:
712: for (int i = 0; i < profiles.size(); i++)
713: {
714: TaggedProfile profile = (TaggedProfile) profiles.get(i);
715: if (profile.tag == profile_id)
716: addComponentTo(profile, tagged_component);
717: }
718: }
719: }
720:
721:
727: private static void addComponentTo(TaggedProfile profile,
728: TaggedComponent component)
729: {
730: if (profile.tag == TAG_MULTIPLE_COMPONENTS.value)
731: {
732: TaggedComponent[] present;
733: if (profile.profile_data.length > 0)
734: {
735: BufferredCdrInput in = new BufferredCdrInput(profile.profile_data);
736:
737: present = new TaggedComponent[in.read_long()];
738:
739: for (int i = 0; i < present.length; i++)
740: {
741: present[i] = TaggedComponentHelper.read(in);
742: }
743: }
744: else
745: present = new TaggedComponent[0];
746:
747: BufferedCdrOutput out = new BufferedCdrOutput(profile.profile_data.length
748: + component.component_data.length
749: + 8);
750:
751:
752: out.write_long(present.length + 1);
753:
754:
755: for (int i = 0; i < present.length; i++)
756: TaggedComponentHelper.write(out, present[i]);
757:
758:
759: TaggedComponentHelper.write(out, component);
760:
761: try
762: {
763: out.close();
764: }
765: catch (IOException e)
766: {
767: throw new Unexpected(e);
768: }
769: profile.profile_data = out.buffer.toByteArray();
770: }
771: else
772:
773: throw new BAD_PARAM("Unsupported profile type " + profile.tag);
774: }
775:
776:
779: public boolean equals(Object x)
780: {
781: if (x instanceof IOR)
782: {
783: boolean keys;
784: boolean hosts = true;
785:
786: IOR other = (IOR) x;
787:
788: if (Internet==null || other.Internet==null)
789: return Internet == other.Internet;
790:
791: if (key != null && other.key != null)
792: keys = Arrays.equals(key, other.key);
793: else
794: keys = key == other.key;
795:
796: if (Internet != null && Internet.host != null)
797: if (other.Internet != null && other.Internet.host != null)
798: hosts = other.Internet.host.equals(Internet.host);
799:
800: return keys & hosts && Internet.port==other.Internet.port;
801: }
802: else
803: return false;
804: }
805:
806:
809: public int hashCode()
810: {
811: Adler32 adler = new Adler32();
812: if (key != null)
813: adler.update(key);
814: if (Internet != null)
815: {
816: if (Internet.host != null)
817: adler.update(Internet.host.getBytes());
818: adler.update(Internet.port);
819: }
820: return (int) adler.getValue();
821: }