//
// JmsaMapMessage.java - JMS Adapter MapMessage
//

package nyangau.jmsadapter;

import java.util.Enumeration;
import java.util.Collections;
import java.util.HashMap;
import java.lang.System;
import javax.jms.*;

public class JmsaMapMessage extends JmsaMessage implements MapMessage
  {
  protected HashMap<String,Object> map;

//...scantConvertItem:2:
protected static String cantConvertItem(String name, Object o, String to)
  { return cantConvert("item " + name + " in MapMessage", o, to); }
//...e
//...scheckSetItem:2:
protected void checkSetItem(String name)
  throws JMSException
  {
  checkWrite();
  if ( name == null || name.length() == 0 )
    throw new IllegalArgumentException("Null or empty MapMessage item name");
  }
//...e

//...sBoolean:2:
// from Boolean, String
 
public boolean getBoolean(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Boolean.valueOf(null).booleanValue();
  if ( o instanceof Boolean )
    return ((Boolean)o).booleanValue();
  if ( o instanceof String )
    return Boolean.valueOf((String)o).booleanValue();
  throw new MessageFormatException(cantConvertItem(name, o, "boolean"));
  }

public void setBoolean(String name, boolean value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Boolean(value));
  }
//...e
//...sChar:2:
// from Character

public char getChar(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    throw new NullPointerException();
  if ( o instanceof Character )
    return ((Character)o).charValue();
  throw new MessageFormatException(cantConvertItem(name, o, "char"));
  }

public void setChar(String name, char value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Character(value));
  }
//...e
//...sByte:2:
// from Byte, String

public byte getByte(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Byte.parseByte(null);
  if ( o instanceof Byte )
    return ((Byte)o).byteValue();
  if ( o instanceof String )
    return Byte.parseByte((String)o);
  throw new MessageFormatException(cantConvertItem(name, o, "byte"));
  }

public void setByte(String name, byte value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Byte(value));
  }
//...e
//...sShort:2:
// from Short, String, Byte

public short getShort(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Short.parseShort(null);
  if ( o instanceof Short )
    return ((Short)o).shortValue();
  if ( o instanceof String )
    return Short.parseShort((String)o);
  if ( o instanceof Byte )
    return ((Byte)o).shortValue();
  throw new MessageFormatException(cantConvertItem(name, o, "short"));
  }

public void setShort(String name, short value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Short(value));
  }
//...e
//...sInt:2:
// from Integer, String, Short, Byte

public int getInt(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Integer.parseInt(null);
  if ( o instanceof Integer )
    return ((Integer)o).intValue();
  if ( o instanceof String )
    return Integer.parseInt((String)o);
  if ( o instanceof Short )
    return ((Short)o).intValue();
  if ( o instanceof Byte )
    return ((Byte)o).intValue();
  throw new MessageFormatException(cantConvertItem(name, o, "int"));
  }

public void setInt(String name, int value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Integer(value));
  }
//...e
//...sLong:2:
// from Long, String, Integer, Short, Byte

public long getLong(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Long.parseLong(null);
  if ( o instanceof Long )
    return ((Long)o).longValue();
  if ( o instanceof String )
    return Long.parseLong((String)o);
  if ( o instanceof Integer )
    return ((Integer)o).longValue();
  if ( o instanceof Short )
    return ((Short)o).longValue();
  if ( o instanceof Byte )
    return ((Byte)o).longValue();
  throw new MessageFormatException(cantConvertItem(name, o, "long"));
  }

public void setLong(String name, long value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Long(value));
  }
//...e
//...sFloat:2:
// from Float, String

public float getFloat(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Float.parseFloat(null);
  if ( o instanceof Float )
    return ((Float)o).floatValue();
  if ( o instanceof String )
    return Float.parseFloat((String)o);
  throw new MessageFormatException(cantConvertItem(name, o, "float"));
  }

public void setFloat(String name, float value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Float(value));
  }
//...e
//...sDouble:2:
// from Double, String, Float

public double getDouble(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return Double.parseDouble(null);
  if ( o instanceof Double )
    return ((Double)o).doubleValue();
  if ( o instanceof String )
    return Double.parseDouble((String)o);
  if ( o instanceof Float )
    return ((Float)o).doubleValue();
  throw new MessageFormatException(cantConvertItem(name, o, "double"));
  }

public void setDouble(String name, double value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, new Double(value));
  }
//...e
//...sString:2:
// from String, and rest via toString()

public String getString(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return null;
  if ( o instanceof String )
    return (String) o;
  return o.toString();
  }

public void setString(String name, String value)
  throws JMSException
  {
  checkSetItem(name);
  map.put(name, value);
  }
//...e
//...sBytes:2:
// from byte[] only

public byte[] getBytes(String name)
  throws JMSException
  {
  Object o = map.get(name);
  if ( o == null )
    return null;
  if ( o instanceof byte[] )
    return (byte[]) o;
  throw new MessageFormatException(cantConvertItem(name, o, "byte[]"));
  }

public void setBytes(String name, byte[] value)
  throws JMSException
  { setBytes(name, value, 0, value.length); }

public void setBytes(String name, byte[] value, int offset, int length)
  throws JMSException
  {
  checkSetItem(name);
  byte[] b = new byte[length];
  System.arraycopy(value, offset, b, 0, length);
  map.put(name, b);
  }
//...e
//...sObject:2:
public Object getObject(String name)
  throws JMSException
  {
  return map.get(name);
  }

public void setObject(String name, Object value)
  throws JMSException
  {
  checkSetItem(name);
  if ( value instanceof Boolean   ||
       value instanceof Character ||
       value instanceof Byte      ||
       value instanceof Short     ||
       value instanceof Integer   ||
       value instanceof Long      ||
       value instanceof Float     ||
       value instanceof Double    ||
       value instanceof String    ||
       value instanceof byte[]    )
    map.put(name, value);
  else
    throw new MessageFormatException(" value is of invalid class: " + value.getClass().getName());
  }
//...e

//...sgetMapNames:2:
public Enumeration getMapNames()
  throws JMSException
  { return Collections.enumeration( map.keySet() ); }
//...e
//...sitemExists:2:
public boolean itemExists(String name)
  throws JMSException
  { return map.containsKey(name); }
//...e

//...sclearBody:2:
public void clearBody()
  throws JMSException
  {
  map.clear();
  bro = false;
  }
//...e
//...sclearItem:2:
// Not a part of JMS javax.jms.MapMessage interface
// Exists to support <bodydel> in flow processor

public void clearItem(String name)
  { map.remove(name); }
//...e
//...sJmsaMapMessage:2:
// Called by JmsaSession
public JmsaMapMessage()
  {
  super();
  map = new HashMap<String,Object>();
  }

// For newly received messages, or foreign outbound messages
public JmsaMapMessage(MapMessage m)
  throws JMSException // caused nby super(m), m.getMapNames() and m.getObject()
  {
  super(m);
  map = new HashMap<String,Object>();
  for ( Enumeration e = m.getMapNames(); e.hasMoreElements(); )
    {
    String mn = (String) e.nextElement();
    map.put(mn, m.getObject(mn));
    }
  }

// For converting between message formats
public JmsaMapMessage(JmsaMessage m)
  {
  super(m);
  map = new HashMap<String,Object>();
  }
//...e
  }
