1 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 ; 38 import java.io.Serializable ; 39 import java.lang.reflect.Array ; 40 import java.lang.reflect.Method ; 41 import java.util.logging.Logger ; 42 43 public class MarshallObject { 44 private static final Logger 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 objClass; 82 private MarshallObject subObj; 83 private Method readHelper; 84 private Method 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 cl) 96 { 97 _code = code; 98 this.objClass = cl; 99 } 100 101 private MarshallObject(int code, Class cl, 102 Method readHelper, Method writeHelper) 103 { 104 _code = code; 105 this.objClass = cl; 106 this.readHelper = readHelper; 107 this.writeHelper = writeHelper; 108 } 109 110 static MarshallObject create(Class cl, boolean isJava) 111 { 112 if (void.class.equals(cl)) 113 return new MarshallObject(VOID); 114 else if (boolean.class.equals(cl) || Boolean .class.equals(cl)) 115 return new MarshallObject(BOOLEAN); 116 else if (byte.class.equals(cl) || Byte .class.equals(cl)) 117 return new MarshallObject(BYTE); 118 else if (short.class.equals(cl) || Short .class.equals(cl)) 119 return MARSHALL_SHORT; 120 else if (char.class.equals(cl) || Character .class.equals(cl)) 121 return MARSHALL_CHAR; 122 else if (int.class.equals(cl) || Integer .class.equals(cl)) 123 return new MarshallObject(INT); 124 else if (long.class.equals(cl) || Long .class.equals(cl)) 125 return MARSHALL_LONG; 126 else if (float.class.equals(cl) || Float .class.equals(cl)) 127 return new MarshallObject(FLOAT); 128 else if (double.class.equals(cl) || Double .class.equals(cl)) 129 return new MarshallObject(DOUBLE); 130 else if (String .class.equals(cl)) 131 return new MarshallObject(STRING); 132 else if (String [].class.equals(cl) && ! isJava) 133 return new MarshallObject(STRING_ARRAY); 134 else if (javax.ejb.EJBHome .class.isAssignableFrom(cl)) 135 return new MarshallObject(EJB_HOME, cl); 136 else if (javax.ejb.EJBObject .class.isAssignableFrom(cl)) 137 return new MarshallObject(EJB_OBJECT, cl); 138 else if (java.rmi.Remote .class.isAssignableFrom(cl)) 139 return new MarshallObject(REMOTE, cl); 140 else if (org.omg.CORBA.Object .class.isAssignableFrom(cl)) 141 return new MarshallObject(CORBA_OBJECT, cl); 142 else if (cl.isArray() && ! isJava) { 143 Class 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 helperClass = null; 152 Method readHelper = null; 153 Method writeHelper = null; 154 try { 155 helperClass = CauchoSystem.loadClass(cl.getName() + "Helper"); 156 157 readHelper = helperClass.getMethod("read", new Class [] { 158 org.omg.CORBA.portable.InputStream .class 159 }); 160 161 writeHelper = helperClass.getMethod("write", new Class [] { 162 org.omg.CORBA.portable.OutputStream .class, cl 163 }); 164 } catch (Exception 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 unmarshall(IiopReader reader) 175 throws Exception 176 { 177 switch (_code) { 178 case BOOLEAN: 179 return new Boolean (reader.read_boolean()); 180 case BYTE: 181 return new Byte (reader.read_octet()); 182 case SHORT: 183 return new Short (reader.read_short()); 184 case CHAR: 185 return new Character (reader.read_wchar()); 186 case INT: 187 return new Integer (reader.read_long()); 188 case LONG: 189 return new Long (reader.read_longlong()); 190 case FLOAT: 191 return new Float (reader.read_float()); 192 case DOUBLE: 193 return new Double (reader.read_double()); 194 case STRING: 195 return (String ) reader.read_value(String .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 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 []obj = (Object []) 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 [] { reader }); 277 } 278 279 default: 280 try { 281 log.info("Class: " + objClass); 282 283 return reader.read_value(objClass); 284 } catch (Exception e) { 285 e.printStackTrace(); 286 return null; 287 } 288 } 289 } 290 291 public void marshall(Object obj, IiopWriter writer) 292 throws Exception 293 { 294 switch (_code) { 295 case BOOLEAN: 296 writer.write_boolean(((Boolean ) obj).booleanValue()); 297 break; 298 case BYTE: 299 writer.write_octet(((Byte ) obj).byteValue()); 300 break; 301 case SHORT: 302 writer.write_short(((Short ) obj).shortValue()); 303 break; 304 case INT: 305 writer.write_long(((Integer ) obj).intValue()); 306 break; 307 case LONG: 308 writer.write_longlong(((Long ) obj).longValue()); 309 break; 310 case FLOAT: 311 writer.write_float(((Float ) obj).floatValue()); 312 break; 313 case DOUBLE: 314 writer.write_double(((Double ) obj).doubleValue()); 315 break; 316 case CHAR: 317 writer.write_wchar(((Character ) obj).charValue()); 318 break; 319 case STRING: 320 writer.write_value((String ) obj, String .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 local = absObj.__caucho_getId(); 330 331 String url = server.getProtocolId() + "?" + local; 332 String typeName = "RMI:" + objClass.getName() + ":0"; 333 334 IOR ior = new IOR(typeName, writer.getHost(), writer.getPort(), url); 335 writer.write_Object(new DummyObjectImpl(ior)); 337 } 338 else { 339 writer.write_value((Serializable ) obj); 341 } 342 343 break; 344 364 case EJB_HOME: 365 Util.writeRemoteObject(writer, obj); 366 break; 367 case CORBA_OBJECT: 368 writer.write_Object((org.omg.CORBA.Object ) obj); 369 break; 370 case OBJECT_HELPER: 371 { 372 writeHelper.invoke(null, new Object [] { writer, obj }); 373 break; 374 } 375 default: 376 writer.write_value((Serializable ) obj); 377 break; 378 } 379 } 380 381 private static final MarshallObject MARSHALL_SHORT = new MarshallObject() { 382 public Object unmarshall(IiopReader reader) 383 throws Exception 384 { 385 short v = reader.read_short(); 386 387 return new Short (v); 388 } 389 390 public void marshall(Object obj, IiopWriter writer) 391 throws Exception 392 { 393 writer.write_short(((Number ) obj).shortValue()); 394 } 395 }; 396 397 private static final MarshallObject MARSHALL_CHAR = new MarshallObject() { 398 public Object unmarshall(IiopReader reader) 399 throws Exception 400 { 401 char v = reader.read_wchar(); 402 403 return new Character (v); 404 } 405 406 public void marshall(Object obj, IiopWriter writer) 407 throws Exception 408 { 409 writer.write_wchar(((Character ) obj).charValue()); 410 } 411 }; 412 413 private static final MarshallObject MARSHALL_LONG = new MarshallObject() { 414 public Object unmarshall(IiopReader reader) 415 throws Exception 416 { 417 return new Long (reader.read_longlong()); 418 } 419 420 public void marshall(Object obj, IiopWriter writer) 421 throws Exception 422 { 423 writer.write_longlong(((Long ) obj).longValue()); 424 } 425 }; 426 } 427 | Popular Tags |