From 0a5d8524f72ee4389ffee2f34006cbffd331546f Mon Sep 17 00:00:00 2001 From: Arnaud Simon Date: Mon, 13 Aug 2007 13:59:48 +0000 Subject: added constructors git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@565354 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/qpidity/jms/MessageActor.java | 2 +- .../java/org/apache/qpidity/jms/SessionImpl.java | 2 +- .../qpidity/jms/message/BytesMessageImpl.java | 20 + .../apache/qpidity/jms/message/MapMessageImpl.java | 21 + .../apache/qpidity/jms/message/MessageFactory.java | 53 +- .../apache/qpidity/jms/message/MessageImpl.java | 48 +- .../qpidity/jms/message/ObjectMessageImpl.java | 26 +- .../apache/qpidity/jms/message/QpidMessage.java | 26 +- .../qpidity/jms/message/StreamMessaeImpl.java | 1091 ------------------- .../qpidity/jms/message/StreamMessageImpl.java | 1112 ++++++++++++++++++++ .../qpidity/jms/message/TextMessageImpl.java | 21 +- 11 files changed, 1316 insertions(+), 1106 deletions(-) delete mode 100644 java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessaeImpl.java create mode 100644 java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessageImpl.java (limited to 'java/client') diff --git a/java/client/src/main/java/org/apache/qpidity/jms/MessageActor.java b/java/client/src/main/java/org/apache/qpidity/jms/MessageActor.java index 6363108d45..fe3ead4155 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/MessageActor.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/MessageActor.java @@ -157,7 +157,7 @@ public abstract class MessageActor * * @return This Actor's Session. */ - protected SessionImpl getSession() + public SessionImpl getSession() { return _session; } diff --git a/java/client/src/main/java/org/apache/qpidity/jms/SessionImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/SessionImpl.java index 88def8a807..9b716d2b99 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/SessionImpl.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/SessionImpl.java @@ -975,7 +975,7 @@ public class SessionImpl implements Session * * @throws JMSException If this method is called on a closed session. */ - protected void acknowledge() throws JMSException + public void acknowledge() throws JMSException { checkNotClosed(); if (getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE) diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/BytesMessageImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/BytesMessageImpl.java index 9323a03803..efc1b40d2d 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/BytesMessageImpl.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/BytesMessageImpl.java @@ -51,7 +51,27 @@ public class BytesMessageImpl extends MessageImpl implements BytesMessage */ protected DataOutputStream _dataOut = new DataOutputStream(_storedData); + //--- Constructor + /** + * Constructor used by SessionImpl. + */ + public BytesMessageImpl() + { + super(); + setMessageType(String.valueOf(MessageFactory.JAVAX_JMS_BYTESMESSAGE)); + } + + /** + * Constructor used by MessageFactory + * + * @param message The new qpid message. + */ + protected BytesMessageImpl(org.apache.qpidity.api.Message message) + { + super(message); + } + //--- BytesMessage API /** * Gets the number of bytes of the message body when the message * is in read-only mode. diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/MapMessageImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/MapMessageImpl.java index 24cd8a5a9a..d2493c1cc9 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/MapMessageImpl.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/MapMessageImpl.java @@ -40,6 +40,27 @@ public class MapMessageImpl extends MessageImpl implements MapMessage */ private Map _map = new HashMap(); + //--- Constructor + /** + * Constructor used by SessionImpl. + */ + public MapMessageImpl() + { + super(); + setMessageType(String.valueOf(MessageFactory.JAVAX_JMS_MAPMESSAGE)); + } + + /** + * Constructor used by MessageFactory + * + * @param message The new qpid message. + */ + protected MapMessageImpl(org.apache.qpidity.api.Message message) + { + super(message); + } + + //-- Map Message API /** * Indicates whether an key exists in this MapMessage. * diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/MessageFactory.java b/java/client/src/main/java/org/apache/qpidity/jms/message/MessageFactory.java index 8703afcaff..2f67beb995 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/MessageFactory.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/MessageFactory.java @@ -17,10 +17,59 @@ */ package org.apache.qpidity.jms.message; +import org.apache.qpidity.QpidException; + /** - * - * + * A factory for JMS messages */ public class MessageFactory { + /** + * JMS Message hierarchy. + */ + public static final byte JAVAX_JMS_MESSAGE = 1; + public static final byte JAVAX_JMS_TEXTMESSAGE = 2; + public static final byte JAVAX_JMS_STREAMMESSAGE = 3; + public static final byte JAVAX_JMS_BYTESMESSAGE = 4; + public static final byte JAVAX_JMS_OBJECTMESSAGE = 5; + public static final byte JAVAX_JMS_MAPMESSAGE = 6; + + /** + * Create a QpidMessage subclass according to the JMS message type. + * + * @param message The received qpidity messsage + * @return The newly craeted JMS message + * @throws QpidException If an appropriate Message class cannot be created. + */ + public static QpidMessage getQpidMessage(org.apache.qpidity.api.Message message) throws QpidException + { + QpidMessage result = null; + byte type = Byte.valueOf(message.getMessageProperties().getType()); + switch (type) + { + case JAVAX_JMS_MESSAGE: + result = new MessageImpl(message); + break; + case JAVAX_JMS_TEXTMESSAGE: + result = new TextMessageImpl(message); + break; + case JAVAX_JMS_STREAMMESSAGE: + result = new StreamMessageImpl(message); + break; + case JAVAX_JMS_BYTESMESSAGE: + result = new BytesMessageImpl(message); + break; + case JAVAX_JMS_OBJECTMESSAGE: + result = new ObjectMessageImpl(message); + break; + case JAVAX_JMS_MAPMESSAGE: + result = new MapMessageImpl(message); + break; + default: + throw new QpidException( + "Message type identifier is not mapped " + "to a Message class in the current factory: " + type, + null, null); + } + return result; + } } diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/MessageImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/MessageImpl.java index 18b2fa571b..1d1ee0582d 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/MessageImpl.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/MessageImpl.java @@ -18,6 +18,7 @@ package org.apache.qpidity.jms.message; import org.apache.qpidity.jms.ExceptionHelper; +import org.apache.qpidity.jms.MessageConsumerImpl; import org.apache.qpidity.QpidException; import javax.jms.*; @@ -52,6 +53,31 @@ public class MessageImpl extends QpidMessage implements Message */ protected boolean _proertiesReadOnly = false; + /** + * The message consumer through which this message was received. + */ + private MessageConsumerImpl _messageConsumer; + + //--- Constructor + /** + * Constructor used by SessionImpl. + */ + public MessageImpl() + { + super(); + setMessageType(String.valueOf(MessageFactory.JAVAX_JMS_MESSAGE)); + } + + /** + * Constructor used by MessageFactory + * + * @param message The new qpid message. + */ + protected MessageImpl(org.apache.qpidity.api.Message message) + { + super(message); + } + //---- javax.jms.Message interface /** * Get the message ID. @@ -799,9 +825,18 @@ public class MessageImpl extends QpidMessage implements Message super.setProperty(name, value); } + /** + * Acknowledgment of a message automatically acknowledges all + * messages previously received by the session. Clients may + * individually acknowledge messages or they may choose to acknowledge + * messages in application defined groups (which is done by acknowledging + * the last received message in the group). + * + * @throws JMSException If this method is called on a closed session. + */ public void acknowledge() throws JMSException { - // TODO + _messageConsumer.getSession().acknowledge(); } /** @@ -863,4 +898,15 @@ public class MessageImpl extends QpidMessage implements Message throw new MessageNotWriteableException("Cannot update message"); } } + + /** + * Set the MessageConsumerImpl through which this message was received. + *

