KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > xmpp > XmppWireFormat


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.xmpp;
19
20 import org.apache.activemq.util.ByteArrayInputStream;
21 import org.apache.activemq.util.ByteArrayOutputStream;
22 import org.apache.activemq.util.ByteSequence;
23 import org.apache.activemq.wireformat.WireFormat;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.io.DataInput JavaDoc;
28 import java.io.DataInputStream JavaDoc;
29 import java.io.DataOutput JavaDoc;
30 import java.io.DataOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34  * A wire format which uses XMPP format of messages
35  *
36  * @version $Revision: 470716 $
37  */

38 public class XmppWireFormat implements WireFormat {
39     private static final Log log = LogFactory.getLog(XmppWireFormat.class);
40
41     private int version = 1;
42
43     public WireFormat copy() {
44         return new XmppWireFormat();
45     }
46
47     /*
48     public Packet readPacket(DataInput in) throws IOException {
49         return null;
50     }
51
52     public Packet readPacket(int firstByte, DataInput in) throws IOException {
53         return null;
54     }
55
56     public Packet writePacket(Packet packet, DataOutput out) throws IOException, JMSException {
57         switch (packet.getPacketType()) {
58             case Packet.ACTIVEMQ_MESSAGE:
59                 writeMessage((ActiveMQMessage) packet, "", out);
60                 break;
61
62             case Packet.ACTIVEMQ_TEXT_MESSAGE:
63                 writeTextMessage((ActiveMQTextMessage) packet, out);
64                 break;
65
66             case Packet.ACTIVEMQ_BYTES_MESSAGE:
67                 writeBytesMessage((ActiveMQBytesMessage) packet, out);
68                 break;
69
70             case Packet.ACTIVEMQ_OBJECT_MESSAGE:
71                 writeObjectMessage((ActiveMQObjectMessage) packet, out);
72                 break;
73
74             case Packet.ACTIVEMQ_MAP_MESSAGE:
75             case Packet.ACTIVEMQ_STREAM_MESSAGE:
76
77
78             case Packet.ACTIVEMQ_BROKER_INFO:
79             case Packet.ACTIVEMQ_CONNECTION_INFO:
80             case Packet.ACTIVEMQ_MSG_ACK:
81             case Packet.CONSUMER_INFO:
82             case Packet.DURABLE_UNSUBSCRIBE:
83             case Packet.INT_RESPONSE_RECEIPT_INFO:
84             case Packet.PRODUCER_INFO:
85             case Packet.RECEIPT_INFO:
86             case Packet.RESPONSE_RECEIPT_INFO:
87             case Packet.SESSION_INFO:
88             case Packet.TRANSACTION_INFO:
89             case Packet.XA_TRANSACTION_INFO:
90             default:
91                 log.warn("Ignoring message type: " + packet.getPacketType() + " packet: " + packet);
92         }
93         return null;
94     }
95 */

96
97 // /**
98
// * Can this wireformat process packets of this version
99
// * @param version the version number to test
100
// * @return true if can accept the version
101
// */
102
// public boolean canProcessWireFormatVersion(int version){
103
// return true;
104
// }
105
//
106
// /**
107
// * @return the current version of this wire format
108
// */
109
// public int getCurrentWireFormatVersion(){
110
// return 1;
111
// }
112
//
113
// // Implementation methods
114
// //-------------------------------------------------------------------------
115
// protected void writeObjectMessage(ActiveMQObjectMessage message, DataOutput out) throws JMSException, IOException {
116
// Serializable object = message.getObject();
117
// String text = (object != null) ? object.toString() : "";
118
// writeMessage(message, text, out);
119
// }
120
//
121
// protected void writeTextMessage(ActiveMQTextMessage message, DataOutput out) throws JMSException, IOException {
122
// writeMessage(message, message.getText(), out);
123
// }
124
//
125
// protected void writeBytesMessage(ActiveMQBytesMessage message, DataOutput out) throws IOException {
126
// ByteArray data = message.getBodyAsBytes();
127
// String text = encodeBinary(data.getBuf(),data.getOffset(),data.getLength());
128
// writeMessage(message, text, out);
129
// }
130
//
131
// protected void writeMessage(ActiveMQMessage message, String body, DataOutput out) throws IOException {
132
// String type = getXmppType(message);
133
//
134
// StringBuffer buffer = new StringBuffer("<");
135
// buffer.append(type);
136
// buffer.append(" to='");
137
// buffer.append(message.getJMSDestination().toString());
138
// buffer.append("' from='");
139
// buffer.append(message.getJMSReplyTo().toString());
140
// String messageID = message.getJMSMessageID();
141
// if (messageID != null) {
142
// buffer.append("' id='");
143
// buffer.append(messageID);
144
// }
145
//
146
// HashMap properties = message.getProperties();
147
// if (properties != null) {
148
// for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
149
// Map.Entry entry = (Map.Entry) iter.next();
150
// Object key = entry.getKey();
151
// Object value = entry.getValue();
152
// if (value != null) {
153
// buffer.append("' ");
154
// buffer.append(key.toString());
155
// buffer.append("='");
156
// buffer.append(value.toString());
157
// }
158
// }
159
// }
160
//
161
// buffer.append("'>");
162
//
163
// String id = message.getJMSCorrelationID();
164
// if (id != null) {
165
// buffer.append("<thread>");
166
// buffer.append(id);
167
// buffer.append("</thread>");
168
// }
169
// buffer.append(body);
170
// buffer.append("</");
171
// buffer.append(type);
172
// buffer.append(">");
173
//
174
// out.write(buffer.toString().getBytes());
175
// }
176
//
177
// protected String encodeBinary(byte[] data,int offset,int length) {
178
// // TODO
179
// throw new RuntimeException("Not implemented yet!");
180
// }
181
//
182
// protected String getXmppType(ActiveMQMessage message) {
183
// String type = message.getJMSType();
184
// if (type == null) {
185
// type = "message";
186
// }
187
// return type;
188
// }
189

190
191     public ByteSequence marshal(Object JavaDoc command) throws IOException JavaDoc {
192         ByteArrayOutputStream baos = new ByteArrayOutputStream();
193         DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(baos);
194         marshal(command, dos);
195         dos.close();
196         return baos.toByteSequence();
197     }
198
199     public Object JavaDoc unmarshal(ByteSequence packet) throws IOException JavaDoc {
200         ByteArrayInputStream stream = new ByteArrayInputStream(packet);
201         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(stream);
202         return unmarshal(dis);
203     }
204
205     public void marshal(Object JavaDoc object, DataOutput JavaDoc dataOutput) throws IOException JavaDoc {
206         /** TODO */
207     }
208
209     public Object JavaDoc unmarshal(DataInput JavaDoc dataInput) throws IOException JavaDoc {
210         return null; /** TODO */
211     }
212
213
214     public int getVersion() {
215         return version;
216     }
217
218     public void setVersion(int version) {
219         this.version = version;
220     }
221 }
222
Popular Tags