Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: mobile/protocol/Request.java


1   /** Java class "Request.java" generated from Poseidon for UML.
2    *  Poseidon for UML is developed by <A HREF="http://www.gentleware.com">Gentleware</A>.
3    *  Generated with <A HREF="http://jakarta.apache.org/velocity/">velocity</A> template engine.
4    */
5   
6   package mobile.protocol;
7   
8   
9   import mobile.jmsapi.MessageProperty;
10  import java.util.Vector;
11  /**
12   * <p>
13   * This class provides implementation of message sent to the proxy server. This
14   * message represents an action such as a call of a JMS or JNDI method.
15   *
16   * </p>
17   * @author Pawel Koziol
18   */
19  public class Request
20      extends Frame {
21  
22  ///////////////////////////////////////
23    // attributes
24  
25    /**
26     * <p>
27         * Id of the RMS record containing this Request. Some records are stored in the
28     * RMS to ensure reliability of message sending.
29     * </p>
30     */
31  
32    public int rms_id = 0;
33  
34    ///////////////////////////////////////
35    // operations
36  
37  /** logs a debuging message */
38    private void log(String msg){
39      System.out.println("MRM: " + msg);
40    }
41    /**
42     * <p>
43     * Converts an int value into 4 bytes and puts them into the tab beginning
44     * at the given offset
45     * </p>
46     */
47  
48    private void intToByte(int anInt, byte[] tab, int offset) {
49      int i = 0;
50  
51      for (i = 0; i < INT_LEN; i++) {
52        tab[offset + i] = (byte) anInt;
53        anInt /= 256;
54      }
55    }
56  
57    /**
58     * <p>
59     * Converts a long value into 8 bytes and puts them into the tab beginning
60     * at the given offset
61     * </p>
62     */
63  
64    private void longToByte(long aLong, byte[] tab, int offset) {
65      int i = 0;
66      for (i = 0; i < LONG_LEN; i++) {
67        tab[offset + i] = (byte) aLong;
68        aLong /= 256;
69      }
70    }
71  
72    /**
73     * <p>
74     * Copies len bytes from source to dest beginning from offset in the dest and
75     * from the begining in the source
76     * </p><p>
77     *
78     * </p>
79     */
80  
81    private void copyBytes(byte[] source, byte[] dest, int offset, int len) {
82      int i = 0;
83      for (i = 0; i < len; i++) {
84        dest[offset + i] = source[i];
85      }
86    }
87  
88    /**
89     * <p>
90     * Converts this message to a byte array. All objects associated with this
91     * request are converted to their byte representation.
92     * </p><p>
93     *
94     * @return A byte array containing the objects associated with this request.
95     * </p>
96     */
97  
98    public byte[] serialize() {
99      byte[] ret = null; /* return value */
100     int len = 0; /* length */
101     int id = 0; /*for ids stored in requests */
102     boolean mode = false;
103     try{
104       switch (code) {
105         //case LOOKUP_QUEUE_CONNECTION_FACTORY:
106         //case LOOKUP_TOPIC_CONNECTION_FACTORY:
107         //case LOOKUP_QUEUE:
108         case LOOKUP:
109           len = ( (String) getData()[0]).length(); /*String name */
110           ret = new byte[INT_LEN + len];
111           intToByte(len, ret, 0);
112           copyBytes( ( (String) getData()[0]).getBytes(), ret, INT_LEN, len);
113           break;
114         case CREATE_QUEUE_CONNECTION:
115         case CREATE_TOPIC_CONNECTION:
116           ret = new byte[INT_LEN];
117           id = ( (Integer) getData()[0]).intValue(); /*ConnectionFactory.id*/
118           intToByte(id, ret, 0);
119           break;
120         case CREATE_QUEUE_SESSION:
121         case CREATE_TOPIC_SESSION:
122           ret = new byte[2 * INT_LEN + 1];
123           id = ( (Integer) getData()[0]).intValue(); /*Connection.id */
124           intToByte(id, ret, 0);
125           mode = ( (Boolean) getData()[1]).booleanValue();
126               /*transacted:boolean*/
127           if (mode)
128             ret[INT_LEN] = 1;
129           else
130             ret[INT_LEN] = 0;
131           id = ( (Integer) getData()[2]).intValue(); /*acknowledgeMode */
132           intToByte(id, ret, INT_LEN + 1);
133           break;
134         case CREATE_QUEUE_SENDER:
135         case CREATE_TOPIC_PUBLISHER:
136         case CREATE_QUEUE_RECEIVER:
137         case CREATE_TOPIC_SUBSCRIBER:
138         case CREATE_DURABLE_TOPIC_SUBSCRIBER:
139           ret = new byte[2 * INT_LEN];
140           id = ( (Integer) getData()[0]).intValue(); /*Session.id*/
141           intToByte(id, ret, 0);
142           id = ( (Integer) getData()[1]).intValue(); /*Destination.id*/
143           intToByte(id, ret, INT_LEN);
144           break;
145         case SEND_BYTES_MESSAGE:
146         case SEND_TEXT_MESSAGE:
147         case PUBLISH_BYTES_MESSAGE:
148         case PUBLISH_TEXT_MESSAGE:
149 
150           /*count the byte array length */
151           len = 5 * INT_LEN; /* 3 ids + byte[] length + String length */
152           log("moj log:" + new Integer(code).toString());
153           if (code == SEND_BYTES_MESSAGE | code == PUBLISH_BYTES_MESSAGE){
154             if(getData()[3] != null)
155             len += ( (byte[]) getData()[3]).length;
156             /* byte[] (message.readBytes())*/
157           }
158           else{
159           log("serialize przed string");
160             /*code == SEND_TEXT_MESSAGE | code == PUBLISH_TEXT_MESSAGE*/
161             if (getData()[3] != null)
162             len += ( (String) getData()[3]).length();
163             /* string (message.getText())*/
164           }
165           log("serialize po string");
166             if (getData()[4] != null)
167           len += ( (String) getData()[4]).length(); /* string (JMSMessageId)*/
168 
169           len += LONG_LEN; /* long (JMSTimeStamp) */
170           len += INT_LEN; /*String length */
171           if (getData()[6] != null)
172           len += ( (String) getData()[6]).length();
173               /* string (JMSCorrelation)*/
174           len += 3 * INT_LEN; /* Destination id ( JMSReplyTo) */
175           /* Destination id (JMSDestination) */
176           /* int (JMSDeliveryMode) */
177           len += 1; /*boolean (JMSRedelivered) */
178           len += INT_LEN; /*String length */
179           if (getData()[11] != null)
180           len += ( (String) getData()[11]).length(); /* string (JMSType)*/
181           len += LONG_LEN; /*long (JMSExpiration) */
182           len += INT_LEN; /*int (JMSPriority) */
183 
184           //count properties
185           Vector mesgProps = (Vector) getData()[14];
186           len += INT_LEN;
187           for(int i = 0; i < mesgProps.size(); i++){
188             len += INT_LEN;
189             len += mesgProps.elementAt(i).toString().length();
190           }
191 
192           /*serialize*/
193           ret = new byte[len];
194           for (int i = 0; i < 3; i++) { /*copy first 3 ids */
195             if (getData()[i] != null)
196               id = ( (Integer) getData()[i]).intValue();
197               else id = 0;
198             intToByte(id, ret, i * INT_LEN);
199           }
200           len = 3 * INT_LEN; /* offset in ret array */
201           int lenAkt = 0; /* length of currently copied array*/
202 
203           if (code == SEND_BYTES_MESSAGE | code == PUBLISH_BYTES_MESSAGE) {
204             if (getData()[3] != null){
205             lenAkt = ( (byte[]) getData()[3]).length;
206             /* byte[] (message.readBytes())*/
207             intToByte(lenAkt, ret, len); /*write length */
208             len += INT_LEN;
209             copyBytes( (byte[]) getData()[3], ret, len, lenAkt);
210                 /*write bytes */
211             len += lenAkt;
212           }
213           else{
214             lenAkt = 0;
215             intToByte(lenAkt, ret, len); /*write length */
216             len += INT_LEN;
217             len += lenAkt;
218           }
219 
220           }
221           else { /*code == SEND_TEXT_MESSAGE | code == PUBLISH_TEXT_MESSAGE*/
222             if(getData()[3] != null){
223               lenAkt = ( (String) getData()[3]).length();
224               /* String (message.getText())*/
225               intToByte(lenAkt, ret, len); /*write length */
226               len += INT_LEN;
227               copyBytes( ( (String) getData()[3]).getBytes(), ret, len, lenAkt);
228               /*write bytes
229                */
230               len += lenAkt;
231             }
232             else{
233            lenAkt = 0;
234            intToByte(lenAkt, ret, len); /*write length */
235            len += INT_LEN;
236            len += lenAkt;
237          }
238 
239 
240           }
241 
242           if(getData()[4] != null){
243              lenAkt = ( (String) getData()[4]).length();
244                 /* string (JMSMessageId)*/
245              intToByte(lenAkt, ret, len); /*write length */
246              len += INT_LEN;
247              copyBytes( ( (String) getData()[4]).getBytes(), ret, len, lenAkt);
248              /*write bytes
249               */
250              len += lenAkt;
251            }
252            else{
253           lenAkt = 0;
254           intToByte(lenAkt, ret, len); /*write length */
255           len += INT_LEN;
256           len += lenAkt;
257         }
258 
259         log("serizalize po string");
260           /*write long (JMSTimeStamp)*/
261           longToByte( ( (Long) getData()[5]).longValue(), ret, len);
262           len += LONG_LEN;
263 
264           /*write string (JMSCorrelation)*/
265           if(getData()[6] != null){
266              lenAkt = ( (String) getData()[6]).length();
267              intToByte(lenAkt, ret, len); /*write length */
268              len += INT_LEN;
269              copyBytes( ( (String) getData()[6]).getBytes(), ret, len, lenAkt);
270              /*write bytes
271               */
272              len += lenAkt;
273            }
274            else{
275           lenAkt = 0;
276           intToByte(lenAkt, ret, len); /*write length */
277           len += INT_LEN;
278           len += lenAkt;
279         }
280 
281           /*write int (JMSReplyTo)*/
282           /*FIXME*/
283           if(getData()[7] != null){
284             intToByte( ( (Integer) getData()[7]).intValue(), ret, len);
285           }
286            len += INT_LEN;
287           /*write int (JMSDestination) */
288           /*FIXME*/
289           if (getData()[8] != null) {
290             intToByte( ( (Integer) getData()[8]).intValue(), ret, len);
291           }
292           len += INT_LEN;
293           /*write int ((JMSDeliveryMode)) */
294           intToByte( ( (Integer) getData()[9]).intValue(), ret, len);
295           len += INT_LEN;
296           /*write boolean */
297            if(getData()[10] != null){
298              mode = ( (Boolean) getData()[10]).booleanValue();
299                  /*JMSRedelivered*/
300              if (mode)
301                ret[len] = 1;
302              else
303                ret[len] = 0;
304            }
305            else ret[len] = 0;
306           len++;
307           /*write string (JMSType)*/
308           if(getData()[11] != null){
309                lenAkt = ( (String) getData()[11]).length();
310                intToByte(lenAkt, ret, len); /*write length */
311                len += INT_LEN;
312                copyBytes( ( (String) getData()[11]).getBytes(), ret, len, lenAkt);
313                /*write bytes
314                 */
315                len += lenAkt;
316              }
317              else{
318             lenAkt = 0;
319             intToByte(lenAkt, ret, len); /*write length */
320             len += INT_LEN;
321             len += lenAkt;
322           }
323 
324 
325           /*write long (JMSExpiration)*/
326           longToByte( ( (Long) getData()[12]).longValue(), ret, len);
327           len += LONG_LEN;
328           /*write int (JMSPriority)*/
329           intToByte( ( (Integer) getData()[13]).intValue(), ret, len);
330           len += INT_LEN;
331 
332           /*write properties */
333           lenAkt = mesgProps.size();
334           intToByte(lenAkt, ret, len); /*write number of properties */
335           len += INT_LEN;
336 
337           for (int i = 0; i < mesgProps.size(); i++) {
338 
339             if (mesgProps.elementAt(i) != null && mesgProps.elementAt(i).toString() != null) {
340               lenAkt = mesgProps.elementAt(i).toString().length();
341               intToByte(lenAkt, ret, len); /*write String length*/
342               len += INT_LEN;
343               copyBytes(mesgProps.elementAt(i).toString().getBytes(), ret, len, lenAkt);
344               /*write bytes
345                */
346               len += lenAkt;
347             }
348             else {
349               lenAkt = 0;
350               intToByte(lenAkt, ret, len); /*write length */
351               len += INT_LEN;
352               len += lenAkt;
353             }
354           }
355 
356 
357 
358           break;
359         case RECEIVE_MESSAGE_FROM_QUEUE:
360         case RECEIVE_MESSAGE_FROM_TOPIC:
361           ret = new byte[3 * INT_LEN];
362           id = ( (Integer) getData()[0]).intValue(); /*Consumer.id*/
363           intToByte(id, ret, 0);
364           id = ( (Integer) getData()[1]).intValue(); /*Destination.id*/
365           intToByte(id, ret, INT_LEN);
366           id = ( (Integer) getData()[2]).intValue(); /*Session.id*/
367           intToByte(id, ret, 2 * INT_LEN);
368           break;
369         case STOP_CONNECTION:
370         case START_CONNECTION:
371         case CLOSE_CONNECTION:
372         case COMMIT:
373         case ROLLBACK:
374         case CLOSE_MESSAGE_PRODUCER:
375         case CLOSE_MESSAGE_CONSUMER:
376         case ACKNOWLEDGE_MESSAGE:
377         case CREATE_TEMPORARY_QUEUE:
378         case CREATE_TEMPORARY_TOPIC:
379         case DELETE_TEMPORARY_QUEUE:
380         case DELETE_TEMPORARY_TOPIC:
381           ret = new byte[INT_LEN];
382           id = ( (Integer) getData()[0]).intValue(); /*id*/
383           intToByte(id, ret, 0);
384           break;
385         default:
386           log("unsupported request code");
387           ret = null;
388           break;
389       } // switch
390     }
391     catch(Exception e){
392       log("wyjatek przy serializacji " + e.getMessage());
393       e.printStackTrace();
394     }
395 
396     return ret;
397 
398   } // end serilaize
399 
400   /**
401    * <p>
402    * Converts this message to a byte array. All objects associated with this
403    * request are converted to their byte representation. All request
404    * attributes are also stored in the array.
405    * <P>
406    * It is used when the request is stored in the record store.
407    * </p>
408    * </p><p>
409    *
410    * @return A byte array containing the objects associated with this request.
411    * </p>
412    */
413 
414   /*the byte[] contains:
415        - 4 bytes - code
416        - 4 bytes - owner
417        - 4 bytes - id
418        - following bytes - serialized request
419    */
420   public byte[] rmsSerialize() {
421     byte[] body = this.serialize();
422     int len = body.length + 3 * INT_LEN;
423     byte[] ret = new byte[len];
424     intToByte(this.code, ret, 0);
425     intToByte(this.owner, ret, INT_LEN);
426     intToByte(this.id, ret, 2 * INT_LEN);
427     copyBytes(body, ret, 3 * INT_LEN, body.length);
428     return ret;
429   }
430 
431   /**
432    * <p>
433        * Copies len bytes from source to dest beginning from offset in the source and
434    * from the begining in the dest
435    * </p><p>
436    *
437    * </p>
438    */
439 
440   private void copyBytesRMS(byte[] source, byte[] dest, int offset, int len) {
441     int i = 0;
442     for (i = 0; i < len; i++) {
443       dest[i] = source[i + offset];
444     }
445   }
446 
447   /**
448    * <p>
449        * Converts four consecutive bytes of tab starting at offset into an int value
450    * </p><p>
451    *
452    * @param tab ...
453    * </p><p>
454    * @return a ...
455    * </p>
456    */
457 
458   private int byteToInt(byte[] tab, int offset) {
459     int i = 0, ret = 0, exp = 1, mul = 0;
460     for (i = 0; i < INT_LEN; i++) {
461       if (tab[offset + i] < 0)
462         mul = (256 + tab[offset + i]);
463       else
464         mul = tab[offset + i];
465       ret += mul * exp;
466       exp *= 256;
467     }
468     return ret;
469   }
470 
471   /**
472    * <p>
473        * Converts eight consecutive bytes of tab starting at offset into a long value
474    * </p><p>
475    *
476    * @param arr ...
477    * </p><p>
478    * @return a ...
479    * </p>
480    */
481 
482   private long byteToLong(byte[] tab, int offset) {
483     int i = 0;
484     long ret = 0, exp = 1, mul = 0;
485     for (i = 0; i < LONG_LEN; i++) {
486       if (tab[offset + i] < 0)
487         mul = (256 + tab[offset + i]);
488       else
489         mul = tab[offset + i];
490       ret += mul * exp;
491 
492       //ret += (long) tab[offset + i] * exp;
493       exp *= 256;
494     }
495     return ret;
496   }
497 
498   /**
499    * <p>
500    * Converts a byte from tab starting at offset into an Boolean object
501    * </p><p>
502    *
503    * @param tab
504    * </p><p>
505    * @return a Boolean object
506    * </p>
507    */
508 
509   private Boolean byteToBool(byte[] tab, int offset) {
510     if (tab[offset] == 1)
511       return new Boolean(true);
512     else
513       return new Boolean(false);
514   }
515 
516   /**
517    *<p>
518    *A constructor used to recreate the request from the byte array form
519    * used, when the request is stored in the record store
520    * @param msg The byte array containing the serialized form of this
521    * request
522    * </p>
523    */
524 
525   Request(byte[] msg) {
526     Object[] objects = null;
527     int len = 0, num = 0;
528     byte[] byteArr;
529     int offset = 0; /*offset in the msg for the first 3 ids*/
530 
531     this.code = byteToInt(msg, offset);
532     offset += INT_LEN;
533     this.owner = byteToInt(msg, offset);
534     offset += INT_LEN;
535     this.id = byteToInt(msg, offset);
536     offset += INT_LEN;
537 
538     objects = new Object[15];
539 
540     for (int i = 0; i < 3; i++) { /*copy first 3 ids */
541       num = byteToInt(msg, offset + INT_LEN * i);
542       objects[i] = new Integer(num);
543     }
544     len = offset + 3 * INT_LEN; /* offset in ret array */
545     int lenAkt = 0; /* length of currently copied array*/
546     /*read byte[] or string length*/
547     lenAkt = byteToInt(msg, len);
548     len += INT_LEN;
549     if (code == SEND_BYTES_MESSAGE | code == PUBLISH_BYTES_MESSAGE) {
550       byteArr = new byte[lenAkt];
551       copyBytesRMS(msg, byteArr, len, lenAkt);
552       objects[3] = byteArr;
553     }
554     else { /*code == SEND_TEXT_MESSAGE | code == PUBLISH_TEXT_MESSAGE*/
555       objects[3] = new String(msg, len, lenAkt);
556     }
557     len += lenAkt;
558     /*read String (JMSMessageId) */
559     lenAkt = byteToInt(msg, len);
560     len += INT_LEN;
561     objects[4] = new String(msg, len, lenAkt);
562     len += lenAkt;
563     /*read long (JMSTimeStamp) */
564     objects[5] = new Long(byteToLong(msg, len));
565     len += LONG_LEN;
566     /*read String (JMSCorrelation)*/
567     lenAkt = byteToInt(msg, len);
568     len += INT_LEN;
569     objects[6] = new String(msg, len, lenAkt);
570     len += lenAkt;
571     /*read int Destination (JMSReplyTo)*/
572     num = byteToInt(msg, len);
573     objects[7] = new Integer(num);
574     len += INT_LEN;
575     /* read int Destination (JMSDestination) */
576     num = byteToInt(msg, len);
577     objects[8] = new Integer(num);
578     len += INT_LEN;
579     /* read int Destination (JMSDeliveryMode) */
580     num = byteToInt(msg, len);
581     objects[9] = new Integer(num);
582     len += INT_LEN;
583     /*read boolean (JMSRedelivered) */
584     objects[10] = byteToBool(msg, len);
585     len++;
586     /*read String (JMSType) */
587     lenAkt = byteToInt(msg, len);
588     len += INT_LEN;
589     objects[11] = new String(msg, len, lenAkt);
590     len += lenAkt;
591     /*read long (JMSExpiration)*/
592     objects[12] = new Long(byteToLong(msg, len));
593     len += LONG_LEN;
594     /*read int int (JMSPriority)*/
595     num = byteToInt(msg, len);
596     objects[13] = new Integer(num);
597     len += INT_LEN;
598     setData(objects);
599 
600     /*read properties*/
601 
602     int propNum = (new Integer(byteToInt(msg, len))).intValue();
603     Vector props = new Vector();
604     len += INT_LEN;
605     for(int i = 0; i < propNum; i++){
606       lenAkt = byteToInt(msg, len);
607       len += INT_LEN;
608       props.addElement(new String(msg, len, lenAkt));
609       len += lenAkt;
610 
611     }
612 
613 
614   }
615 
616   /**
617    * <p>
618    * A default constructor for this class.
619    * </p>
620    */
621   public Request() {
622     rms_id = 0;
623     //id = getCurrentId();
624   }
625 } // end Request