This method is called after a message is received. + * + * @param messageConsumer the MessageConsumerImpl reference through which this message was received. + */ + public void setMessageConsumer(MessageConsumerImpl messageConsumer) + { + _messageConsumer = messageConsumer; + } } diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/ObjectMessageImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/ObjectMessageImpl.java index ab5ac21d78..d3653a0962 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/ObjectMessageImpl.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/ObjectMessageImpl.java @@ -43,6 +43,26 @@ public class ObjectMessageImpl extends MessageImpl implements ObjectMessage */ private Serializable _object = null; + //--- Constructor + /** + * Constructor used by SessionImpl. + */ + public ObjectMessageImpl() + { + super(); + setMessageType(String.valueOf(MessageFactory.JAVAX_JMS_OBJECTMESSAGE)); + } + + /** + * Constructor used by MessageFactory + * + * @param message The new qpid message. + */ + protected ObjectMessageImpl(org.apache.qpidity.api.Message message) + { + super(message); + } + //--- Interface ObjctMessage /** * Sets the serializable object containing this message's data. @@ -140,15 +160,13 @@ public class ObjectMessageImpl extends MessageImpl implements ObjectMessage catch (IOException ioe) { throw new QpidException( - "Unexpected error during rebuild of message in afterReceive() - " + - "The Object stored in the message was not a Serializable object.", + "Unexpected error during rebuild of message in afterReceive() - " + "The Object stored in the message was not a Serializable object.", null, ioe); } catch (ClassNotFoundException clnfe) { throw new QpidException( - "Unexpected error during rebuild of message in afterReceive() - " + - "Could not find the required class in classpath.", + "Unexpected error during rebuild of message in afterReceive() - " + "Could not find the required class in classpath.", null, clnfe); } } diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/QpidMessage.java b/java/client/src/main/java/org/apache/qpidity/jms/message/QpidMessage.java index 3fe838d3bb..34d75e1eba 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/QpidMessage.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/QpidMessage.java @@ -27,6 +27,7 @@ import javax.jms.Message; import java.util.Map; import java.util.Enumeration; import java.util.Vector; +import java.util.HashMap; import java.nio.ByteBuffer; @@ -51,13 +52,31 @@ public class QpidMessage //--- This is required as AMQP delivery modes are different from the JMS ones public static final short DELIVERY_MODE_PERSISTENT = 2; public static final short DELIVERY_MODE_NON_PERSISTENT = 1; - //--- This is the default message type - public static final String MESSAGE_TYPE = "JMS Message"; + + + //-- Constructors + + /** + * Constructor used when JMS messages are created by SessionImpl. + */ + protected QpidMessage() + { + // TODO we need an implementation class: _qpidityMessage + _messageProperties = new HashMap(); + } /** - * The message properties + * Constructor used when a Qpid message is received + * + * @param message The received message */ + protected QpidMessage(org.apache.qpidity.api.Message message) + { + _qpidityMessage = message; + _messageProperties = (Map) message.getMessageProperties().getApplicationHeaders(); + } + //---- getters and setters. /** * Get the message ID. * @@ -178,7 +197,6 @@ public class QpidMessage /** * Get this message type. - * The default value is {@link QpidMessage#MESSAGE_TYPE} * * @return This message type. */ diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessaeImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessaeImpl.java deleted file mode 100644 index 4fc64a9a23..0000000000 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessaeImpl.java +++ /dev/null @@ -1,1091 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpidity.jms.message; - -import javax.jms.*; -import java.io.IOException; -import java.io.EOFException; - -/** - * The JMS spec says: - * StreamMessage objects support the following conversion table. - * The marked cases must be supported. The unmarked cases must throw a JMSException. - * The String-to-primitive conversions may throw a runtime exception if the - * primitive's valueOf() method does not accept it as a valid String representation of the primitive. - *

