KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.iiop;
31
32 import com.caucho.ejb.AbstractEJBObject;
33 import com.caucho.ejb.AbstractServer;
34 import com.caucho.log.Log;
35 import com.caucho.server.util.CauchoSystem;
36
37 import javax.rmi.CORBA.Util JavaDoc;
38 import java.io.Serializable JavaDoc;
39 import java.lang.reflect.Array JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 public class MarshallObject {
44   private static final Logger JavaDoc log = Log.open(MarshallObject.class);
45
46   public final static int VOID = -1;
47
48   public final static int BOOLEAN = 0;
49   public final static int BYTE = 1;
50   public final static int SHORT = 2;
51   public final static int CHAR = 3;
52   public final static int INT = 4;
53   public final static int LONG = 5;
54   public final static int FLOAT = 6;
55   public final static int DOUBLE = 7;
56
57   public final static int STRING = 8;
58
59   public final static int BOOLEAN_ARRAY = 9;
60   public final static int BYTE_ARRAY = 10;
61   public final static int SHORT_ARRAY = 11;
62   public final static int CHAR_ARRAY = 12;
63   public final static int INT_ARRAY = 13;
64   public final static int LONG_ARRAY = 14;
65   public final static int FLOAT_ARRAY = 15;
66   public final static int DOUBLE_ARRAY = 16;
67   public final static int STRING_ARRAY = 17;
68
69   public final static int OBJECT = 20;
70
71   public final static int CORBA_OBJECT = 21;
72   public final static int REMOTE = 22;
73   public final static int EJB_HOME = 23;
74   public final static int EJB_OBJECT = 24;
75
76   public final static int OBJECT_ARRAY = 25;
77
78   public final static int OBJECT_HELPER = 26;
79
80   private int _code;
81   private Class JavaDoc objClass;
82   private MarshallObject subObj;
83   private Method JavaDoc readHelper;
84   private Method JavaDoc writeHelper;
85
86   private MarshallObject()
87   {
88   }
89
90   private MarshallObject(int code)
91   {
92     _code = code;
93   }
94
95   private MarshallObject(int code, Class JavaDoc cl)
96   {
97     _code = code;
98     this.objClass = cl;
99   }
100
101   private MarshallObject(int code, Class JavaDoc cl,
102                          Method JavaDoc readHelper, Method JavaDoc writeHelper)
103   {
104     _code = code;
105     this.objClass = cl;
106     this.readHelper = readHelper;
107     this.writeHelper = writeHelper;
108   }
109
110   static MarshallObject create(Class JavaDoc cl, boolean isJava)
111   {
112     if (void.class.equals(cl))
113       return new MarshallObject(VOID);
114     else if (boolean.class.equals(cl) || Boolean JavaDoc.class.equals(cl))
115       return new MarshallObject(BOOLEAN);
116     else if (byte.class.equals(cl) || Byte JavaDoc.class.equals(cl))
117       return new MarshallObject(BYTE);
118     else if (short.class.equals(cl) || Short JavaDoc.class.equals(cl))
119       return MARSHALL_SHORT;
120     else if (char.class.equals(cl) || Character JavaDoc.class.equals(cl))
121       return MARSHALL_CHAR;
122     else if (int.class.equals(cl) || Integer JavaDoc.class.equals(cl))
123       return new MarshallObject(INT);
124     else if (long.class.equals(cl) || Long JavaDoc.class.equals(cl))
125       return MARSHALL_LONG;
126     else if (float.class.equals(cl) || Float JavaDoc.class.equals(cl))
127       return new MarshallObject(FLOAT);
128     else if (double.class.equals(cl) || Double JavaDoc.class.equals(cl))
129       return new MarshallObject(DOUBLE);
130     else if (String JavaDoc.class.equals(cl))
131       return new MarshallObject(STRING);
132     else if (String JavaDoc[].class.equals(cl) && ! isJava)
133       return new MarshallObject(STRING_ARRAY);
134     else if (javax.ejb.EJBHome JavaDoc.class.isAssignableFrom(cl))
135       return new MarshallObject(EJB_HOME, cl);
136     else if (javax.ejb.EJBObject JavaDoc.class.isAssignableFrom(cl))
137       return new MarshallObject(EJB_OBJECT, cl);
138     else if (java.rmi.Remote JavaDoc.class.isAssignableFrom(cl))
139       return new MarshallObject(REMOTE, cl);
140     else if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(cl))
141       return new MarshallObject(CORBA_OBJECT, cl);
142     else if (cl.isArray() && ! isJava) {
143       Class JavaDoc compType = cl.getComponentType();
144       MarshallObject subObj = MarshallObject.create(compType, isJava);
145       MarshallObject obj = new MarshallObject(OBJECT_ARRAY, compType);
146       obj.subObj = subObj;
147
148       return obj;
149     }
150     else {
151       Class JavaDoc helperClass = null;
152       Method JavaDoc readHelper = null;
153       Method JavaDoc writeHelper = null;
154       try {
155         helperClass = CauchoSystem.loadClass(cl.getName() + "Helper");
156
157         readHelper = helperClass.getMethod("read", new Class JavaDoc[] {
158           org.omg.CORBA.portable.InputStream JavaDoc.class
159         });
160
161         writeHelper = helperClass.getMethod("write", new Class JavaDoc[] {
162           org.omg.CORBA.portable.OutputStream JavaDoc.class, cl
163         });
164       } catch (Exception JavaDoc e) {
165       }
166
167       if (readHelper != null)
168         return new MarshallObject(OBJECT_HELPER, cl, readHelper, writeHelper);
169       else
170         return new MarshallObject(OBJECT, cl);
171     }
172   }
173
174   public Object JavaDoc unmarshall(IiopReader reader)
175     throws Exception JavaDoc
176   {
177     switch (_code) {
178     case BOOLEAN:
179       return new Boolean JavaDoc(reader.read_boolean());
180     case BYTE:
181       return new Byte JavaDoc(reader.read_octet());
182     case SHORT:
183       return new Short JavaDoc(reader.read_short());
184     case CHAR:
185       return new Character JavaDoc(reader.read_wchar());
186     case INT:
187       return new Integer JavaDoc(reader.read_long());
188     case LONG:
189       return new Long JavaDoc(reader.read_longlong());
190     case FLOAT:
191       return new Float JavaDoc(reader.read_float());
192     case DOUBLE:
193       return new Double JavaDoc(reader.read_double());
194     case STRING:
195       return (String JavaDoc) reader.read_value(String JavaDoc.class);
196     case BOOLEAN_ARRAY:
197     {
198       boolean []array = new boolean[reader.read_sequence_length()];
199       reader.read_boolean_array(array, 0, array.length);
200       return array;
201     }
202     case BYTE_ARRAY:
203     {
204       byte []array = new byte[reader.read_sequence_length()];
205       reader.read_octet_array(array, 0, array.length);
206       return array;
207     }
208     case CHAR_ARRAY:
209     {
210       char []array = new char[reader.read_sequence_length()];
211       reader.read_wchar_array(array, 0, array.length);
212       return array;
213     }
214     case SHORT_ARRAY:
215     {
216       short []array = new short[reader.read_sequence_length()];
217       reader.read_short_array(array, 0, array.length);
218       return array;
219     }
220     case INT_ARRAY:
221     {
222       int []array = new int[reader.read_sequence_length()];
223       reader.read_long_array(array, 0, array.length);
224       return array;
225     }
226     case LONG_ARRAY:
227     {
228       long []array = new long[reader.read_sequence_length()];
229       reader.read_longlong_array(array, 0, array.length);
230       return array;
231     }
232     case FLOAT_ARRAY:
233     {
234       float []array = new float[reader.read_sequence_length()];
235       reader.read_float_array(array, 0, array.length);
236       return array;
237     }
238     case DOUBLE_ARRAY:
239     {
240       double []array = new double[reader.read_sequence_length()];
241       reader.read_double_array(array, 0, array.length);
242       return array;
243     }
244     /*
245     case STRING_ARRAY:
246     {
247       String []array = new String[reader.read_sequence_length()];
248       for (int i = 0; i < array.length; i++) {
249         array[i] = reader.read_wstring();
250     System.out.println(array[i]);
251       }
252
253       return array;
254     }
255     */

256
257     case REMOTE:
258       return reader.readObject(objClass);
259
260     case CORBA_OBJECT:
261       return reader.read_Object();
262
263     case OBJECT_ARRAY:
264     {
265       int len = reader.read_sequence_length();
266       Object JavaDoc []obj = (Object JavaDoc []) Array.newInstance(objClass, len);
267
268       for (int i = 0; i < len; i++)
269         obj[i] = subObj.unmarshall(reader);
270
271       return obj;
272     }
273
274     case OBJECT_HELPER:
275     {
276       return readHelper.invoke(null, new Object JavaDoc[] { reader });
277     }
278
279     default:
280       try {
281     log.info("Class: " + objClass);
282
283         return reader.read_value(objClass);
284       } catch (Exception JavaDoc e) {
285         e.printStackTrace();
286         return null;
287       }
288     }
289   }
290
291   public void marshall(Object JavaDoc obj, IiopWriter writer)
292     throws Exception JavaDoc
293   {
294     switch (_code) {
295     case BOOLEAN:
296       writer.write_boolean(((Boolean JavaDoc) obj).booleanValue());
297       break;
298     case BYTE:
299       writer.write_octet(((Byte JavaDoc) obj).byteValue());
300       break;
301     case SHORT:
302       writer.write_short(((Short JavaDoc) obj).shortValue());
303       break;
304     case INT:
305       writer.write_long(((Integer JavaDoc) obj).intValue());
306       break;
307     case LONG:
308       writer.write_longlong(((Long JavaDoc) obj).longValue());
309       break;
310     case FLOAT:
311       writer.write_float(((Float JavaDoc) obj).floatValue());
312       break;
313     case DOUBLE:
314       writer.write_double(((Double JavaDoc) obj).doubleValue());
315       break;
316     case CHAR:
317       writer.write_wchar(((Character JavaDoc) obj).charValue());
318       break;
319     case STRING:
320       writer.write_value((String JavaDoc) obj, String JavaDoc.class);
321       break;
322     case EJB_OBJECT:
323     case REMOTE:
324       if (obj instanceof AbstractEJBObject) {
325         AbstractEJBObject absObj = (AbstractEJBObject) obj;
326
327
328         AbstractServer server = absObj.__caucho_getServer();
329     String JavaDoc local = absObj.__caucho_getId();
330
331     String JavaDoc url = server.getProtocolId() + "?" + local;
332     String JavaDoc typeName = "RMI:" + objClass.getName() + ":0";
333
334     IOR ior = new IOR(typeName, writer.getHost(), writer.getPort(), url);
335     //writer.write_boolean(true);
336
writer.write_Object(new DummyObjectImpl(ior));
337       }
338       else {
339     //writer.write_boolean(false);
340
writer.write_value((Serializable JavaDoc) obj);
341       }
342
343       break;
344       /*
345       if (obj instanceof AbstractEJBObject) {
346         AbstractEJBObject absObj = (AbstractEJBObject) obj;
347
348         AbstractServer server = absObj.__caucho_getServer();
349     String local = absObj.__caucho_getId();
350
351     String url = server.getEJBName() + "?" + local;
352     String typeName = "RMI:" + objClass.getName() + ":0";
353
354     IOR ior = new IOR(typeName, writer.getHost(), writer.getPort(), url);
355     writer.write_Object(new DummyObjectImpl(ior));
356     System.out.println("REMOTE: " + writer.getHost() + ":" + writer.getPort());
357       }
358       else {
359     Util.writeRemoteObject(writer, obj);
360       }
361       
362       break;
363       */

364     case EJB_HOME:
365       Util.writeRemoteObject(writer, obj);
366       break;
367     case CORBA_OBJECT:
368       writer.write_Object((org.omg.CORBA.Object JavaDoc) obj);
369       break;
370     case OBJECT_HELPER:
371     {
372       writeHelper.invoke(null, new Object JavaDoc[] { writer, obj });
373       break;
374     }
375     default:
376       writer.write_value((Serializable JavaDoc) obj);
377       break;
378     }
379   }
380
381   private static final MarshallObject MARSHALL_SHORT = new MarshallObject() {
382       public Object JavaDoc unmarshall(IiopReader reader)
383     throws Exception JavaDoc
384       {
385     short v = reader.read_short();
386
387     return new Short JavaDoc(v);
388       }
389
390       public void marshall(Object JavaDoc obj, IiopWriter writer)
391     throws Exception JavaDoc
392       {
393     writer.write_short(((Number JavaDoc) obj).shortValue());
394       }
395     };
396
397   private static final MarshallObject MARSHALL_CHAR = new MarshallObject() {
398       public Object JavaDoc unmarshall(IiopReader reader)
399     throws Exception JavaDoc
400       {
401     char v = reader.read_wchar();
402
403     return new Character JavaDoc(v);
404       }
405
406       public void marshall(Object JavaDoc obj, IiopWriter writer)
407     throws Exception JavaDoc
408       {
409     writer.write_wchar(((Character JavaDoc) obj).charValue());
410       }
411     };
412
413   private static final MarshallObject MARSHALL_LONG = new MarshallObject() {
414       public Object JavaDoc unmarshall(IiopReader reader)
415     throws Exception JavaDoc
416       {
417     return new Long JavaDoc(reader.read_longlong());
418       }
419
420       public void marshall(Object JavaDoc obj, IiopWriter writer)
421     throws Exception JavaDoc
422       {
423     writer.write_longlong(((Long JavaDoc) obj).longValue());
424       }
425     };
426 }
427
Popular Tags