KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > stomp > FrameTranslator


1 package org.apache.activemq.transport.stomp;
2
3 import org.apache.activemq.command.ActiveMQMessage;
4 import org.apache.activemq.command.ActiveMQDestination;
5
6 import javax.jms.JMSException JavaDoc;
7 import javax.jms.Destination JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 /**
13  * Implementations of this interface are used to map back and forth from Stomp to ActiveMQ.
14  * There are several standard mappings which are semantically the same, the inner class,
15  * Helper, provides functions to copy those properties from one to the other
16  */

17 public interface FrameTranslator
18 {
19     public ActiveMQMessage convertFrame(StompFrame frame) throws JMSException JavaDoc, ProtocolException;
20
21     public StompFrame convertMessage(ActiveMQMessage message) throws IOException JavaDoc, JMSException JavaDoc;
22
23     public String JavaDoc convertDestination(Destination d);
24
25     public ActiveMQDestination convertDestination(String JavaDoc name) throws ProtocolException;
26
27     /**
28      * Helper class which holds commonly needed functions used when implementing
29      * FrameTranslators
30      */

31     public final static class Helper
32     {
33         public static void copyStandardHeadersFromMessageToFrame(ActiveMQMessage message,
34                                                                  StompFrame command,
35                                                                  FrameTranslator ft)
36                 throws IOException JavaDoc
37         {
38             final Map JavaDoc headers = command.getHeaders();
39             headers.put(Stomp.Headers.Message.DESTINATION, ft.convertDestination(message.getDestination()));
40             headers.put(Stomp.Headers.Message.MESSAGE_ID, message.getJMSMessageID());
41
42             if (message.getJMSCorrelationID() != null) {
43                 headers.put(Stomp.Headers.Message.CORRELATION_ID, message.getJMSCorrelationID());
44             }
45             headers.put(Stomp.Headers.Message.EXPIRATION_TIME, ""+message.getJMSExpiration());
46
47             if (message.getJMSRedelivered()) {
48                 headers.put(Stomp.Headers.Message.REDELIVERED, "true");
49             }
50             headers.put(Stomp.Headers.Message.PRORITY, ""+message.getJMSPriority());
51
52             if (message.getJMSReplyTo() != null) {
53                 headers.put(Stomp.Headers.Message.REPLY_TO, ft.convertDestination(message.getJMSReplyTo()));
54             }
55             headers.put(Stomp.Headers.Message.TIMESTAMP, ""+message.getJMSTimestamp());
56
57             if (message.getJMSType() != null) {
58                 headers.put(Stomp.Headers.Message.TYPE, message.getJMSType());
59             }
60
61             // now lets add all the message headers
62
final Map JavaDoc properties = message.getProperties();
63             if (properties != null) {
64                 headers.putAll(properties);
65             }
66         }
67
68         public static void copyStandardHeadersFromFrameToMessage(StompFrame command,
69                                                                  ActiveMQMessage msg,
70                                                                  FrameTranslator ft)
71                 throws ProtocolException, JMSException JavaDoc
72         {
73             final Map JavaDoc headers = new HashMap JavaDoc(command.getHeaders());
74             final String JavaDoc destination = (String JavaDoc) headers.remove(Stomp.Headers.Send.DESTINATION);
75             msg.setDestination( ft.convertDestination(destination));
76
77             // the standard JMS headers
78
msg.setJMSCorrelationID((String JavaDoc) headers.remove(Stomp.Headers.Send.CORRELATION_ID));
79
80             Object JavaDoc o = headers.remove(Stomp.Headers.Send.EXPIRATION_TIME);
81             if (o != null) {
82                 msg.setJMSExpiration(Long.parseLong((String JavaDoc) o));
83             }
84
85             o = headers.remove(Stomp.Headers.Send.PRIORITY);
86             if (o != null) {
87                 msg.setJMSPriority(Integer.parseInt((String JavaDoc) o));
88             }
89
90             o = headers.remove(Stomp.Headers.Send.TYPE);
91             if (o != null) {
92                 msg.setJMSType((String JavaDoc) o);
93             }
94
95             o = headers.remove(Stomp.Headers.Send.REPLY_TO);
96             if (o != null) {
97                 msg.setJMSReplyTo(ft.convertDestination((String JavaDoc) o));
98             }
99
100             o = headers.remove(Stomp.Headers.Send.PERSISTENT);
101             if (o != null) {
102                 msg.setPersistent("true".equals(o));
103             }
104
105             // now the general headers
106
msg.setProperties(headers);
107         }
108     }
109 }
110
Popular Tags