A value written as the row type can be read as the column type. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |boolean | X X - * |byte | X X X X X - * |short | X X X X - * |char | X X - * |int | X X X - * |long | X X - * |float | X X X - * |double | X X - * |String | X X X X X X X X - * |byte[] | X - * |---------------------------------------------------------------------- - */ -public class StreamMessaeImpl extends BytesMessageImpl implements StreamMessage -{ - /** - * Those statics represent incoming field types. The type of a field is - * written first in the stream - */ - private static final byte BOOLEAN = 1; - private static final byte BYTE = 2; - private static final byte CHAR = 3; - private static final byte DOUBLE = 4; - private static final byte FLOAT = 5; - private static final byte INT = 6; - private static final byte LONG = 7; - private static final byte SHORT = 8; - private static final byte STRING = 9; - private static final byte BYTEARRAY = 10; - private static final byte NULL = 11; - - /** - * The size of the byteArray written in this stream - */ - private int _sizeOfByteArray = 0; - - /** - * Reads a boolean. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |boolean | X X - * - * @return The boolean value read - * @throws JMSException If reading a boolean fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public boolean readBoolean() throws JMSException - { - isReadableAndNotReadingByteArray(); - boolean result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case BOOLEAN: - result = super.readBoolean(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = Boolean.valueOf(new String(bArray)); - break; - case NULL: - result = false; - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads a byte. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |byte | X X - * - * @return The byte value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public byte readByte() throws JMSException - { - isReadableAndNotReadingByteArray(); - byte result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case BYTE: - result = super.readByte(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new Byte(new String(bArray)); - break; - case NULL: - result = Byte.valueOf(null); - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads a short. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |short | X X X - * - * @return The short value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public short readShort() throws JMSException - { - isReadableAndNotReadingByteArray(); - short result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case SHORT: - result = super.readShort(); - break; - case BYTE: - result = super.readByte(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new Short(new String(bArray)); - break; - case NULL: - result = Short.valueOf(null); - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads a char. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |char | X - * - * @return The char value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public char readChar() throws JMSException - { - isReadableAndNotReadingByteArray(); - char result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case CHAR: - result = super.readChar(); - break; - case NULL: - _dataIn.reset(); - throw new NullPointerException(); - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an Int. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |int | X X X X - * - * @return The int value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public int readInt() throws JMSException - { - isReadableAndNotReadingByteArray(); - int result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case INT: - result = super.readInt(); - break; - case SHORT: - result = super.readShort(); - break; - case BYTE: - result = super.readByte(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new Integer(new String(bArray)); - break; - case NULL: - result = Integer.valueOf(null); - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an Long. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |long | X X X X X - * - * @return The long value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public long readLong() throws JMSException - { - isReadableAndNotReadingByteArray(); - long result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case LONG: - result = super.readLong(); - break; - case INT: - result = super.readInt(); - break; - case SHORT: - result = super.readShort(); - break; - case BYTE: - result = super.readByte(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = (new Long(new String(bArray))); - break; - case NULL: - result = Long.valueOf(null); - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an Float. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |float | X X - * - * @return The float value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public float readFloat() throws JMSException - { - isReadableAndNotReadingByteArray(); - float result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case FLOAT: - result = super.readFloat(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new Float(new String(bArray)); - break; - case NULL: - result = Float.valueOf(null); - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an double. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |double | X X X - * - * @return The double value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public double readDouble() throws JMSException - { - isReadableAndNotReadingByteArray(); - double result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case DOUBLE: - result = super.readDouble(); - break; - case FLOAT: - result = super.readFloat(); - break; - case STRING: - int len = _dataIn.readInt(); - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new Double(new String(bArray)); - break; - case NULL: - result = Double.valueOf(null); - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an string. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |double | X X X X X X X X X - * - * @return The string value read - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public String readString() throws JMSException - { - isReadableAndNotReadingByteArray(); - String result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case BOOLEAN: - result = Boolean.valueOf(super.readBoolean()).toString(); - break; - case BYTE: - result = Byte.valueOf(super.readByte()).toString(); - break; - case SHORT: - result = Short.valueOf(super.readShort()).toString(); - break; - case CHAR: - result = Character.valueOf(super.readChar()).toString(); - break; - case INT: - result = Integer.valueOf(super.readInt()).toString(); - break; - case LONG: - result = Long.valueOf(super.readLong()).toString(); - break; - case FLOAT: - result = Float.valueOf(super.readFloat()).toString(); - break; - case DOUBLE: - result = Double.valueOf(super.readDouble()).toString(); - break; - case STRING: - int len = _dataIn.readInt(); - if (len == 0) - { - throw new NullPointerException("trying to read a null String"); - } - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new String(bArray); - break; - case NULL: - result = null; - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an byte[]. - *

- * | | boolean byte short char int long float double String byte[] - * |---------------------------------------------------------------------- - * |byte[] | X - *

The JMS spec says: - * To read the field value, readBytes should be successively called until - * it returns a value less than the length - * of the read buffer. The value of the bytes in the buffer following the last byte read is undefined. - * - * @param value The byte array into which the data is read. - * @return the total number of bytes read into the array, or -1 if - * there is no more data because the end of the byte field has been - * reached. - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public int readBytes(byte[] value) throws JMSException - { - isReadable(); - int result = -1; - try - { - byte type = BYTEARRAY; - if (_sizeOfByteArray == 0) - { - // we are not in the middle of reading this byte array - _dataIn.mark(10); - type = _dataIn.readByte(); - } - switch (type) - { - case BYTEARRAY: - if (_sizeOfByteArray == 0) - { - // we need to read the size of this byte array - _sizeOfByteArray = _dataIn.readInt(); - } - result = _dataIn.read(value, 0, value.length); - if (result != -1) - { - _sizeOfByteArray = _sizeOfByteArray - result; - } - else - { - _sizeOfByteArray = 0; - } - case NULL: - // result = -1; - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Reads an object from the stream message. - *

The JMS spec says: - *

This method can be used to return, in objectified format, - * an object in the Java programming language ("Java object") that has - * been written to the stream with the equivalent - * writeObject method call, or its equivalent primitive - * writetype method. - *

An attempt to call readObject to read a byte field - * value into a new byte[] object before the full value of the - * byte field has been read will throw a - * MessageFormatException. - * - * @return A Java object from the stream message, in objectified - * format - * @throws JMSException If reading fails due to some error. - * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. - * @throws javax.jms.MessageNotReadableException - * If the message is in write-only mode. - * @throws MessageFormatException If this type conversion is invalid. - */ - public Object readObject() throws JMSException - { - isReadableAndNotReadingByteArray(); - Object result; - try - { - _dataIn.mark(10); - byte type = _dataIn.readByte(); - switch (type) - { - case BOOLEAN: - result = super.readBoolean(); - break; - case BYTE: - result = super.readByte(); - break; - case SHORT: - result = super.readShort(); - break; - case CHAR: - result = super.readChar(); - break; - case INT: - result = super.readInt(); - break; - case LONG: - result = super.readLong(); - break; - case FLOAT: - result = super.readFloat(); - break; - case DOUBLE: - result = super.readDouble(); - break; - case STRING: - int len = _dataIn.readInt(); - if (len == 0) - { - result = null; - } - else - { - byte[] bArray = new byte[len]; - _dataIn.readFully(bArray); - result = new String(bArray); - } - break; - case BYTEARRAY: - int totalBytes = _dataIn.readInt(); - byte[] bArray = new byte[totalBytes]; - _dataIn.read(bArray, 0, totalBytes); - result = bArray; - break; - case NULL: - result = null; - break; - default: - _dataIn.reset(); - throw new MessageFormatException("Invalid Object Type"); - } - } - catch (EOFException eof) - { - throw new MessageEOFException("End of file Reached when reading message"); - } - catch (IOException io) - { - throw new JMSException("IO exception when reading message"); - } - return result; - } - - /** - * Writes a boolean to the stream message. - * - * @param val The boolean value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeBoolean(boolean val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(BOOLEAN); - super.writeBoolean(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a byte to the stream message. - * - * @param val The byte value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeByte(byte val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(BYTE); - super.writeByte(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a short to the stream message. - * - * @param val The short value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeShort(short val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(SHORT); - super.writeShort(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a char to the stream message. - * - * @param val The char value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeChar(char val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(CHAR); - super.writeChar(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a int to the stream message. - * - * @param val The int value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeInt(int val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(INT); - super.writeInt(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a long to the stream message. - * - * @param val The long value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeLong(long val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(LONG); - super.writeLong(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a float to the stream message. - * - * @param val The float value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeFloat(float val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(FLOAT); - super.writeFloat(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a double to the stream message. - * - * @param val The double value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeDouble(double val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(DOUBLE); - super.writeDouble(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a string to the stream message. - * - * @param val The string value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeString(String val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(STRING); - if (val == null) - { - _dataOut.writeInt(0); - } - else - { - byte[] bArray = val.getBytes(); - int len = bArray.length; - _dataOut.writeInt(len); - _dataOut.write(bArray); - } - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a byte array to the stream message. - * - * @param val The byte array value to be written - * @throws JMSException If writting a boolean fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeBytes(byte[] val) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(BYTEARRAY); - _dataOut.writeInt(val.length); - super.writeBytes(val); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes a portion of byte array to the bytes message. - * - * @param val The byte array value to be written - * @throws JMSException If writting a byte array fails due to some error. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeBytes(byte[] val, int offset, int length) throws JMSException - { - isWriteable(); - try - { - _dataOut.writeShort(BYTEARRAY); - _dataOut.writeInt(length); - super.writeBytes(val, offset, length); - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - /** - * Writes an Object to the bytes message. - * JMS spec says: - *

This method works only for the objectified primitive - * object types Integer, Double, Long, String and byte - * arrays. - * - * @param val The short value to be written - * @throws JMSException If writting a short fails due to some error. - * @throws NullPointerException if the parameter val is null. - * @throws MessageFormatException If the object is of an invalid type. - * @throws javax.jms.MessageNotWriteableException - * If the message is in read-only mode. - */ - public void writeObject(Object val) throws JMSException - { - isWriteable(); - try - { - if (val == null) - { - _dataOut.writeShort(NULL); - } - else if (val instanceof Byte) - { - writeByte((Byte) val); - } - else if (val instanceof Boolean) - { - writeBoolean((Boolean) val); - } - else if (val instanceof Short) - { - writeShort((Short) val); - } - else if (val instanceof Integer) - { - writeInt((Integer) val); - } - else if (val instanceof Long) - { - writeLong((Long) val); - } - else if (val instanceof Double) - { - writeDouble((Double) val); - } - else if (val instanceof Float) - { - writeFloat((Float) val); - } - else if (val instanceof Character) - { - writeChar((Character) val); - } - else if (val instanceof String) - { - writeString((String) val); - } - else if (val instanceof byte[]) - { - writeBytes((byte[]) val); - } - else - { - throw new MessageFormatException( - "The data type of the object specified as the value to writeObject " + "was of an invalid type."); - } - } - catch (IOException e) - { - throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); - } - } - - //-- overwritten methods - /** - * Test whether this message is readable by throwing a MessageNotReadableException if this - * message cannot be read. - * - * @throws javax.jms.MessageNotReadableException - * If this message cannot be read. - * @throws javax.jms.MessageFormatException - * If reading a byte array. - */ - protected void isReadableAndNotReadingByteArray() throws MessageNotReadableException, MessageFormatException - { - if (_dataIn == null) - { - throw new MessageNotReadableException("Cannot read this message"); - } - if (_sizeOfByteArray > 0) - { - throw new MessageFormatException( - "Read of object attempted while incomplete byteArray stored in message " + "- finish reading byte array first."); - } - } -} diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessageImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessageImpl.java new file mode 100644 index 0000000000..94075a3c6f --- /dev/null +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/StreamMessageImpl.java @@ -0,0 +1,1112 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpidity.jms.message; + +import javax.jms.*; +import java.io.IOException; +import java.io.EOFException; + +/** + * The JMS spec says: + * StreamMessage objects support the following conversion table. + * The marked cases must be supported. The unmarked cases must throw a JMSException. + * The String-to-primitive conversions may throw a runtime exception if the + * primitive's valueOf() method does not accept it as a valid String representation of the primitive. + *

