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