Source code: org/activemq/transport/openwire/OpenWireFormatSupport.java
1 /**
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.transport.openwire;
19
20 import org.activemq.io.AbstractWireFormat;
21 import org.activemq.io.WireFormat;
22 import org.activemq.io.impl.AbstractDefaultWireFormat;
23 import org.activemq.io.impl.AbstractPacketMarshaller;
24 import org.activemq.message.Packet;
25 import org.activemq.transport.openwire.io.OpenWireFormat;
26
27 import javax.jms.JMSException;
28 import java.io.DataInput;
29 import java.io.DataOutput;
30 import java.io.IOException;
31
32 /**
33 * Base class for the code generated {@link OpenWireFormat}
34 *
35 * @version $Revision: 1.1 $
36 */
37 public abstract class OpenWireFormatSupport extends AbstractWireFormat {
38
39 public Packet readPacket(DataInput dataIn) throws IOException {
40 int type = -1;
41 while ((type = dataIn.readByte()) == 0);
42
43 if (type == -1){
44 throw new IOException("InputStream now closed");
45 }
46 return readPacket(type, dataIn);
47 }
48
49 public boolean canProcessWireFormatVersion(int version) {
50 return version == getCurrentWireFormatVersion();
51 }
52
53 public WireFormat copy() {
54 return new OpenWireFormat();
55 }
56
57 public int getCurrentWireFormatVersion() {
58 return AbstractDefaultWireFormat.WIRE_FORMAT_VERSION;
59 }
60
61 public Packet writePacket(Packet packet, DataOutput out) throws IOException, JMSException {
62 AbstractPacketMarshaller marshaller = getMarshaller(packet);
63 if (marshaller == null) {
64 throw new JMSException("No marshaller for packet type: " + packet.getPacketType() + " value: " + packet);
65 }
66 out.writeByte(marshaller.getPacketType());
67 marshaller.writePacket(packet, out);
68 return packet;
69 }
70
71
72 protected Packet readPacket(DataInput dataIn, AbstractPacketMarshaller marshaler) throws IOException {
73 Packet packet = marshaler.createPacket();
74 marshaler.buildPacket(packet, dataIn);
75 return packet;
76 }
77
78 protected abstract AbstractPacketMarshaller getMarshaller(Packet packet);
79
80
81 }