KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > EJBRequest


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: EJBRequest.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.client;
46
47
48 import java.io.IOException JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52
53 /**
54  *
55  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
56  * @since 11/25/2001
57  */

58 public class EJBRequest implements Request {
59
60     private transient int requestMethod;
61     private transient int deploymentCode = 0;
62     private transient Object JavaDoc clientIdentity;
63     private transient Method JavaDoc methodInstance;
64
65     private transient Class JavaDoc methodClass;
66     private transient String JavaDoc methodName;
67     private transient Class JavaDoc[] methodParamTypes;
68     private transient Object JavaDoc[] methodParameters;
69     private transient String JavaDoc deploymentId;
70     private transient Object JavaDoc primaryKey;
71
72     
73     //-------------------------------------------------------//
74
// Enertprise Bean Types //
75
//-------------------------------------------------------//
76

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 JavaDoc getClientIdentity(){
100         return clientIdentity;
101     }
102
103     public Method JavaDoc getMethodInstance(){
104         return methodInstance;
105     }
106     
107     public Object JavaDoc[] getMethodParameters(){
108         return methodParameters;
109     }
110     
111     public String JavaDoc getDeploymentId(){
112         return deploymentId;
113     }
114     
115     public int getDeploymentCode(){
116         return deploymentCode;
117     }
118     
119     public Object JavaDoc getPrimaryKey(){
120         return primaryKey;
121     }
122
123
124     public Class JavaDoc getMethodClass(){
125         return methodClass;
126     }
127     
128     public String JavaDoc getMethodName(){
129         return methodName;
130     }
131     
132     public Class JavaDoc[] getMethodParamTypes(){
133         return methodParamTypes;
134     }
135     
136     //-------------
137

138     public void setRequestMethod(int requestMethod){
139         this.requestMethod = requestMethod;
140     }
141
142     public void setClientIdentity(Object JavaDoc clientIdentity){
143         this.clientIdentity = clientIdentity;
144     }
145
146     public void setMethodInstance(Method JavaDoc 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 JavaDoc[] methodParameters){
154         this.methodParameters = methodParameters;
155     }
156     
157     public void setDeploymentId(String JavaDoc deploymentId){
158         this.deploymentId = deploymentId;
159     }
160     
161     public void setDeploymentCode(int deploymentCode){
162         this.deploymentCode = deploymentCode;
163     }
164     
165     public void setPrimaryKey(Object JavaDoc primaryKey){
166         this.primaryKey = primaryKey;
167     }
168
169     public String JavaDoc toString(){
170         StringBuffer JavaDoc s = null;
171         switch (requestMethod) {
172             case EJB_HOME_GET_EJB_META_DATA:
173                 s = new StringBuffer JavaDoc( "EJB_HOME.GET_EJB_META_DATA" );
174                 break;
175             case EJB_HOME_GET_HOME_HANDLE:
176                 s = new StringBuffer JavaDoc( "EJB_HOME.GET_HOME_HANDLE" );
177                 break;
178             case EJB_HOME_REMOVE_BY_HANDLE:
179                 s = new StringBuffer JavaDoc( "EJB_HOME.REMOVE_BY_HANDLE" );
180                 break;
181             case EJB_HOME_REMOVE_BY_PKEY:
182                 s = new StringBuffer JavaDoc( "EJB_HOME.REMOVE_BY_PKEY" );
183                 break;
184             case EJB_HOME_FIND:
185                 s = new StringBuffer JavaDoc( "EJB_HOME.FIND" );
186                 break;
187             case EJB_HOME_CREATE:
188                 s = new StringBuffer JavaDoc( "EJB_HOME.CREATE" );
189                 break;
190             case EJB_OBJECT_GET_EJB_HOME:
191                 s = new StringBuffer JavaDoc( "EJB_OBJECT.GET_EJB_HOME" );
192                 break;
193             case EJB_OBJECT_GET_HANDLE:
194                 s = new StringBuffer JavaDoc( "EJB_OBJECT.GET_HANDLE" );
195                 break;
196             case EJB_OBJECT_GET_PRIMARY_KEY:
197                 s = new StringBuffer JavaDoc( "EJB_OBJECT.GET_PRIMARY_KEY" );
198                 break;
199             case EJB_OBJECT_IS_IDENTICAL:
200                 s = new StringBuffer JavaDoc( "EJB_OBJECT.IS_IDENTICAL" );
201                 break;
202             case EJB_OBJECT_REMOVE:
203                 s = new StringBuffer JavaDoc( "EJB_OBJECT.REMOVE" );
204                 break;
205             case EJB_OBJECT_BUSINESS_METHOD:
206                 s = new StringBuffer JavaDoc( "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     /*
217     When the Request externalizes itself, it will reset
218     the appropriate values so that this instance can be used
219     again.
220     
221     There will be one request instance for each handler
222     */

223
224     /**
225      * The object implements the readExternal method to restore its
226      * contents by calling the methods of DataInput for primitive
227      * types and readObject for objects, strings and arrays. The
228      * readExternal method must read the values in the same sequence
229      * and with the same types as were written by writeExternal.
230      *
231      * @param in the stream to read data from in order to restore the object
232      * @exception IOException if I/O errors occur
233      * @exception ClassNotFoundException If the class for an object being
234      * restored cannot be found.
235      */

236     public void readExternal(ObjectInput JavaDoc in)
237     throws IOException JavaDoc, ClassNotFoundException JavaDoc
238     {
239     ClassNotFoundException JavaDoc 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 JavaDoc)in.readObject();
253     } catch (ClassNotFoundException JavaDoc cnfe) { result = cnfe; }
254         deploymentCode = in.readShort();
255     try {
256         clientIdentity = in.readObject();
257         primaryKey = in.readObject();
258         methodClass = (Class JavaDoc)in.readObject();
259     } catch (ClassNotFoundException JavaDoc cnfe) {
260         if (result == null) result = cnfe;
261     }
262         methodName = in.readUTF();
263
264     try {
265         readMethodParameters(in);
266     } catch (ClassNotFoundException JavaDoc 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 JavaDoc nsme) {
277         //if (result == null) result = nsme;
278
}
279     }
280
281     if (result != null)
282         throw result;
283     }
284
285     /**
286      * The object implements the writeExternal method to save its contents
287      * by calling the methods of DataOutput for its primitive values or
288      * calling the writeObject method of ObjectOutput for objects, strings,
289      * and arrays.
290      *
291      * @serialData Overriding methods should use this tag to describe
292      * the data layout of this Externalizable object.
293      * List the sequence of element types and, if possible,
294      * relate the element to a public/protected field and/or
295      * method of this Externalizable class.
296      *
297      * @param out the stream to write the object to
298      * @exception IOException Includes any I/O exceptions that may occur
299      */

300     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
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         // The declaring class should always be the
313
// home or remote interface, depending on the
314
// request type.
315

316         out.writeObject( methodClass );
317         out.writeUTF( methodName );
318
319         writeMethodParameters( out, methodParamTypes, methodParameters );
320     }
321
322     protected void writeMethodParameters(ObjectOutput JavaDoc out, Class JavaDoc[] types, Object JavaDoc[] args) throws IOException JavaDoc{
323         
324         out.writeByte( types.length );
325   
326         for ( int i=0; i < types.length; i++ ){
327             Class JavaDoc type = types[i];
328             Object JavaDoc obj = args[i];
329
330             if (type.isPrimitive()) {
331                 if (type == Byte.TYPE){
332                     out.write(B);
333             byte bytevalue = ((Byte JavaDoc)obj).byteValue();
334             out.writeByte(bytevalue);
335                 
336                 } else if (type == Character.TYPE){
337                     out.write(C);
338             char charvalue = ((Character JavaDoc)obj).charValue();
339             out.writeChar(charvalue);
340                 
341                 } else if (type == Integer.TYPE){
342                     out.write(I);
343             int intvalue = ((Integer JavaDoc)obj).intValue();
344             out.writeInt(intvalue);
345                 
346                 } else if (type == Boolean.TYPE){
347                     out.write(Z);
348                     boolean booleanvalue = ((Boolean JavaDoc)obj).booleanValue();
349             out.writeBoolean(booleanvalue);
350                 
351                 } else if (type == Long.TYPE){
352                     out.write(J);
353             long longvalue = ((Long JavaDoc)obj).longValue();
354             out.writeLong(longvalue);
355                 
356                 } else if (type == Float.TYPE){
357                     out.write(F);
358             float fvalue = ((Float JavaDoc)obj).floatValue();
359             out.writeFloat(fvalue);
360                 
361                 } else if (type == Double.TYPE){
362                     out.write(D);
363             double dvalue = ((Double JavaDoc)obj).doubleValue();
364             out.writeDouble(dvalue);
365                 
366                 } else if (type == Short.TYPE){
367                     out.write(S);
368             short shortvalue = ((Short JavaDoc)obj).shortValue();
369             out.writeShort(shortvalue);
370                 
371                 } else {
372                     throw new IOException JavaDoc("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 JavaDoc[] noArgsC = new Class JavaDoc[0];
383     static final Object JavaDoc[] noArgsO = new Object JavaDoc[0];
384
385     protected void readMethodParameters(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc{
386         int length = in.read();
387         
388         if ( length < 1 ) {
389             methodParamTypes = noArgsC;
390             methodParameters = noArgsO;
391             return;
392         }
393
394         Class JavaDoc[] types = new Class JavaDoc[length];
395         Object JavaDoc[] args = new Object JavaDoc[length];
396
397         for ( int i=0; i < types.length; i++ ){
398             Class JavaDoc clazz = null;
399             Object JavaDoc obj = null;
400             
401             int type = in.read();
402             
403             switch (type) {
404                 case B:
405             clazz = Byte.TYPE;
406             obj = new Byte JavaDoc( in.readByte() );
407             break;
408
409                 case C:
410             clazz = Character.TYPE;
411             obj = new Character JavaDoc( in.readChar() );
412             break;
413
414                 case I:
415             clazz = Integer.TYPE;
416             obj = new Integer JavaDoc( in.readInt() );
417             break;
418
419                 case Z:
420             clazz = Boolean.TYPE;
421             obj = new Boolean JavaDoc( in.readBoolean() );
422             break;
423
424                 case J:
425             clazz = Long.TYPE;
426             obj = new Long JavaDoc( in.readLong() );
427             break;
428
429                 case F:
430             clazz = Float.TYPE;
431             obj = new Float JavaDoc( in.readFloat() );
432             break;
433
434                 case D:
435             clazz = Double.TYPE;
436             obj = new Double JavaDoc( in.readDouble() );
437             break;
438
439                 case S:
440             clazz = Short.TYPE;
441             obj = new Short JavaDoc( in.readShort() );
442             break;
443                     
444                 case L:
445                     clazz = (Class JavaDoc)in.readObject();
446                     obj = in.readObject();
447             break;
448                 default:
449                     throw new IOException JavaDoc("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