KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > IiopWriter


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.iiop;
30
31 import com.caucho.util.Alarm;
32 import com.caucho.util.IdentityIntMap;
33
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.ORB JavaDoc;
36 import org.omg.CORBA.SystemException JavaDoc;
37 import org.omg.CORBA.TCKind JavaDoc;
38 import org.omg.CORBA.TypeCode JavaDoc;
39 import org.omg.SendingContext.RunTime JavaDoc;
40
41 import javax.rmi.CORBA.ClassDesc JavaDoc;
42 import javax.rmi.CORBA.Util JavaDoc;
43 import javax.rmi.CORBA.ValueHandler JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.Serializable JavaDoc;
46
47 abstract public class IiopWriter extends org.omg.CORBA_2_3.portable.OutputStream JavaDoc {
48   static protected final int VALUE_TAG = 0x7fffff00;
49   static protected final int VALUE_HAS_CODESET = 0x1;
50   static protected final int VALUE_NO_TYPE = 0x0;
51   static protected final int VALUE_ONE_REP_ID = 0x2;
52   static protected final int VALUE_MANY_REP_IDS = 0x6;
53   
54   protected MessageWriter _out;
55   protected int _type;
56
57   protected SystemException JavaDoc _nullSystemException;
58   
59   protected IiopReader _reader;
60
61   protected String JavaDoc _host;
62   protected int _port;
63
64   private IdentityIntMap _refMap = new IdentityIntMap();
65   
66   private ValueHandler JavaDoc valueHandler = Util.createValueHandler();
67   private RunTime JavaDoc runTime = valueHandler.getRunTimeCodeBase();
68
69   /**
70    * Initialize the writer with a new underlying stream.
71    *
72    * @param ws the underlying write stream.
73    */

74   public void init(MessageWriter out)
75   {
76     _out = out;
77
78     _refMap.clear();
79   }
80
81   /**
82    * Initialize the writer with a new underlying stream and a reader.
83    *
84    * @param ws the underlying write stream.
85    * @param reader the reader
86    */

87   public void init(MessageWriter out, IiopReader reader)
88   {
89     init(out);
90     
91     _reader = reader;
92   }
93
94   /**
95    * Set the default host.
96    */

97   public void setHost(String JavaDoc host)
98   {
99     _host = host;
100   }
101
102   /**
103    * Get the default host.
104    */

105   public String JavaDoc getHost()
106   {
107     return _host;
108   }
109
110   /**
111    * Set the default port.
112    */

113   public void setPort(int port)
114   {
115     _port = port;
116   }
117
118   /**
119    * Get the default port.
120    */

121   public int getPort()
122   {
123     return _port;
124   }
125
126   /**
127    * Writes the header for a request
128    *
129    * @param operation the method to call
130    */

131   public void startRequest(IOR ior, String JavaDoc operation)
132     throws IOException JavaDoc
133   {
134     byte []bytes = ior.getOid();
135
136     startRequest(bytes, 0, bytes.length, operation);
137   }
138   
139   /**
140    * Writes the header for a request
141    *
142    * @param operation the method to call
143    */

144   public void startRequest(byte []oid, int off, int len, String JavaDoc operation)
145     throws IOException JavaDoc
146   {
147     startRequest(oid, off, len, operation, (int) Alarm.getCurrentTime());
148   }
149   
150   /**
151    * Writes the header for a request
152    *
153    * @param operation the method to call
154    */

155   abstract public void startRequest(byte []oid, int off, int len,
156                     String JavaDoc operation, int requestId)
157     throws IOException JavaDoc;
158
159   public void writeRequestServiceControlList()
160     throws IOException JavaDoc
161   {
162     write_long(1);
163     writeCodeSetService();
164   }
165
166   public void writeCodeSetService()
167     throws IOException JavaDoc
168   {
169     write_long(IiopReader.SERVICE_CODE_SET);
170     write_long(12); // length
171
write_long(0);
172     write_long(0x10001); // iso-8859-1
173
write_long(0x10100); // utf-16
174
}
175   
176   /**
177    * Writes the header for a request
178    */

179   abstract public void startReplyOk(int requestId)
180     throws IOException JavaDoc;
181   
182   /**
183    * Writes the header for a system reply exception
184    */

185   abstract public void startReplySystemException(int requestId,
186                          String JavaDoc exceptionId,
187                          int minorStatus,
188                          int completionStatus)
189     throws IOException JavaDoc;
190
191   /**
192    * Writes the header for a user exception
193    */

194   public void startReplyUserException(int requestId,
195                       String JavaDoc exceptionId)
196     throws IOException JavaDoc
197   {
198     startReplyUserException(requestId);
199
200     writeString(exceptionId);
201   }
202   
203   /**
204    * Writes the header for a user exception with no exception id
205    */

206   abstract public void startReplyUserException(int requestId)
207     throws IOException JavaDoc;
208
209   /**
210    * Writes an IOR to the output.
211    */

212   public void writeIOR(IOR ior)
213   {
214     byte []bytes = ior.getByteArray();
215
216     _out.align(4);
217     _out.write(bytes, 0, bytes.length);
218   }
219
220   /**
221    * Writes a null IOR to the packet.
222    */

223   public void writeNullIOR()
224   {
225     write_long(1);
226     write_long(0);
227     write_long(0);
228   }
229
230   /**
231    * Writes a null to the packet.
232    */

233   public void writeNull()
234   {
235     write_long(0);
236   }
237
238   /* CORBA */
239   public org.omg.CORBA.ORB JavaDoc orb()
240   {
241     //
242
ORB JavaDoc orb = ORB.init();
243
244     return orb;
245   }
246   
247   public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
248   {
249     throw new UnsupportedOperationException JavaDoc("no input stream");
250   }
251
252   /**
253    * Writes a sequence of booleans to the output stream.
254    */

255   public void write_boolean_array(boolean []value, int offset, int length)
256   {
257     for (int i = 0; i < length; i++)
258       _out.write(value[i + offset] ? 1 : 0);
259   }
260
261   /**
262    * Writes a sequence of 8-bit characters to the output stream.
263    */

264   public void write_char_array(char []value, int offset, int length)
265   {
266     for (int i = 0; i < length; i++)
267       _out.write((int) value[i + offset]);
268   }
269
270   /**
271    * Writes a sequence of 16-bit characters to the output stream.
272    */

273   public void write_wchar_array(char []value, int offset, int length)
274   {
275     for (int i = 0; i < length; i++)
276       write_wchar(value[i + offset]);
277   }
278
279   /**
280    * Writes a sequence of bytes to the output stream.
281    */

282   public void write_octet_array(byte []value, int offset, int length)
283   {
284     for (int i = 0; i < length; i++)
285       _out.write((int) value[i + offset]);
286   }
287
288   /**
289    * Writes a sequence of shorts to the output stream.
290    */

291   public void write_short_array(short []value, int offset, int length)
292   {
293     for (int i = 0; i < length; i++)
294       _out.writeShort((int) value[i + offset]);
295   }
296
297   /**
298    * Writes a sequence of unsigned shorts to the output stream.
299    */

300   public void write_ushort_array(short []value, int offset, int length)
301   {
302     _out.align(2);
303     for (int i = 0; i < length; i++)
304       _out.writeShort((int) value[i + offset]);
305   }
306
307   /**
308    * Writes a sequence of CORBA long (Java int) to the output stream.
309    */

310   public void write_long_array(int []value, int offset, int length)
311   {
312     _out.align(4);
313     for (int i = 0; i < length; i++)
314       _out.writeInt(value[i + offset]);
315   }
316
317   /**
318    * Writes a sequence of CORBA unsigned long (Java int) to the output stream.
319    */

320   public void write_ulong_array(int []value, int offset, int length)
321   {
322     _out.align(4);
323     for (int i = 0; i < length; i++)
324       _out.writeInt(value[i + offset]);
325   }
326
327   /**
328    * Writes a sequence of CORBA long long (Java long) to the output stream.
329    */

330   public void write_longlong_array(long []value, int offset, int length)
331   {
332     _out.align(8);
333     for (int i = 0; i < length; i++)
334       _out.writeLong(value[i + offset]);
335   }
336
337   /**
338    * Writes a sequence of CORBA long long (Java long) to the output stream.
339    */

340   public void write_ulonglong_array(long []value, int offset, int length)
341   {
342     _out.align(8);
343     for (int i = 0; i < length; i++)
344       _out.writeLong(value[i + offset]);
345   }
346
347   /**
348    * Writes a sequence of floats to the output stream.
349    */

350   public void write_float_array(float []value, int offset, int length)
351   {
352     _out.align(4);
353     for (int i = 0; i < length; i++) {
354       float v = value[i + offset];
355       int bits = Float.floatToIntBits(v);
356       
357       _out.writeInt(bits);
358     }
359   }
360
361   /**
362    * Writes a sequence of doubles to the output stream.
363    */

364   public void write_double_array(double []value, int offset, int length)
365   {
366     _out.align(8);
367     for (int i = 0; i < length; i++) {
368       double v = value[i + offset];
369       long bits = Double.doubleToLongBits(v);
370       
371       _out.writeLong(bits);
372     }
373   }
374
375   /**
376    * Writes a sequence of 8-bit characters to the output stream.
377    */

378   public void write_string(String JavaDoc a)
379   {
380     if (a == null) {
381       write_long(0);
382       return;
383     }
384     
385     int length = a.length();
386     write_long(length + 1);
387     for (int i = 0; i < length; i++)
388       _out.write((int) a.charAt(i));
389     _out.write(0);
390   }
391
392   /**
393    * Writes a sequence of 8-bit characters to the output stream.
394    */

395   public void write_wstring(String JavaDoc a)
396   {
397     if (a == null) {
398       write_long(0);
399       return;
400     }
401     
402     int length = a.length();
403     write_long(length + 1);
404     for (int i = 0; i < length; i++)
405       _out.writeShort((int) a.charAt(i));
406     _out.writeShort(0);
407   }
408
409   /**
410    * Writes a CORBA object to the output stream.
411    */

412   public void write_Object(org.omg.CORBA.Object JavaDoc obj)
413   {
414     if (obj == null) {
415       write_long(1);
416       write(0);
417       write_long(0);
418     }
419     else {
420       writeIOR(((DummyObjectImpl) obj).getIOR());
421     }
422   }
423
424   /**
425    * Writes a CORBA typecode to the output stream.
426    */

427   public void write_TypeCode(TypeCode JavaDoc tc)
428   {
429     //System.out.println("WRITE: " + tc);
430
if (tc == null) {
431       write_long(TCKind._tk_null);
432       return;
433     }
434       
435     try {
436       switch (tc.kind().value()) {
437       case TCKind._tk_null:
438       case TCKind._tk_void:
439       case TCKind._tk_short:
440       case TCKind._tk_ushort:
441       case TCKind._tk_long:
442       case TCKind._tk_ulong:
443       case TCKind._tk_longlong:
444       case TCKind._tk_ulonglong:
445       case TCKind._tk_float:
446       case TCKind._tk_double:
447       case TCKind._tk_longdouble:
448       case TCKind._tk_boolean:
449       case TCKind._tk_char:
450       case TCKind._tk_wchar:
451       case TCKind._tk_octet:
452       case TCKind._tk_any:
453       case TCKind._tk_TypeCode:
454     write_long(tc.kind().value());
455     break;
456       
457       case TCKind._tk_string:
458     write_long(tc.kind().value());
459     write_long(tc.length());
460     break;
461       
462       case TCKind._tk_sequence:
463     write_long(tc.kind().value());
464     write_TypeCode(tc.content_type());
465     write_long(tc.length());
466     break;
467       
468       case TCKind._tk_value:
469     // XXX: need to mark for recursive
470
write_long(tc.kind().value());
471     write_string(tc.id());
472     write_string(tc.name());
473     write_short(tc.type_modifier());
474     write_TypeCode(tc.concrete_base_type());
475     write_ulong(tc.member_count());
476     for (int i = 0; i < tc.member_count(); i++) {
477       write_string(tc.member_name(i));
478       write_TypeCode(tc.member_type(i));
479       write_short(tc.member_visibility(i));
480     }
481     break;
482       
483       case TCKind._tk_value_box:
484     write_long(tc.kind().value());
485     write_string(tc.id());
486     write_string(tc.name());
487     write_TypeCode(tc.content_type());
488     break;
489       
490       case TCKind._tk_abstract_interface:
491     // XXX: need to mark for recursive
492
write_long(tc.kind().value());
493     write_string(tc.id());
494     write_string(tc.name());
495     break;
496       
497       default:
498     System.out.println("UNKNOWN TC: " + tc + " " + tc.kind() + " " + tc.kind().value());
499     throw new UnsupportedOperationException JavaDoc(String.valueOf(tc));
500       }
501     } catch (Exception JavaDoc e) {
502       throw new RuntimeException JavaDoc(e);
503     }
504   }
505
506   /**
507    * Writes a CORBA abstract interface to the output stream.
508    */

509   public void write_abstract_interface(java.lang.Object JavaDoc obj)
510   {
511     // XXX: check for remote object
512
write_boolean(false);
513
514     write_value((Serializable JavaDoc) obj);
515   }
516   
517   public void write_any(Any JavaDoc any)
518   {
519     write_TypeCode(any.type());
520     any.write_value(this);
521   }
522   
523   public void write_Principal(org.omg.CORBA.Principal JavaDoc principal)
524   {
525     throw new UnsupportedOperationException JavaDoc();
526   }
527
528   public void write_value(Serializable JavaDoc obj, Class JavaDoc javaType)
529   {
530     write_value(obj);
531   }
532
533   public void write_value(Serializable JavaDoc obj)
534   {
535     if (obj == null) {
536       write_long(0);
537       return;
538     }
539     else if (obj instanceof String JavaDoc) {
540       write_long(VALUE_TAG | VALUE_ONE_REP_ID);
541       write_string("IDL:omg.org/CORBA/WStringValue:1.0");
542       write_wstring((String JavaDoc) obj);
543     }
544     else if (obj instanceof Class JavaDoc) {
545       write_long(VALUE_TAG | VALUE_ONE_REP_ID);
546       write_string(valueHandler.getRMIRepositoryID(ClassDesc JavaDoc.class));
547       write_value(valueHandler.getRMIRepositoryID((Class JavaDoc) obj));
548       write_value(valueHandler.getRMIRepositoryID((Class JavaDoc) obj));
549     }
550     else {
551       int oldValue = _refMap.get(obj);
552
553       if (oldValue == _refMap.NULL) {
554     _out.align(4);
555     
556     _refMap.put(obj, _out.getOffset());
557
558     write_long(VALUE_TAG | VALUE_ONE_REP_ID);
559     write_string(valueHandler.getRMIRepositoryID(obj.getClass()));
560     valueHandler.writeValue(this, obj);
561       }
562       else {
563     _out.align(4);
564
565     write_long(0xffffffff);
566     int delta = oldValue - _out.getOffset();
567     
568     write_long(delta);
569       }
570     }
571   }
572
573   /**
574    * Writes a 32-bit integer.
575    */

576   public void write(int b)
577   {
578     write_long(b);
579   }
580
581   /**
582    * Writes a boolean
583    */

584   public void write_boolean(boolean v)
585   {
586     _out.write(v ? 1 : 0);
587   }
588
589   /**
590    * Writes a 8-bit byte.
591    */

592   public void write_octet(byte v)
593   {
594     _out.write(v);
595   }
596
597   /**
598    * Writes a 16-bit short.
599    */

600   public void write_short(short v)
601   {
602     _out.align(2);
603     _out.writeShort(v);
604   }
605
606   /**
607    * Writes a 16-bit short.
608    */

609   public void write_ushort(short v)
610   {
611     _out.align(2);
612     _out.writeShort(v);
613   }
614
615   /**
616    * Writes a 8-bit char.
617    */

618   public void write_char(char v)
619   {
620     _out.write(v);
621   }
622
623   /**
624    * Writes a 16-bit char.
625    */

626   public void write_wchar(char v)
627   {
628     _out.align(2);
629     _out.writeShort(v);
630   }
631
632   /**
633    * Writes a 32-bit int
634    */

635   public void write_long(int v)
636   {
637     _out.align(4);
638     _out.writeInt(v);
639   }
640
641   /**
642    * Writes a 32-bit int
643    */

644   public void write_ulong(int v)
645   {
646     _out.align(4);
647     _out.writeInt(v);
648   }
649
650   /**
651    * Writes a 64-bit int
652    */

653   public void write_longlong(long v)
654   {
655     _out.align(8);
656     _out.writeLong(v);
657   }
658
659   /**
660    * Writes a 64-bit long
661    */

662   public void write_ulonglong(long v)
663   {
664     _out.align(8);
665     _out.writeLong(v);
666   }
667
668   /**
669    * Writes a 32-bit float.
670    */

671   public void write_float(float v)
672   {
673     int bits = Float.floatToIntBits(v);
674
675     _out.align(4);
676     _out.writeInt(bits);
677   }
678
679   /**
680    * Writes a 64-bit double.
681    */

682   public void write_double(double v)
683   {
684     long bits = Double.doubleToLongBits(v);
685
686     _out.align(8);
687     _out.writeLong(bits);
688   }
689
690   /**
691    * Writes a string to the packet.
692    *
693    * @param v string value
694    */

695   public void writeString(String JavaDoc v)
696   {
697     if (v == null) {
698       write_long(0);
699       return;
700     }
701
702     int len = v.length();
703     write_long(len + 1);
704     for (int i = 0; i < len; i++)
705       _out.write(v.charAt(i));
706     _out.write(0);
707   }
708
709   /**
710    * Writes a byte array
711    *
712    * @param b byte buffer
713    */

714   public void writeBytes(byte []b, int off, int len)
715   {
716     write_long(len);
717     _out.write(b, off, len);
718   }
719
720   /**
721    * Writes a byte array
722    *
723    * @param b byte buffer
724    */

725   public void write(byte []b, int off, int len)
726   {
727     _out.write(b, off, len);
728   }
729
730   public IiopReader _call()
731     throws IOException JavaDoc
732   {
733     _out.close();
734
735     _reader.readRequest();
736
737     return _reader;
738   }
739 }
740
Popular Tags