1 45 package org.openejb.client; 46 47 48 import java.io.IOException ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectOutput ; 51 import java.lang.reflect.Method ; 52 53 58 public class EJBRequest implements Request { 59 60 private transient int requestMethod; 61 private transient int deploymentCode = 0; 62 private transient Object clientIdentity; 63 private transient Method methodInstance; 64 65 private transient Class methodClass; 66 private transient String methodName; 67 private transient Class [] methodParamTypes; 68 private transient Object [] methodParameters; 69 private transient String deploymentId; 70 private transient Object primaryKey; 71 72 73 77 public static final int SESSION_BEAN_STATELESS = 6; 78 public static final int SESSION_BEAN_STATEFUL = 7; 79 public static final int ENTITY_BM_PERSISTENCE = 8; 80 public static final int ENTITY_CM_PERSISTENCE = 9; 81 82 83 public EJBRequest() { 84 85 } 86 87 public EJBRequest(int requestMethod) { 88 this.requestMethod = requestMethod; 89 } 90 91 public byte getRequestType(){ 92 return EJB_REQUEST; 93 } 94 95 public int getRequestMethod(){ 96 return requestMethod; 97 } 98 99 public Object getClientIdentity(){ 100 return clientIdentity; 101 } 102 103 public Method getMethodInstance(){ 104 return methodInstance; 105 } 106 107 public Object [] getMethodParameters(){ 108 return methodParameters; 109 } 110 111 public String getDeploymentId(){ 112 return deploymentId; 113 } 114 115 public int getDeploymentCode(){ 116 return deploymentCode; 117 } 118 119 public Object getPrimaryKey(){ 120 return primaryKey; 121 } 122 123 124 public Class getMethodClass(){ 125 return methodClass; 126 } 127 128 public String getMethodName(){ 129 return methodName; 130 } 131 132 public Class [] getMethodParamTypes(){ 133 return methodParamTypes; 134 } 135 136 138 public void setRequestMethod(int requestMethod){ 139 this.requestMethod = requestMethod; 140 } 141 142 public void setClientIdentity(Object clientIdentity){ 143 this.clientIdentity = clientIdentity; 144 } 145 146 public void setMethodInstance(Method methodInstance){ 147 this.methodInstance = methodInstance; 148 this.methodClass = methodInstance.getDeclaringClass(); 149 this.methodName = methodInstance.getName(); 150 this.methodParamTypes = methodInstance.getParameterTypes(); 151 } 152 153 public void setMethodParameters(Object [] methodParameters){ 154 this.methodParameters = methodParameters; 155 } 156 157 public void setDeploymentId(String deploymentId){ 158 this.deploymentId = deploymentId; 159 } 160 161 public void setDeploymentCode(int deploymentCode){ 162 this.deploymentCode = deploymentCode; 163 } 164 165 public void setPrimaryKey(Object primaryKey){ 166 this.primaryKey = primaryKey; 167 } 168 169 public String toString(){ 170 StringBuffer s = null; 171 switch (requestMethod) { 172 case EJB_HOME_GET_EJB_META_DATA: 173 s = new StringBuffer ( "EJB_HOME.GET_EJB_META_DATA" ); 174 break; 175 case EJB_HOME_GET_HOME_HANDLE: 176 s = new StringBuffer ( "EJB_HOME.GET_HOME_HANDLE" ); 177 break; 178 case EJB_HOME_REMOVE_BY_HANDLE: 179 s = new StringBuffer ( "EJB_HOME.REMOVE_BY_HANDLE" ); 180 break; 181 case EJB_HOME_REMOVE_BY_PKEY: 182 s = new StringBuffer ( "EJB_HOME.REMOVE_BY_PKEY" ); 183 break; 184 case EJB_HOME_FIND: 185 s = new StringBuffer ( "EJB_HOME.FIND" ); 186 break; 187 case EJB_HOME_CREATE: 188 s = new StringBuffer ( "EJB_HOME.CREATE" ); 189 break; 190 case EJB_OBJECT_GET_EJB_HOME: 191 s = new StringBuffer ( "EJB_OBJECT.GET_EJB_HOME" ); 192 break; 193 case EJB_OBJECT_GET_HANDLE: 194 s = new StringBuffer ( "EJB_OBJECT.GET_HANDLE" ); 195 break; 196 case EJB_OBJECT_GET_PRIMARY_KEY: 197 s = new StringBuffer ( "EJB_OBJECT.GET_PRIMARY_KEY" ); 198 break; 199 case EJB_OBJECT_IS_IDENTICAL: 200 s = new StringBuffer ( "EJB_OBJECT.IS_IDENTICAL" ); 201 break; 202 case EJB_OBJECT_REMOVE: 203 s = new StringBuffer ( "EJB_OBJECT.REMOVE" ); 204 break; 205 case EJB_OBJECT_BUSINESS_METHOD: 206 s = new StringBuffer ( "EJB_OBJECT.BUSINESS_METHOD" ); 207 break; 208 } 209 s.append(':').append(methodName); 210 s.append(':').append(deploymentId); 211 s.append(':').append(primaryKey); 212 213 return s.toString(); 214 } 215 216 223 224 236 public void readExternal(ObjectInput in) 237 throws IOException , ClassNotFoundException 238 { 239 ClassNotFoundException result = null; 240 241 requestMethod = -1; 242 deploymentId = null; 243 deploymentCode = -1; 244 clientIdentity = null; 245 primaryKey = null; 246 methodClass = null; 247 methodName = null; 248 methodInstance = null; 249 250 requestMethod = in.readByte(); 251 try { 252 deploymentId = (String )in.readObject(); 253 } catch (ClassNotFoundException cnfe) { result = cnfe; } 254 deploymentCode = in.readShort(); 255 try { 256 clientIdentity = in.readObject(); 257 primaryKey = in.readObject(); 258 methodClass = (Class )in.readObject(); 259 } catch (ClassNotFoundException cnfe) { 260 if (result == null) result = cnfe; 261 } 262 methodName = in.readUTF(); 263 264 try { 265 readMethodParameters(in); 266 } catch (ClassNotFoundException cnfe) { 267 if (result == null) result = cnfe; 268 } 269 270 if (methodClass != null) 271 { 272 try 273 { 274 methodInstance = methodClass 275 .getDeclaredMethod(methodName, methodParamTypes); 276 } catch (NoSuchMethodException nsme) { 277 } 279 } 280 281 if (result != null) 282 throw result; 283 } 284 285 300 public void writeExternal(ObjectOutput out) throws IOException { 301 out.writeByte( requestMethod ); 302 303 if ( deploymentCode > 0 ) { 304 out.writeObject( null ); 305 } else { 306 out.writeObject( deploymentId ); 307 } 308 309 out.writeShort( deploymentCode ); 310 out.writeObject( clientIdentity ); 311 out.writeObject( primaryKey ); 312 316 out.writeObject( methodClass ); 317 out.writeUTF( methodName ); 318 319 writeMethodParameters( out, methodParamTypes, methodParameters ); 320 } 321 322 protected void writeMethodParameters(ObjectOutput out, Class [] types, Object [] args) throws IOException { 323 324 out.writeByte( types.length ); 325 326 for ( int i=0; i < types.length; i++ ){ 327 Class type = types[i]; 328 Object obj = args[i]; 329 330 if (type.isPrimitive()) { 331 if (type == Byte.TYPE){ 332 out.write(B); 333 byte bytevalue = ((Byte )obj).byteValue(); 334 out.writeByte(bytevalue); 335 336 } else if (type == Character.TYPE){ 337 out.write(C); 338 char charvalue = ((Character )obj).charValue(); 339 out.writeChar(charvalue); 340 341 } else if (type == Integer.TYPE){ 342 out.write(I); 343 int intvalue = ((Integer )obj).intValue(); 344 out.writeInt(intvalue); 345 346 } else if (type == Boolean.TYPE){ 347 out.write(Z); 348 boolean booleanvalue = ((Boolean )obj).booleanValue(); 349 out.writeBoolean(booleanvalue); 350 351 } else if (type == Long.TYPE){ 352 out.write(J); 353 long longvalue = ((Long )obj).longValue(); 354 out.writeLong(longvalue); 355 356 } else if (type == Float.TYPE){ 357 out.write(F); 358 float fvalue = ((Float )obj).floatValue(); 359 out.writeFloat(fvalue); 360 361 } else if (type == Double.TYPE){ 362 out.write(D); 363 double dvalue = ((Double )obj).doubleValue(); 364 out.writeDouble(dvalue); 365 366 } else if (type == Short.TYPE){ 367 out.write(S); 368 short shortvalue = ((Short )obj).shortValue(); 369 out.writeShort(shortvalue); 370 371 } else { 372 throw new IOException ("Unkown primitive type: "+type); 373 } 374 } else { 375 out.write(L); 376 out.writeObject( type ); 377 out.writeObject( obj ); 378 } 379 } 380 } 381 382 static final Class [] noArgsC = new Class [0]; 383 static final Object [] noArgsO = new Object [0]; 384 385 protected void readMethodParameters(ObjectInput in) throws IOException , ClassNotFoundException { 386 int length = in.read(); 387 388 if ( length < 1 ) { 389 methodParamTypes = noArgsC; 390 methodParameters = noArgsO; 391 return; 392 } 393 394 Class [] types = new Class [length]; 395 Object [] args = new Object [length]; 396 397 for ( int i=0; i < types.length; i++ ){ 398 Class clazz = null; 399 Object obj = null; 400 401 int type = in.read(); 402 403 switch (type) { 404 case B: 405 clazz = Byte.TYPE; 406 obj = new Byte ( in.readByte() ); 407 break; 408 409 case C: 410 clazz = Character.TYPE; 411 obj = new Character ( in.readChar() ); 412 break; 413 414 case I: 415 clazz = Integer.TYPE; 416 obj = new Integer ( in.readInt() ); 417 break; 418 419 case Z: 420 clazz = Boolean.TYPE; 421 obj = new Boolean ( in.readBoolean() ); 422 break; 423 424 case J: 425 clazz = Long.TYPE; 426 obj = new Long ( in.readLong() ); 427 break; 428 429 case F: 430 clazz = Float.TYPE; 431 obj = new Float ( in.readFloat() ); 432 break; 433 434 case D: 435 clazz = Double.TYPE; 436 obj = new Double ( in.readDouble() ); 437 break; 438 439 case S: 440 clazz = Short.TYPE; 441 obj = new Short ( in.readShort() ); 442 break; 443 444 case L: 445 clazz = (Class )in.readObject(); 446 obj = in.readObject(); 447 break; 448 default: 449 throw new IOException ("Unkown data type: "+type); 450 } 451 452 types[i] = clazz; 453 args[i] = obj; 454 } 455 456 methodParamTypes = types; 457 methodParameters = args; 458 } 459 460 private static final int I = 0; 461 private static final int B = 1; 462 private static final int J = 2; 463 private static final int F = 3; 464 private static final int D = 4; 465 private static final int S = 5; 466 private static final int C = 6; 467 private static final int Z = 7; 468 private static final int L = 8; 469 private static final int A = 9; 470 471 472 } 473 474 475 | Popular Tags |