mapmessageimpl.java

来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 616 行 · 第 1/2 页

JAVA
616
字号
     * @throws JMSException if JMS fails to read the message due to some     * internal JMS error     * @throws MessageFormatException if this type conversion is invalid     */    public final String getString(String name)        throws JMSException, MessageFormatException {        return FormatConverter.getString(_map.get(name));    }    /**     * Return the byte array value with the given name     *     * @param name the name of the byte array     * @return a copy of the byte array value with the given name.     * If there is no item by this name, a null value is returned.     * @throws JMSException if JMS fails to read the message due to some     * internal JMS error     * @throws MessageFormatException if this type conversion is invalid     */    public final byte[] getBytes(String name)        throws JMSException, MessageFormatException {        return FormatConverter.getBytes(_map.get(name));    }    /**     * Return the Java object value with the given name     * <p>     * Note that this method can be used to return in objectified format,     * an object that had been stored in the Map with the equivalent     * <code>setObject</code> method call, or it's equivalent primitive     * set<type> method.     *     * @param name the name of the Java object     * @return a copy of the Java object value with the given name, in     * objectified format (eg. if it set as an int, then an Integer is     * returned).     * Note that byte values are returned as byte[], not Byte[].     * If there is no item by this name, a null value is returned.     * @throws JMSException if JMS fails to read the message due to some     * internal JMS error     */    public final Object getObject(String name) throws JMSException {        Object result = null;        Object value = _map.get(name);        if (value != null) {            if (value instanceof Boolean) {                result = new Boolean(((Boolean) value).booleanValue());            } else if (value instanceof Byte) {                result = new Byte(((Byte) value).byteValue());            } else if (value instanceof Short) {                result = new Short(((Short) value).shortValue());            } else if (value instanceof Character) {                result = new Character(((Character) value).charValue());            } else if (value instanceof Integer) {                result = new Integer(((Integer) value).intValue());            } else if (value instanceof Long) {                result = new Long(((Long) value).longValue());            } else if (value instanceof Float) {                result = new Float(((Float) value).floatValue());            } else if (value instanceof Double) {                result = new Double(((Double) value).doubleValue());            } else if (value instanceof String) {                result = (String) value;            } else if (value instanceof byte[]) {                result = getBytes(name);            } else {                throw new MessageFormatException(                    "MapMessage contains an unsupported object of type=" +                    value.getClass().getName());            }        }        return result;    }    /**     * Return an Enumeration of all the Map message's names.     *     * @return an enumeration of all the names in this Map message.     */    public final Enumeration getMapNames() {        return Collections.enumeration(_map.keySet());    }    /**     * Set a boolean value with the given name, into the Map     *     * @param name the name of the boolean     * @param value the boolean value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setBoolean(String name, boolean value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Boolean(value));    }    /**     * Set a byte value with the given name, into the Map     *     * @param name the name of the byte     * @param value the byte value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setByte(String name, byte value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Byte(value));    }    /**     * Set a short value with the given name, into the Map     *     * @param name the name of the short     * @param value the short value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setShort(String name, short value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Short(value));    }    /**     * Set a Unicode character value with the given name, into the Map     *     * @param name the name of the Unicode character     * @param value the Unicode character value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setChar(String name, char value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Character(value));    }    /**     * Set an integer value with the given name, into the Map     *     * @param name the name of the integer     * @param value the integer value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setInt(String name, int value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Integer(value));    }    /**     * Set a long value with the given name, into the Map     *     * @param name the name of the long     * @param value the long value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setLong(String name, long value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Long(value));    }    /**     * Set a float value with the given name, into the Map     *     * @param name the name of the float     * @param value the float value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setFloat(String name, float value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Float(value));    }    /**     * Set a double value with the given name, into the Map     *     * @param name the name of the double     * @param value the double value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setDouble(String name, double value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, new Double(value));    }    /**     * Set a String value with the given name, into the Map     *     * @param name the name of the String     * @param value the String value to set in the Map     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setString(String name, String value)        throws MessageNotWriteableException {        checkWrite();        _map.put(name, value);    }    /**     * Set a byte array value with the given name, into the Map     *     * @param name the name of the byte array     * @param value the byte array value to set in the Map. The array is     * copied so the value for name will not be altered by future     * modifications.     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setBytes(String name, byte[] value)        throws MessageNotWriteableException {        checkWrite();        byte[] bytes = null;        if (value != null) {            bytes = new byte[value.length];            System.arraycopy(value, 0, bytes, 0, bytes.length);        }        _map.put(name, bytes);    }    /**     * Set a portion of the byte array value with the given name, into the Map     *     * @param name the name of the byte array     * @param value the byte array value to set in the Map.     * @param offset the initial offset within the byte array.     * @param length the number of bytes to use.     * @throws MessageNotWriteableException if the message is in read-only mode     */    public final void setBytes(String name, byte[] value,                               int offset, int length)        throws MessageNotWriteableException {        checkWrite();        byte[] bytes = null;        if (value != null) {            bytes = new byte[length];            System.arraycopy(value, offset, bytes, 0, length);        }        _map.put(name, bytes);    }    /**     * Set a Java object value with the given name, into the Map     * <p>     * Note that this method only works for the objectified primitive     * object types (Integer, Double, Long ...), String's and byte arrays.     *     * @param name the name of the Java object     * @param value the Java object value to set in the Map     * @throws MessageFormatException if object is invalid     * @throws MessageNotWriteableException if message in read-only mode.     */    public final void setObject(String name, Object value)        throws MessageFormatException, MessageNotWriteableException {        checkWrite();        if (value == null) {            _map.put(name, null);        } else if (value instanceof Boolean) {            setBoolean(name, ((Boolean) value).booleanValue());        } else if (value instanceof Byte) {            setByte(name, ((Byte) value).byteValue());        } else if (value instanceof Short) {            setShort(name, ((Short) value).shortValue());        } else if (value instanceof Character) {            setChar(name, ((Character) value).charValue());        } else if (value instanceof Integer) {            setInt(name, ((Integer) value).intValue());        } else if (value instanceof Long) {            setLong(name, ((Long) value).longValue());        } else if (value instanceof Float) {            setFloat(name, ((Float) value).floatValue());        } else if (value instanceof Double) {            setDouble(name, ((Double) value).doubleValue());        } else if (value instanceof String) {            setString(name, (String) value);        } else if (value instanceof byte[]) {            setBytes(name, (byte[]) value);        } else {            throw new MessageFormatException(                "MapMessage does not support objects of type=" +                value.getClass().getName());        }    }    /**     * Check if an item exists in this MapMessage     *     * @param name the name of the item to test     * @return true if the item exists     */    public final boolean itemExists(String name) {        return _map.containsKey(name);    }    /**     * Clear out the message body. Clearing a message's body does not clear     * its header values or property entries.     * If this message body was read-only, calling this method leaves the     * message body is in the same state as an empty body in a newly created     * message     */    public final void clearBody() throws JMSException {        super.clearBody();        _map = new HashMap(INITIAL_SIZE);    }} // End MapMessageImpl

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?