com/scalagent/kjoram/MapMessage.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - ScalAgent Distributed Technologies
00004  * Copyright (C) 1996 - Dyade
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA.
00020  *
00021  * Initial developer(s): Frederic Maistre (INRIA)
00022  * Contributor(s): Nicolas Tachker (ScalAgent)
00023  */
00024 package com.scalagent.kjoram;
00025 
00026 import com.scalagent.kjoram.excepts.*;
00027 import com.scalagent.kjoram.messages.ConversionHelper;
00028 
00029 import java.util.*;
00030 
00031 public class MapMessage extends Message
00032 {
00034   private Hashtable map;
00036   private boolean RObody = false;
00037 
00038 
00042   MapMessage()
00043   {
00044     super();
00045     map = new Hashtable();
00046   }
00047 
00058   MapMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg)
00059   throws MessageFormatException
00060   {
00061     super(sess, momMsg);
00062     try {
00063       map = momMsg.getMap();
00064     }
00065     catch (Exception exc) {
00066       MessageFormatException jE =
00067         new MessageFormatException("Error while getting the body.");
00068       jE.setLinkedException(exc);
00069       throw jE;
00070     }
00071     RObody = true;
00072   }
00073   
00074 
00080   public void clearBody() throws JMSException
00081   {
00082     super.clearBody();
00083     map.clear();
00084     RObody = false;
00085   }
00086 
00087 
00093   public void setBoolean(String name, boolean value) throws JMSException
00094   {
00095     setObject(name, new Boolean(value));
00096   }
00097  
00103   public void setByte(String name, byte value) throws JMSException
00104   {
00105     setObject(name, new Byte(value));
00106   }
00107  
00113   public void setBytes(String name, byte[] value) throws JMSException
00114   {
00115     setObject(name, value);
00116   }
00117  
00123   public void setBytes(String name, byte[] value, int offset, int length)
00124               throws JMSException
00125   {
00126     byte[] buff = new byte[length];
00127 
00128     for (int i = 0; i < length; i++)
00129       buff[i] = value[i + offset];
00130 
00131     setObject(name, buff);
00132   }
00133  
00139   public void setChar(String name, char value) throws JMSException
00140   {
00141     setObject(name, new Character(value));
00142   }
00143  
00149 //    public void setDouble(String name, double value) throws JMSException
00150 //    {
00151 //      setObject(name, new Double(value));
00152 //    }
00153  
00159 //    public void setFloat(String name, float value) throws JMSException
00160 //    {
00161 //      setObject(name, new Float(value));
00162 //    }
00163  
00169   public void setInt(String name, int value) throws JMSException
00170   {
00171     setObject(name, new Integer(value));
00172   }
00173  
00179   public void setLong(String name, long value) throws JMSException
00180   {
00181     setObject(name, new Long(value));
00182   }
00183   
00189   public void setShort(String name, short value) throws JMSException
00190   {
00191     setObject(name, new Short(value));
00192   }
00193  
00199   public void setString(String name, String value) throws JMSException
00200   {
00201     setObject(name, value);
00202   }
00203 
00210   public void setObject(String name, Object value) throws JMSException
00211   {
00212     if (RObody)
00213       throw new MessageNotWriteableException("Can't set a value as the message"
00214                                              + " body is read-only.");
00215     if (name == null || name.equals(""))
00216       throw new IllegalArgumentException("Invalid null or empty value name.");
00217 
00218     if (value == null)
00219       return;
00220 
00221     if (value instanceof Boolean || 
00222         value instanceof Character
00223         || value instanceof Long
00224         || value instanceof Short
00225         || value instanceof Byte
00226         || value instanceof Integer 
00227         || value instanceof String
00228         || value instanceof byte[])
00229       map.put(name, value);
00230     else
00231       throw new MessageFormatException("Can't set non Java primitive type as"
00232                                        + " a map value.");
00233   }
00234 
00240   public boolean getBoolean(String name) throws JMSException
00241   {
00242     try {
00243       return ConversionHelper.toBoolean(map.get(name));
00244     }
00245     catch (MessageValueException mE) {
00246       throw new MessageFormatException(mE.getMessage());
00247     }
00248   }
00249 
00255   public byte getByte(String name) throws JMSException
00256   {
00257     try {
00258       return ConversionHelper.toByte(map.get(name));
00259     }
00260     catch (MessageValueException mE) {
00261       throw new MessageFormatException(mE.getMessage());
00262     }
00263   }
00264 
00270   public byte[] getBytes(String name) throws JMSException
00271   {
00272     try {
00273       return ConversionHelper.toBytes(map.get(name));
00274     }
00275     catch (MessageValueException mE) {
00276       throw new MessageFormatException(mE.getMessage());
00277     }
00278   }
00279 
00285   public char getChar(String name) throws JMSException
00286   {
00287     try {
00288       return ConversionHelper.toChar(map.get(name));
00289     }
00290     catch (MessageValueException mE) {
00291       throw new MessageFormatException(mE.getMessage());
00292     }
00293   }
00294  
00300 //    public double getDouble(String name) throws JMSException
00301 //    {
00302 //      try {
00303 //        return ConversionHelper.toDouble(map.get(name));
00304 //      }
00305 //      catch (MessageValueException mE) {
00306 //        throw new MessageFormatException(mE.getMessage());
00307 //      }
00308 //    }
00309 
00315 //    public float getFloat(String name) throws JMSException
00316 //    {
00317 //      try {
00318 //        return ConversionHelper.toFloat(map.get(name));
00319 //      }
00320 //      catch (MessageValueException mE) {
00321 //        throw new MessageFormatException(mE.getMessage());
00322 //      }
00323 //    }
00324 
00330   public int getInt(String name) throws JMSException
00331   {
00332     try {
00333       return ConversionHelper.toInt(map.get(name));
00334     }
00335     catch (MessageValueException mE) {
00336       throw new MessageFormatException(mE.getMessage());
00337     }
00338   }
00339 
00345   public long getLong(String name) throws JMSException
00346   {
00347     try {
00348       return ConversionHelper.toLong(map.get(name));
00349     }
00350     catch (MessageValueException mE) {
00351       throw new MessageFormatException(mE.getMessage());
00352     }
00353   }
00354  
00360   public Object getObject(String name) throws JMSException
00361   {
00362     return map.get(name);
00363   }
00364 
00370   public short getShort(String name) throws JMSException
00371   {
00372     try {
00373       return ConversionHelper.toShort(map.get(name));
00374     }
00375     catch (MessageValueException mE) {
00376       throw new MessageFormatException(mE.getMessage());
00377     }
00378   }
00379 
00385   public String getString(String name) throws JMSException
00386   {
00387     return ConversionHelper.toString(map.get(name));
00388   }
00389 
00395   public boolean itemExists(String name) throws JMSException
00396   {
00397     return (map.get(name) != null);
00398   }
00399 
00405   public Enumeration getMapNames() throws JMSException
00406   {
00407     return map.keys();
00408   }
00409  
00410 
00417   protected void prepare() throws Exception
00418   {
00419     super.prepare();
00420 
00421     momMsg.clearBody(); 
00422     momMsg.setMap(map);
00423   }
00424 }

Generated on Tue Sep 16 16:14:24 2008 for joram by  doxygen 1.5.0