/*
* 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.
*/
using System;
using System.Collections;
using Apache.NMS;
using Apache.NMS.Util;
using Apache.NMS.XMS.Util;
using IBM.XMS;
namespace Apache.NMS.XMS
{
///
/// Represents a map message which contains key and value pairs which are
/// of primitive types.
///
class MapMessage : Apache.NMS.XMS.Message, Apache.NMS.IMapMessage,
Apache.NMS.IPrimitiveMap
{
#region Constructors and access to internal map message
///
/// Internal IBM XMS map message.
///
public IBM.XMS.IMapMessage xmsMapMessage
{
get { return (IBM.XMS.IMapMessage)(this.xmsMessage); }
set { this.xmsMessage = value; }
}
///
/// Constructs a MapMessage object.
///
/// XMS map message.
public MapMessage(IBM.XMS.IMapMessage message)
: base(message)
{
}
#endregion
#region IMapMessage Members
public Apache.NMS.IPrimitiveMap Body
{
get { return this; }
}
#endregion
#region IPrimitiveMap Members
#region General methods
///
/// Clears the contents of the message body.
///
public void Clear()
{
try
{
this.ReadOnlyBody = false;
this.xmsMapMessage.ClearBody();
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
///
/// Checks if the body contains the specified item.
///
/// Item key.
public bool Contains(object key)
{
try
{
return this.xmsMapMessage.ItemExists(key.ToString());
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return false;
}
}
///
/// Removes an item from the map message body.
///
/// Item key.
public void Remove(object key)
{
try
{
// Best guess at equivalent implementation.
this.xmsMapMessage.SetObject(key.ToString(), null);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
///
/// Count of key/value pairs in the message body.
///
public int Count
{
get
{
int count = 0;
try
{
IEnumerator mapNames = this.xmsMapMessage.MapNames;
while(mapNames.MoveNext())
{
count++;
}
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
return count;
}
}
///
/// The collection of keys in the mep message body.
///
public ICollection Keys
{
get
{
ArrayList keys = new ArrayList();
try
{
IEnumerator mapNames = this.xmsMapMessage.MapNames;
while(mapNames.MoveNext())
{
keys.Add(mapNames.Current);
}
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
return keys;
}
}
///
/// The collection of values in the mep message body.
///
public ICollection Values
{
get
{
ArrayList values = new ArrayList();
try
{
IEnumerator mapNames = this.xmsMapMessage.MapNames;
while(mapNames.MoveNext())
{
string key = (string)mapNames.Current;
values.Add(this.xmsMapMessage.GetObject(key));
}
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
return values;
}
}
///
/// Accesses an item by its key.
///
/// Item key.
public object this[string key]
{
get
{
try
{
return this.xmsMapMessage.GetObject(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return null;
}
}
set
{
try
{
this.xmsMapMessage.SetObject(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
}
#endregion
#region String items
///
/// Gets the value of a string item.
///
/// Item key.
/// Item value.
public string GetString(string key)
{
try
{
return this.xmsMapMessage.GetString(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return null;
}
}
///
/// Sets the value of a string item.
///
/// Item key.
/// Item value.
public void SetString(string key, string value)
{
try
{
this.xmsMapMessage.SetString(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Boolean items
///
/// Gets the value of a bool item.
///
/// Item key.
/// Item value.
public bool GetBool(string key)
{
try
{
return this.xmsMapMessage.GetBoolean(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return false;
}
}
///
/// Sets the value of a bool item.
///
/// Item key.
/// Item value.
public void SetBool(string key, bool value)
{
try
{
this.xmsMapMessage.SetBoolean(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Byte items
///
/// Gets the value of a byte item.
///
/// Item key.
/// Item value.
public byte GetByte(string key)
{
try
{
return this.xmsMapMessage.GetByte(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return 0;
}
}
///
/// Sets the value of a byte item.
///
/// Item key.
/// Item value.
public void SetByte(string key, byte value)
{
try
{
this.xmsMapMessage.SetByte(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Char items
///
/// Gets the value of a char item.
///
/// Item key.
/// Item value.
public char GetChar(string key)
{
try
{
return this.xmsMapMessage.GetChar(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return (char) 0;
}
}
///
/// Sets the value of a char item.
///
/// Item key.
/// Item value.
public void SetChar(string key, char value)
{
try
{
this.xmsMapMessage.SetChar(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Short items
///
/// Gets the value of a 16 bits short integer item.
///
/// Item key.
/// Item value.
public short GetShort(string key)
{
try
{
return this.xmsMapMessage.GetShort(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return 0;
}
}
///
/// Sets the value of a 16 bits short integer item.
///
/// Item key.
/// Item value.
public void SetShort(string key, short value)
{
try
{
this.xmsMapMessage.SetShort(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Int items
///
/// Gets the value of a 32 bits int integer item.
///
/// Item key.
/// Item value.
public int GetInt(string key)
{
try
{
return this.xmsMapMessage.GetInt(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return 0;
}
}
///
/// Sets the value of a 32 bits int integer item.
///
/// Item key.
/// Item value.
public void SetInt(string key, int value)
{
try
{
this.xmsMapMessage.SetInt(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Long items
///
/// Gets the value of a 64 bits long integer item.
///
/// Item key.
/// Item value.
public long GetLong(string key)
{
try
{
return this.xmsMapMessage.GetLong(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return 0;
}
}
///
/// Sets the value of a 64 bits long integer item.
///
/// Item key.
/// Item value.
public void SetLong(string key, long value)
{
try
{
this.xmsMapMessage.SetLong(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Float items
///
/// Gets the value of a float item.
///
/// Item key.
/// Item value.
public float GetFloat(string key)
{
try
{
return this.xmsMapMessage.GetFloat(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return 0;
}
}
///
/// Sets the value of a float item.
///
/// Item key.
/// Item value.
public void SetFloat(string key, float value)
{
try
{
this.xmsMapMessage.SetFloat(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Double items
///
/// Gets the value of a double item.
///
/// Item key.
/// Item value.
public double GetDouble(string key)
{
try
{
return this.xmsMapMessage.GetDouble(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return 0;
}
}
///
/// Sets the value of a double item.
///
/// Item key.
/// Item value.
public void SetDouble(string key, double value)
{
try
{
this.xmsMapMessage.SetDouble(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region List items
///
/// Gets the value of an IList item.
///
/// Item key.
/// Item value.
public IList GetList(string key)
{
try
{
return (IList) this.xmsMapMessage.GetObject(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return null;
}
}
///
/// Sets the value of an IList item.
///
/// Item key.
/// Item value.
public void SetList(string key, IList list)
{
try
{
this.xmsMapMessage.SetObject(key, list);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Bytes array items
///
/// Gets the value of a byte[] byte array item.
///
/// Item key.
/// Item value.
public byte[] GetBytes(string key)
{
try
{
return this.xmsMapMessage.GetBytes(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return null;
}
}
///
/// Sets the value of a byte[] byte array item.
///
/// Item key.
/// Item value.
public void SetBytes(string key, byte[] value)
{
try
{
this.xmsMapMessage.SetBytes(key, value);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
///
/// Sets the value of a byte[] byte array item.
///
/// Item key.
/// Byte array from which value is extracted.
/// Index of first byte to extract.
/// Number of bytes to extract.
public void SetBytes(string key, byte[] value, int offset, int length)
{
try
{
this.xmsMapMessage.SetBytes(key, value, offset, length);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#region Dictionary items
///
/// Gets the value of an IDictionary item.
///
/// Item key.
/// Item value.
public IDictionary GetDictionary(string key)
{
try
{
return (IDictionary) this.xmsMapMessage.GetObject(key);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
return null;
}
}
///
/// Sets the value of an IDictionary item.
///
/// Item key.
/// Item value.
public void SetDictionary(string key, IDictionary dictionary)
{
try
{
this.xmsMapMessage.SetObject(key, dictionary);
}
catch(Exception ex)
{
ExceptionUtil.WrapAndThrowNMSException(ex);
}
}
#endregion
#endregion
}
}