001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jms.util;
023:
024: import javax.jms.JMSException;
025: import javax.jms.MapMessage;
026: import javax.jms.MessageFormatException;
027: import java.util.Collections;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030:
031: /**
032: * JMS specific map backed by a {@link HashMap}. Does not implement
033: * <code>Map</code> but instead implements the body elements of the
034: * {@link MapMessage}. Serves as he basis for {@link MessageProperties}
035: * and the body for the <code>MapMessage</code>.
036: *
037: * @author <a href="mailto:nathan@jboss.org">Nathan Phelps</a>
038: * @version $Revision: 57195 $ $Date: 2006-09-26 08:08:17 -0400 (Tue, 26 Sep 2006) $
039: */
040: public class JMSMap implements java.io.Serializable {
041:
042: protected HashMap contents = new HashMap();
043:
044: public static final JMSMap createInstance(Class type) {
045: if (type.equals(MapMessage.class)) {
046: return new JMSMap();
047: } else {
048: return new MessageProperties();
049: }
050: }
051:
052: public final void clear() {
053: this .contents.clear();
054: }
055:
056: public boolean getBoolean(String name) throws JMSException {
057: Object value = this .contents.get(name);
058: return JMSTypeConversions.getBoolean(value);
059: }
060:
061: public byte getByte(String name) throws JMSException {
062: Object value = this .contents.get(name);
063: return JMSTypeConversions.getByte(value);
064: }
065:
066: public byte[] getBytes(String name) throws JMSException {
067: Object value = this .contents.get(name);
068: return JMSTypeConversions.getBytes(value);
069: }
070:
071: public char getChar(String name) throws JMSException {
072: Object value = this .contents.get(name);
073: return JMSTypeConversions.getChar(value);
074: }
075:
076: public double getDouble(String name) throws JMSException {
077: Object value = this .contents.get(name);
078: return JMSTypeConversions.getDouble(value);
079: }
080:
081: public float getFloat(String name) throws JMSException {
082: Object value = this .contents.get(name);
083: return JMSTypeConversions.getFloat(value);
084: }
085:
086: public int getInt(String name) throws JMSException {
087: Object value = this .contents.get(name);
088: return JMSTypeConversions.getInt(value);
089: }
090:
091: public long getLong(String name) throws JMSException {
092: Object value = this .contents.get(name);
093: return JMSTypeConversions.getLong(value);
094: }
095:
096: public Enumeration getMapNames() throws JMSException {
097: return Collections.enumeration(this .contents.keySet());
098: }
099:
100: public Object getObject(String name) throws JMSException {
101: return this .contents.get(name);
102: }
103:
104: public short getShort(String name) throws JMSException {
105: Object value = this .contents.get(name);
106: return JMSTypeConversions.getShort(value);
107: }
108:
109: public String getString(String name) throws JMSException {
110: Object value = this .contents.get(name);
111: return JMSTypeConversions.getString(value);
112: }
113:
114: public boolean itemExists(String name) {
115: return this .contents.containsKey(name);
116: }
117:
118: public void setBoolean(String name, boolean value)
119: throws JMSException {
120: this .contents.put(name, new Boolean(value));
121: }
122:
123: public void setByte(String name, byte value) throws JMSException {
124: this .contents.put(name, new Byte(value));
125: }
126:
127: public void setBytes(String name, byte[] value) throws JMSException {
128: byte[] bytes = new byte[value.length];
129: System.arraycopy(value, 0, bytes, 0, bytes.length);
130: this .contents.put(name, bytes);
131: }
132:
133: public void setBytes(String name, byte[] value, int offset,
134: int length) throws JMSException {
135: byte[] bytes = new byte[length];
136: System.arraycopy(value, offset, bytes, 0, length);
137: this .contents.put(name, bytes);
138: }
139:
140: public void setChar(String name, char value) throws JMSException {
141: this .contents.put(name, new Character(value));
142: }
143:
144: public void setDouble(String name, double value)
145: throws JMSException {
146: this .contents.put(name, new Double(value));
147: }
148:
149: public void setFloat(String name, float value) throws JMSException {
150: this .contents.put(name, new Float(value));
151: }
152:
153: public void setInt(String name, int value) throws JMSException {
154: this .contents.put(name, new Integer(value));
155: }
156:
157: public void setLong(String name, long value) throws JMSException {
158: this .contents.put(name, new Long(value));
159: }
160:
161: public void setObject(String name, Object value)
162: throws JMSException {
163: if (value instanceof Boolean || value instanceof Byte
164: || value instanceof Character
165: || value instanceof Double || value instanceof Float
166: || value instanceof Integer || value instanceof Long
167: || value instanceof Short || value instanceof String) {
168: this .contents.put(name, value);
169: } else {
170: throw new MessageFormatException(""); //TOD: Implement message
171: }
172: }
173:
174: public void setShort(String name, short value) throws JMSException {
175: this .contents.put(name, new Short(value));
176: }
177:
178: public void setString(String name, String value)
179: throws JMSException {
180: this.contents.put(name, value);
181: }
182: }
|