KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > message > ObjectConverter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.message;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.jms.JMSException JavaDoc;
35 import javax.jms.MessageFormatException JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * A basic message.
40  */

41 public class ObjectConverter {
42   static final Logger JavaDoc log = Log.open(ObjectConverter.class);
43   static final L10N L = new L10N(ObjectConverter.class);
44   
45   /**
46    * Returns an object converted to a boolean.
47    */

48   public static boolean toBoolean(Object JavaDoc obj)
49     throws JMSException JavaDoc
50   {
51     if (obj instanceof Boolean JavaDoc)
52       return ((Boolean JavaDoc) obj).booleanValue();
53     else if (obj == null || obj instanceof String JavaDoc)
54       return Boolean.valueOf((String JavaDoc) obj).booleanValue();
55     else
56       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to boolean",
57                        obj.getClass().getName()));
58   }
59
60   /**
61    * Returns a property as a byte
62    */

63   public static byte toByte(Object JavaDoc obj)
64     throws JMSException JavaDoc
65   {
66     if (obj instanceof Byte JavaDoc)
67       return ((Number JavaDoc) obj).byteValue();
68     else if (obj == null || obj instanceof String JavaDoc)
69       return (byte) Long.parseLong((String JavaDoc) obj);
70     else
71       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to byte",
72                        obj.getClass().getName()));
73   }
74
75   /**
76    * Returns a property as a short
77    */

78   public static short toShort(Object JavaDoc obj)
79     throws JMSException JavaDoc
80   {
81     if (obj instanceof Short JavaDoc || obj instanceof Byte JavaDoc)
82       return ((Number JavaDoc) obj).shortValue();
83     else if (obj == null || obj instanceof String JavaDoc)
84       return (short) Long.parseLong((String JavaDoc) obj);
85     else
86       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to short",
87                        obj.getClass().getName()));
88   }
89
90   /**
91    * Returns a property as an integer
92    */

93   public static int toInt(Object JavaDoc obj)
94     throws JMSException JavaDoc
95   {
96     if (obj instanceof Integer JavaDoc ||
97     obj instanceof Short JavaDoc ||
98     obj instanceof Byte JavaDoc)
99       return ((Number JavaDoc) obj).intValue();
100     else if (obj == null || obj instanceof String JavaDoc)
101       return (int) Long.parseLong((String JavaDoc) obj);
102     else
103       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to int",
104                        obj.getClass().getName()));
105   }
106
107   /**
108    * Returns a property as a long
109    */

110   public static long toLong(Object JavaDoc obj)
111     throws JMSException JavaDoc
112   {
113     if (obj instanceof Long JavaDoc ||
114     obj instanceof Integer JavaDoc ||
115     obj instanceof Short JavaDoc ||
116     obj instanceof Byte JavaDoc)
117       return ((Number JavaDoc) obj).longValue();
118     else if (obj == null || obj instanceof String JavaDoc)
119       return Long.parseLong((String JavaDoc) obj);
120     else
121       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to long",
122                        obj.getClass().getName()));
123   }
124
125   /**
126    * Returns a property as a float
127    */

128   public static float toFloat(Object JavaDoc obj)
129     throws JMSException JavaDoc
130   {
131     if (obj == null || obj instanceof Float JavaDoc)
132       return ((Number JavaDoc) obj).floatValue();
133     else if (obj == null || obj instanceof String JavaDoc)
134       return (float) Double.parseDouble((String JavaDoc) obj);
135     else
136       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to float",
137                        obj.getClass().getName()));
138   }
139
140   /**
141    * Returns a property as a double
142    */

143   public static double toDouble(Object JavaDoc obj)
144     throws JMSException JavaDoc
145   {
146     if (obj == null || obj instanceof Float JavaDoc || obj instanceof Double JavaDoc)
147       return ((Number JavaDoc) obj).doubleValue();
148     else if (obj == null || obj instanceof String JavaDoc)
149       return Double.parseDouble((String JavaDoc) obj);
150     else
151       throw new MessageFormatException JavaDoc(L.l("can't convert '{0}' to double",
152                        obj.getClass().getName()));
153   }
154
155   /**
156    * Returns a property as a byte[]
157    */

158   public static String JavaDoc toString(Object JavaDoc obj)
159     throws JMSException JavaDoc
160   {
161     if (obj == null)
162       return null;
163     else
164       return obj.toString();
165   }
166
167   /**
168    * Returns a property as a char
169    */

170   public static char toChar(Object JavaDoc obj)
171     throws JMSException JavaDoc
172   {
173     if (obj == null)
174       throw new NullPointerException JavaDoc();
175     else if (obj instanceof Character JavaDoc)
176       return ((Character JavaDoc) obj).charValue();
177     else if (obj instanceof String JavaDoc)
178       return ((String JavaDoc) obj).charAt(0);
179     else
180       throw new MessageFormatException JavaDoc(L.l("bad property {0}", obj));
181     
182   }
183
184   /**
185    * Returns a property as a byte[]
186    */

187   public static byte []toBytes(Object JavaDoc obj)
188     throws JMSException JavaDoc
189   {
190     if (obj == null)
191       return null;
192     else if (obj instanceof byte[]) {
193       byte []bytes = (byte []) obj;
194       byte []newBytes = new byte[bytes.length];
195       System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
196       
197       return newBytes;
198     }
199     else if (obj instanceof String JavaDoc) {
200       String JavaDoc string = toString(obj);
201       try {
202     return string.getBytes("UTF-8");
203       } catch (Exception JavaDoc e) {
204     throw new MessageFormatException JavaDoc(e.toString());
205       }
206     }
207     else
208       throw new MessageFormatException JavaDoc(L.l("can't convert {0} to byte[]",
209                        obj.getClass().getName()));
210   }
211 }
212
Popular Tags