@SuppressWarnings("unchecked") public static <T> T convertPropertyTo(String name, Object value, Class<T> target) throws JMSException { if (value == null) { if (Boolean.class.equals(target)) { return (T) Boolean.FALSE; } else if (Float.class.equals(target) || Double.class.equals(target)) { throw new NullPointerException("property " + name + " was null"); } else if (Number.class.isAssignableFrom(target)) { throw new NumberFormatException("property " + name + " was null"); } else { return null; } } T rc = (T) TypeConversionSupport.convert(value, target); if (rc == null) { throw new MessageFormatException("Property " + name + " was a " + value.getClass().getName() + " and cannot be read as a " + target.getName()); } return rc; }
public static void checkValidObject(Object value) throws MessageFormatException { boolean valid = value instanceof Boolean || value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long || value instanceof Float || value instanceof Double || value instanceof Character || value instanceof String || value == null; if (!valid) { throw new MessageFormatException("Only objectified primitive objects and String types are allowed but was: " + value + " type: " + value.getClass()); } }
@Override public int getInt(String name) throws JMSException { Object value = getObject(name); if (value instanceof Integer) { return ((Integer) value).intValue(); } else if (value instanceof Short) { return ((Short) value).intValue(); } else if (value instanceof Byte) { return ((Byte) value).intValue(); } else if (value instanceof String || value == null) { return Integer.valueOf((String) value).intValue(); } else { throw new MessageFormatException("Cannot read an int from " + value.getClass().getSimpleName()); } }
@Override public long getLong(String name) throws JMSException { Object value = getObject(name); if (value instanceof Long) { return ((Long) value).longValue(); } else if (value instanceof Integer) { return ((Integer) value).longValue(); } else if (value instanceof Short) { return ((Short) value).longValue(); } else if (value instanceof Byte) { return ((Byte) value).longValue(); } else if (value instanceof String || value == null) { return Long.valueOf((String) value).longValue(); } else { throw new MessageFormatException("Cannot read a long from " + value.getClass().getSimpleName()); } }
private void checkValidObject(Object value) throws MessageFormatException { boolean valid = value instanceof Boolean || value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long || value instanceof Float || value instanceof Double || value instanceof Character || value instanceof String || value instanceof byte[] || value == null; if (!valid) { throw new MessageFormatException("Only objectified primitive objects and String types are allowed but was: " + value + " type: " + value.getClass()); } }
private void doSend(Destination destination, Message message) throws JMSException { if (message == null) { throw new MessageFormatException("Message must not be null"); } for (Map.Entry<String, Object> entry : messageProperties.entrySet()) { message.setObjectProperty(entry.getKey(), entry.getValue()); } if (correlationId != null) { message.setJMSCorrelationID(correlationId); } if (correlationIdBytes != null) { message.setJMSCorrelationIDAsBytes(correlationIdBytes); } if (type != null) { message.setJMSType(type); } if (replyTo != null) { message.setJMSReplyTo(replyTo); } session.send(producer, destination, message, deliveryMode, priority, timeToLive, disableMessageId, disableTimestamp, deliveryDelay, completionListener); }
/** * Get the value for a property that represents a java primitive(e.g. int or * long). * * @param property The name of the property to get. * @param type The type of the property. * @return the converted value for the property. * @throws JMSException On internal error. * @throws MessageFormatException If the property cannot be converted to the specified type. * @throws NullPointerException and NumberFormatException when property name or value is * null. Method throws same exception as primitives * corresponding valueOf(String) method. */ <T> T getPrimitiveProperty(final String property, final Class<T> type) throws JMSException { if (property == null) { throw new NullPointerException("Property name is null"); } final Object value = getObjectProperty(property); if (value == null) { return handleNullPropertyValue(property, type); } final T convertedValue = TypeConversionSupport.convert(value, type); if (convertedValue == null) { throw new MessageFormatException("Property " + property + " was " + value.getClass().getName() + " and cannot be read as " + type.getName()); } return convertedValue; }
@SuppressWarnings("unchecked") private static <T> T convertPropertyTo(String name, Object value, Class<T> target) throws JMSException { if (value == null) { if (Boolean.class.equals(target)) { return (T) Boolean.FALSE; } else if (Float.class.equals(target) || Double.class.equals(target)) { throw new NullPointerException("property " + name + " was null"); } else if (Number.class.isAssignableFrom(target)) { throw new NumberFormatException("property " + name + " was null"); } else { return null; } } if (target.isInstance(value)) { return target.cast(value); } else { try { return target.getConstructor(String.class).newInstance(value.toString()); } catch (Exception e) { throw new MessageFormatException("Property " + name + " was a " + value.getClass().getName() + " and cannot be read as a " + target.getName()); } } }
/** * Convert from a JMS Message to a Java object. * * @param message the message to convert for discovering who sends it * @return the converted Java object * @throws javax.jms.JMSException if thrown by JMS API methods */ @Override public Object fromMessage(final Message message) throws JMSException { if (!(message instanceof TextMessage)) { throw new MessageFormatException("Expected TextMessage as response but received " + message.getClass()); } else { try { // LOGGER.debug("fromMessage() - Message received: " + ((TextMessage) message).getText()); LOGGER.debug("fromMessage() - Message properly received"); return this.xmlConverter.fromXml(((TextMessage) message).getText()); } catch (Exception ex) { LOGGER.error("fromMessage() - Error caught in conversion of JMS message to Process Object"); LOGGER.error("Message was: " + ((TextMessage) message).getText()); throw new JMSException(ex.getMessage()); } } }
public Object Receive(String filter, long timeoutReceive) throws JMSException { System.out.println("CLIENT: Creating cosumer: " + filter + "'"); MessageConsumer consumer = queuesession.createConsumer(queue, filter); Message mrcv; if ((mrcv = consumer.receive(timeoutReceive)) != null) { Object rm1; try { rm1 = ((ObjectMessage) mrcv).getObject(); //System.out.println("RCV1: " + mrcv.getJMSCorrelationID() + " req=" + rm1.name); return rm1; } catch (MessageFormatException e) { System.out.println("MessageFormatException: " + e.getMessage()); mrcv.acknowledge(); } } return null; }
protected void checkValidObject(Object value) throws MessageFormatException { boolean valid = value instanceof Boolean || value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long; valid = valid || value instanceof Float || value instanceof Double || value instanceof Character || value instanceof String || value == null; if (!valid) { ActiveMQConnection conn = getConnection(); // conn is null if we are in the broker rather than a JMS client if (conn == null || conn.isNestedMapAndListEnabled()) { if (!(value instanceof Map || value instanceof List)) { throw new MessageFormatException("Only objectified primitive objects, String, Map and List types are allowed but was: " + value + " type: " + value.getClass()); } } else { throw new MessageFormatException("Only objectified primitive objects and String types are allowed but was: " + value + " type: " + value.getClass()); } } }
@Override public String getStringProperty(String name) throws JMSException { Object value = null; if (name.equals("JMSXUserID")) { value = getUserID(); if (value == null) { value = getObjectProperty(name); } } else { value = getObjectProperty(name); } if (value == null) { return null; } String rc = (String) TypeConversionSupport.convert(value, String.class); if (rc == null) { throw new MessageFormatException("Property " + name + " was a " + value.getClass().getName() + " and cannot be read as a String"); } return rc; }
/** * Returns the <CODE>boolean</CODE> value with the specified name. * * @param name the name of the <CODE>boolean</CODE> * @return the <CODE>boolean</CODE> value with the specified name * @throws JMSException if the JMS provider fails to read the message due to * some internal error. * @throws MessageFormatException if this type conversion is invalid. */ @Override public boolean getBoolean(String name) throws JMSException { initializeReading(); Object value = map.get(name); if (value == null) { return false; } if (value instanceof Boolean) { return ((Boolean)value).booleanValue(); } if (value instanceof UTF8Buffer) { return Boolean.valueOf(value.toString()).booleanValue(); } if (value instanceof String) { return Boolean.valueOf(value.toString()).booleanValue(); } else { throw new MessageFormatException(" cannot read a boolean from " + value.getClass().getName()); } }
/** * Returns the <CODE>byte</CODE> value with the specified name. * * @param name the name of the <CODE>byte</CODE> * @return the <CODE>byte</CODE> value with the specified name * @throws JMSException if the JMS provider fails to read the message due to * some internal error. * @throws MessageFormatException if this type conversion is invalid. */ @Override public byte getByte(String name) throws JMSException { initializeReading(); Object value = map.get(name); if (value == null) { return 0; } if (value instanceof Byte) { return ((Byte)value).byteValue(); } if (value instanceof UTF8Buffer) { return Byte.valueOf(value.toString()).byteValue(); } if (value instanceof String) { return Byte.valueOf(value.toString()).byteValue(); } else { throw new MessageFormatException(" cannot read a byte from " + value.getClass().getName()); } }
/** * Returns the <CODE>short</CODE> value with the specified name. * * @param name the name of the <CODE>short</CODE> * @return the <CODE>short</CODE> value with the specified name * @throws JMSException if the JMS provider fails to read the message due to * some internal error. * @throws MessageFormatException if this type conversion is invalid. */ @Override public short getShort(String name) throws JMSException { initializeReading(); Object value = map.get(name); if (value == null) { return 0; } if (value instanceof Short) { return ((Short)value).shortValue(); } if (value instanceof Byte) { return ((Byte)value).shortValue(); } if (value instanceof UTF8Buffer) { return Short.valueOf(value.toString()).shortValue(); } if (value instanceof String) { return Short.valueOf(value.toString()).shortValue(); } else { throw new MessageFormatException(" cannot read a short from " + value.getClass().getName()); } }
/** * Returns the <CODE>int</CODE> value with the specified name. * * @param name the name of the <CODE>int</CODE> * @return the <CODE>int</CODE> value with the specified name * @throws JMSException if the JMS provider fails to read the message due to * some internal error. * @throws MessageFormatException if this type conversion is invalid. */ @Override public int getInt(String name) throws JMSException { initializeReading(); Object value = map.get(name); if (value == null) { return 0; } if (value instanceof Integer) { return ((Integer)value).intValue(); } if (value instanceof Short) { return ((Short)value).intValue(); } if (value instanceof Byte) { return ((Byte)value).intValue(); } if (value instanceof UTF8Buffer) { return Integer.valueOf(value.toString()).intValue(); } if (value instanceof String) { return Integer.valueOf(value.toString()).intValue(); } else { throw new MessageFormatException(" cannot read an int from " + value.getClass().getName()); } }
/** * Returns the <CODE>float</CODE> value with the specified name. * * @param name the name of the <CODE>float</CODE> * @return the <CODE>float</CODE> value with the specified name * @throws JMSException if the JMS provider fails to read the message due to * some internal error. * @throws MessageFormatException if this type conversion is invalid. */ @Override public float getFloat(String name) throws JMSException { initializeReading(); Object value = map.get(name); if (value == null) { return 0; } if (value instanceof Float) { return ((Float)value).floatValue(); } if (value instanceof UTF8Buffer) { return Float.valueOf(value.toString()).floatValue(); } if (value instanceof String) { return Float.valueOf(value.toString()).floatValue(); } else { throw new MessageFormatException(" cannot read a float from " + value.getClass().getName()); } }
/** * Returns the <CODE>double</CODE> value with the specified name. * * @param name the name of the <CODE>double</CODE> * @return the <CODE>double</CODE> value with the specified name * @throws JMSException if the JMS provider fails to read the message due to * some internal error. * @throws MessageFormatException if this type conversion is invalid. */ @Override public double getDouble(String name) throws JMSException { initializeReading(); Object value = map.get(name); if (value == null) { return 0; } if (value instanceof Double) { return ((Double)value).doubleValue(); } if (value instanceof Float) { return ((Float)value).floatValue(); } if (value instanceof UTF8Buffer) { return Float.valueOf(value.toString()).floatValue(); } if (value instanceof String) { return Float.valueOf(value.toString()).floatValue(); } else { throw new MessageFormatException(" cannot read a double from " + value.getClass().getName()); } }
@Override public byte readByte() throws JMSException { if (bodyWriteOnly) { throw new MessageNotReadableException("The message body is writeonly"); } try { Object value = content.get(position); offset = 0; if (value == null) { throw new NullPointerException("Value is null"); } else if (value instanceof Byte) { position++; return ((Byte) value).byteValue(); } else if (value instanceof String) { byte result = Byte.parseByte((String) value); position++; return result; } else { throw new MessageFormatException("Invalid conversion"); } } catch (IndexOutOfBoundsException e) { throw new MessageEOFException(""); } }
@Override public char readChar() throws JMSException { if (bodyWriteOnly) { throw new MessageNotReadableException("The message body is writeonly"); } try { Object value = content.get(position); offset = 0; if (value == null) { throw new NullPointerException("Value is null"); } else if (value instanceof Character) { position++; return ((Character) value).charValue(); } else { throw new MessageFormatException("Invalid conversion"); } } catch (IndexOutOfBoundsException e) { throw new MessageEOFException(""); } }
@Override public float readFloat() throws JMSException { if (bodyWriteOnly) { throw new MessageNotReadableException("The message body is writeonly"); } try { Object value = content.get(position); offset = 0; if (value == null) { throw new NullPointerException("Value is null"); } else if (value instanceof Float) { position++; return ((Float) value).floatValue(); } else if (value instanceof String) { float result = Float.parseFloat((String) value); position++; return result; } else { throw new MessageFormatException("Invalid conversion"); } } catch (IndexOutOfBoundsException e) { throw new MessageEOFException(""); } }
@Override public boolean getBoolean(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Boolean.valueOf(null).booleanValue(); } if (value instanceof Boolean) { return ((Boolean) value).booleanValue(); } else if (value instanceof String) { return Boolean.valueOf((String) value).booleanValue(); } else { throw new MessageFormatException("Invalid conversion"); } }
@Override public byte getByte(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Byte.parseByte(null); } if (value instanceof Byte) { return ((Byte) value).byteValue(); } else if (value instanceof String) { return Byte.parseByte((String) value); } else { throw new MessageFormatException("Invalid conversion"); } }
@Override public short getShort(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Short.parseShort(null); } if (value instanceof Byte) { return ((Byte) value).shortValue(); } else if (value instanceof Short) { return ((Short) value).shortValue(); } else if (value instanceof String) { return Short.parseShort((String) value); } else { throw new MessageFormatException("Invalid conversion"); } }
@Override public String getStringProperty(final String name) throws JMSException { if (MessageUtil.JMSXDELIVERYCOUNT.equals(name)) { return String.valueOf(message.getDeliveryCount()); } try { if (MessageUtil.JMSXGROUPID.equals(name)) { return message.getStringProperty(org.apache.activemq.artemis.api.core.Message.HDR_GROUP_ID); } else if (MessageUtil.JMSXUSERID.equals(name)) { return message.getValidatedUserID(); } else { return message.getStringProperty(name); } } catch (ActiveMQPropertyConversionException e) { throw new MessageFormatException(e.getMessage()); } }
@Override public int getInt(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Integer.parseInt(null); } if (value instanceof Byte) { return ((Byte) value).intValue(); } else if (value instanceof Short) { return ((Short) value).intValue(); } else if (value instanceof Integer) { return ((Integer) value).intValue(); } else if (value instanceof String) { return Integer.parseInt((String) value); } else { throw new MessageFormatException("Invalid conversion"); } }
@Override public long getLong(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Long.parseLong(null); } if (value instanceof Byte) { return ((Byte) value).longValue(); } else if (value instanceof Short) { return ((Short) value).longValue(); } else if (value instanceof Integer) { return ((Integer) value).longValue(); } else if (value instanceof Long) { return ((Long) value).longValue(); } else if (value instanceof String) { return Long.parseLong((String) value); } else { throw new MessageFormatException("Invalid conversion"); } }
@Override public float getFloat(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Float.parseFloat(null); } if (value instanceof Float) { return ((Float) value).floatValue(); } else if (value instanceof String) { return Float.parseFloat((String) value); } else { throw new MessageFormatException("Invalid conversion"); } }
@Override public double getDouble(final String name) throws JMSException { Object value; value = content.get(name); if (value == null) { return Double.parseDouble(null); } if (value instanceof Float) { return ((Float) value).doubleValue(); } else if (value instanceof Double) { return ((Double) value).doubleValue(); } else if (value instanceof String) { return Double.parseDouble((String) value); } else { throw new MessageFormatException("Invalid conversion"); } }
private void doSend(Destination destination, Message message) throws JMSException { if (message == null) { throw new MessageFormatException("Message must not be null"); } for (Map.Entry<String, Object> entry : messageProperties.entrySet()) { message.setObjectProperty(entry.getKey(), entry.getValue()); } if (correlationId != null) { message.setJMSCorrelationID(correlationId); } if (correlationIdBytes != null) { message.setJMSCorrelationIDAsBytes(correlationIdBytes); } if (type != null) { message.setJMSType(type); } if (replyTo != null) { message.setJMSReplyTo(replyTo); } if (completionListener != null) { producer.send(destination, message, deliveryMode, priority, timeToLive, completionListener); } else { producer.send(destination, message, deliveryMode, priority, timeToLive); } }
@Override public final <T> T getBody(Class<T> asType) throws JMSException { if (isBodyAssignableTo(asType)) { return doGetBody(asType); } throw new MessageFormatException("Message body cannot be read as type: " + asType); }
@Override public void writeObject(Object value) throws JMSException { if (value == null) { throw new NullPointerException(); } initializeWriting(); if (value instanceof Boolean) { writeBoolean(((Boolean) value).booleanValue()); } else if (value instanceof Character) { writeChar(((Character) value).charValue()); } else if (value instanceof Byte) { writeByte(((Byte) value).byteValue()); } else if (value instanceof Short) { writeShort(((Short) value).shortValue()); } else if (value instanceof Integer) { writeInt(((Integer) value).intValue()); } else if (value instanceof Long) { writeLong(((Long) value).longValue()); } else if (value instanceof Float) { writeFloat(((Float) value).floatValue()); } else if (value instanceof Double) { writeDouble(((Double) value).doubleValue()); } else if (value instanceof String) { writeUTF(value.toString()); } else if (value instanceof byte[]) { writeBytes((byte[]) value); } else { throw new MessageFormatException("Cannot write non-primitive type:" + value.getClass()); } }
@SuppressWarnings("unchecked") @Override protected <T> T doGetBody(Class<T> asType) throws JMSException { try { return (T) getObject(); } catch (JMSException e) { throw new MessageFormatException("Failed to read Object: " + e.getMessage()); } }
@Override public boolean getBoolean(String name) throws JMSException { Object value = getObject(name); if (value instanceof Boolean) { return ((Boolean) value).booleanValue(); } else if (value instanceof String || value == null) { return Boolean.valueOf((String) value).booleanValue(); } else { throw new MessageFormatException("Cannot read a boolean from " + value.getClass().getSimpleName()); } }
@Override public byte getByte(String name) throws JMSException { Object value = getObject(name); if (value instanceof Byte) { return ((Byte) value).byteValue(); } else if (value instanceof String || value == null) { return Byte.valueOf((String) value).byteValue(); } else { throw new MessageFormatException("Cannot read a byte from " + value.getClass().getSimpleName()); } }
@Override public short getShort(String name) throws JMSException { Object value = getObject(name); if (value instanceof Short) { return ((Short) value).shortValue(); } else if (value instanceof Byte) { return ((Byte) value).shortValue(); } else if (value instanceof String || value == null) { return Short.valueOf((String) value).shortValue(); } else { throw new MessageFormatException("Cannot read a short from " + value.getClass().getSimpleName()); } }
@Override public char getChar(String name) throws JMSException { Object value = getObject(name); if (value == null) { throw new NullPointerException(); } else if (value instanceof Character) { return ((Character) value).charValue(); } else { throw new MessageFormatException("Cannot read a char from " + value.getClass().getSimpleName()); } }
@Override public float getFloat(String name) throws JMSException { Object value = getObject(name); if (value instanceof Float) { return ((Float) value).floatValue(); } else if (value instanceof String || value == null) { return Float.valueOf((String) value).floatValue(); } else { throw new MessageFormatException("Cannot read a float from " + value.getClass().getSimpleName()); } }
@Override public double getDouble(String name) throws JMSException { Object value = getObject(name); if (value instanceof Double) { return ((Double) value).doubleValue(); } else if (value instanceof Float) { return ((Float) value).floatValue(); } else if (value instanceof String || value == null) { return Double.valueOf((String) value).doubleValue(); } else { throw new MessageFormatException("Cannot read a double from " + value.getClass().getSimpleName()); } }
@Override public String getString(String name) throws JMSException { Object value = getObject(name); if (value == null) { return null; } else if (value instanceof byte[]) { throw new MessageFormatException("Use getBytes to read a byte array"); } else { return value.toString(); } }