A value written as the row type can be read as the column type. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |boolean | X X + * |byte | X X X X X + * |short | X X X X + * |char | X X + * |int | X X X + * |long | X X + * |float | X X X + * |double | X X + * |String | X X X X X X X X + * |byte[] | X + * |---------------------------------------------------------------------- + */ +public class StreamMessageImpl extends BytesMessageImpl implements StreamMessage +{ + /** + * Those statics represent incoming field types. The type of a field is + * written first in the stream + */ + private static final byte BOOLEAN = 1; + private static final byte BYTE = 2; + private static final byte CHAR = 3; + private static final byte DOUBLE = 4; + private static final byte FLOAT = 5; + private static final byte INT = 6; + private static final byte LONG = 7; + private static final byte SHORT = 8; + private static final byte STRING = 9; + private static final byte BYTEARRAY = 10; + private static final byte NULL = 11; + + /** + * The size of the byteArray written in this stream + */ + private int _sizeOfByteArray = 0; + + //--- Constructor + /** + * Constructor used by SessionImpl. + */ + public StreamMessageImpl() + { + super(); + setMessageType(String.valueOf(MessageFactory.JAVAX_JMS_STREAMMESSAGE)); + } + + /** + * Constructor used by MessageFactory + * + * @param message The new qpid message. + */ + protected StreamMessageImpl(org.apache.qpidity.api.Message message) + { + super(message); + } + + //--- Interface StreamMessage + /** + * Reads a boolean. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |boolean | X X + * + * @return The boolean value read + * @throws JMSException If reading a boolean fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public boolean readBoolean() throws JMSException + { + isReadableAndNotReadingByteArray(); + boolean result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case BOOLEAN: + result = super.readBoolean(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = Boolean.valueOf(new String(bArray)); + break; + case NULL: + result = false; + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads a byte. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |byte | X X + * + * @return The byte value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public byte readByte() throws JMSException + { + isReadableAndNotReadingByteArray(); + byte result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case BYTE: + result = super.readByte(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new Byte(new String(bArray)); + break; + case NULL: + result = Byte.valueOf(null); + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads a short. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |short | X X X + * + * @return The short value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public short readShort() throws JMSException + { + isReadableAndNotReadingByteArray(); + short result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case SHORT: + result = super.readShort(); + break; + case BYTE: + result = super.readByte(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new Short(new String(bArray)); + break; + case NULL: + result = Short.valueOf(null); + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads a char. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |char | X + * + * @return The char value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public char readChar() throws JMSException + { + isReadableAndNotReadingByteArray(); + char result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case CHAR: + result = super.readChar(); + break; + case NULL: + _dataIn.reset(); + throw new NullPointerException(); + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an Int. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |int | X X X X + * + * @return The int value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public int readInt() throws JMSException + { + isReadableAndNotReadingByteArray(); + int result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case INT: + result = super.readInt(); + break; + case SHORT: + result = super.readShort(); + break; + case BYTE: + result = super.readByte(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new Integer(new String(bArray)); + break; + case NULL: + result = Integer.valueOf(null); + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an Long. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |long | X X X X X + * + * @return The long value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public long readLong() throws JMSException + { + isReadableAndNotReadingByteArray(); + long result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case LONG: + result = super.readLong(); + break; + case INT: + result = super.readInt(); + break; + case SHORT: + result = super.readShort(); + break; + case BYTE: + result = super.readByte(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = (new Long(new String(bArray))); + break; + case NULL: + result = Long.valueOf(null); + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an Float. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |float | X X + * + * @return The float value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public float readFloat() throws JMSException + { + isReadableAndNotReadingByteArray(); + float result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case FLOAT: + result = super.readFloat(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new Float(new String(bArray)); + break; + case NULL: + result = Float.valueOf(null); + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an double. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |double | X X X + * + * @return The double value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public double readDouble() throws JMSException + { + isReadableAndNotReadingByteArray(); + double result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case DOUBLE: + result = super.readDouble(); + break; + case FLOAT: + result = super.readFloat(); + break; + case STRING: + int len = _dataIn.readInt(); + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new Double(new String(bArray)); + break; + case NULL: + result = Double.valueOf(null); + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an string. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |double | X X X X X X X X X + * + * @return The string value read + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public String readString() throws JMSException + { + isReadableAndNotReadingByteArray(); + String result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case BOOLEAN: + result = Boolean.valueOf(super.readBoolean()).toString(); + break; + case BYTE: + result = Byte.valueOf(super.readByte()).toString(); + break; + case SHORT: + result = Short.valueOf(super.readShort()).toString(); + break; + case CHAR: + result = Character.valueOf(super.readChar()).toString(); + break; + case INT: + result = Integer.valueOf(super.readInt()).toString(); + break; + case LONG: + result = Long.valueOf(super.readLong()).toString(); + break; + case FLOAT: + result = Float.valueOf(super.readFloat()).toString(); + break; + case DOUBLE: + result = Double.valueOf(super.readDouble()).toString(); + break; + case STRING: + int len = _dataIn.readInt(); + if (len == 0) + { + throw new NullPointerException("trying to read a null String"); + } + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new String(bArray); + break; + case NULL: + result = null; + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an byte[]. + *

+ * | | boolean byte short char int long float double String byte[] + * |---------------------------------------------------------------------- + * |byte[] | X + *

The JMS spec says: + * To read the field value, readBytes should be successively called until + * it returns a value less than the length + * of the read buffer. The value of the bytes in the buffer following the last byte read is undefined. + * + * @param value The byte array into which the data is read. + * @return the total number of bytes read into the array, or -1 if + * there is no more data because the end of the byte field has been + * reached. + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public int readBytes(byte[] value) throws JMSException + { + isReadable(); + int result = -1; + try + { + byte type = BYTEARRAY; + if (_sizeOfByteArray == 0) + { + // we are not in the middle of reading this byte array + _dataIn.mark(10); + type = _dataIn.readByte(); + } + switch (type) + { + case BYTEARRAY: + if (_sizeOfByteArray == 0) + { + // we need to read the size of this byte array + _sizeOfByteArray = _dataIn.readInt(); + } + result = _dataIn.read(value, 0, value.length); + if (result != -1) + { + _sizeOfByteArray = _sizeOfByteArray - result; + } + else + { + _sizeOfByteArray = 0; + } + case NULL: + // result = -1; + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Reads an object from the stream message. + *

The JMS spec says: + *

This method can be used to return, in objectified format, + * an object in the Java programming language ("Java object") that has + * been written to the stream with the equivalent + * writeObject method call, or its equivalent primitive + * writetype method. + *

An attempt to call readObject to read a byte field + * value into a new byte[] object before the full value of the + * byte field has been read will throw a + * MessageFormatException. + * + * @return A Java object from the stream message, in objectified + * format + * @throws JMSException If reading fails due to some error. + * @throws javax.jms.MessageEOFException If unexpected end of message data has been reached. + * @throws javax.jms.MessageNotReadableException + * If the message is in write-only mode. + * @throws MessageFormatException If this type conversion is invalid. + */ + public Object readObject() throws JMSException + { + isReadableAndNotReadingByteArray(); + Object result; + try + { + _dataIn.mark(10); + byte type = _dataIn.readByte(); + switch (type) + { + case BOOLEAN: + result = super.readBoolean(); + break; + case BYTE: + result = super.readByte(); + break; + case SHORT: + result = super.readShort(); + break; + case CHAR: + result = super.readChar(); + break; + case INT: + result = super.readInt(); + break; + case LONG: + result = super.readLong(); + break; + case FLOAT: + result = super.readFloat(); + break; + case DOUBLE: + result = super.readDouble(); + break; + case STRING: + int len = _dataIn.readInt(); + if (len == 0) + { + result = null; + } + else + { + byte[] bArray = new byte[len]; + _dataIn.readFully(bArray); + result = new String(bArray); + } + break; + case BYTEARRAY: + int totalBytes = _dataIn.readInt(); + byte[] bArray = new byte[totalBytes]; + _dataIn.read(bArray, 0, totalBytes); + result = bArray; + break; + case NULL: + result = null; + break; + default: + _dataIn.reset(); + throw new MessageFormatException("Invalid Object Type"); + } + } + catch (EOFException eof) + { + throw new MessageEOFException("End of file Reached when reading message"); + } + catch (IOException io) + { + throw new JMSException("IO exception when reading message"); + } + return result; + } + + /** + * Writes a boolean to the stream message. + * + * @param val The boolean value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeBoolean(boolean val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(BOOLEAN); + super.writeBoolean(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a byte to the stream message. + * + * @param val The byte value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeByte(byte val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(BYTE); + super.writeByte(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a short to the stream message. + * + * @param val The short value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeShort(short val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(SHORT); + super.writeShort(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a char to the stream message. + * + * @param val The char value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeChar(char val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(CHAR); + super.writeChar(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a int to the stream message. + * + * @param val The int value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeInt(int val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(INT); + super.writeInt(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a long to the stream message. + * + * @param val The long value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeLong(long val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(LONG); + super.writeLong(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a float to the stream message. + * + * @param val The float value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeFloat(float val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(FLOAT); + super.writeFloat(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a double to the stream message. + * + * @param val The double value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeDouble(double val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(DOUBLE); + super.writeDouble(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a string to the stream message. + * + * @param val The string value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeString(String val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(STRING); + if (val == null) + { + _dataOut.writeInt(0); + } + else + { + byte[] bArray = val.getBytes(); + int len = bArray.length; + _dataOut.writeInt(len); + _dataOut.write(bArray); + } + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a byte array to the stream message. + * + * @param val The byte array value to be written + * @throws JMSException If writting a boolean fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeBytes(byte[] val) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(BYTEARRAY); + _dataOut.writeInt(val.length); + super.writeBytes(val); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes a portion of byte array to the bytes message. + * + * @param val The byte array value to be written + * @throws JMSException If writting a byte array fails due to some error. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeBytes(byte[] val, int offset, int length) throws JMSException + { + isWriteable(); + try + { + _dataOut.writeShort(BYTEARRAY); + _dataOut.writeInt(length); + super.writeBytes(val, offset, length); + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + /** + * Writes an Object to the bytes message. + * JMS spec says: + *

This method works only for the objectified primitive + * object types Integer, Double, Long, String and byte + * arrays. + * + * @param val The short value to be written + * @throws JMSException If writting a short fails due to some error. + * @throws NullPointerException if the parameter val is null. + * @throws MessageFormatException If the object is of an invalid type. + * @throws javax.jms.MessageNotWriteableException + * If the message is in read-only mode. + */ + public void writeObject(Object val) throws JMSException + { + isWriteable(); + try + { + if (val == null) + { + _dataOut.writeShort(NULL); + } + else if (val instanceof Byte) + { + writeByte((Byte) val); + } + else if (val instanceof Boolean) + { + writeBoolean((Boolean) val); + } + else if (val instanceof Short) + { + writeShort((Short) val); + } + else if (val instanceof Integer) + { + writeInt((Integer) val); + } + else if (val instanceof Long) + { + writeLong((Long) val); + } + else if (val instanceof Double) + { + writeDouble((Double) val); + } + else if (val instanceof Float) + { + writeFloat((Float) val); + } + else if (val instanceof Character) + { + writeChar((Character) val); + } + else if (val instanceof String) + { + writeString((String) val); + } + else if (val instanceof byte[]) + { + writeBytes((byte[]) val); + } + else + { + throw new MessageFormatException( + "The data type of the object specified as the value to writeObject " + "was of an invalid type."); + } + } + catch (IOException e) + { + throw new JMSException("IO problem when writting " + e.getLocalizedMessage()); + } + } + + //-- overwritten methods + /** + * Test whether this message is readable by throwing a MessageNotReadableException if this + * message cannot be read. + * + * @throws javax.jms.MessageNotReadableException + * If this message cannot be read. + * @throws javax.jms.MessageFormatException + * If reading a byte array. + */ + protected void isReadableAndNotReadingByteArray() throws MessageNotReadableException, MessageFormatException + { + if (_dataIn == null) + { + throw new MessageNotReadableException("Cannot read this message"); + } + if (_sizeOfByteArray > 0) + { + throw new MessageFormatException( + "Read of object attempted while incomplete byteArray stored in message " + "- finish reading byte array first."); + } + } +} diff --git a/java/client/src/main/java/org/apache/qpidity/jms/message/TextMessageImpl.java b/java/client/src/main/java/org/apache/qpidity/jms/message/TextMessageImpl.java index df04673fe1..f5228cb6d2 100644 --- a/java/client/src/main/java/org/apache/qpidity/jms/message/TextMessageImpl.java +++ b/java/client/src/main/java/org/apache/qpidity/jms/message/TextMessageImpl.java @@ -41,8 +41,25 @@ public class TextMessageImpl extends MessageImpl implements TextMessage */ private String _messageText; - //-- constructor - // todo + //--- Constructor + /** + * Constructor used by SessionImpl. + */ + public TextMessageImpl() + { + super(); + setMessageType(String.valueOf(MessageFactory.JAVAX_JMS_STREAMMESSAGE)); + } + + /** + * Constructor used by MessageFactory + * + * @param message The new qpid message. + */ + protected TextMessageImpl(org.apache.qpidity.api.Message message) + { + super(message); + } //--- interface TextMessage -- cgit v1.2.1