001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jms.message;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import javax.jms.JMSException;
036: import javax.jms.MessageFormatException;
037: import java.util.logging.Logger;
038:
039: /**
040: * A basic message.
041: */
042: public class ObjectConverter {
043: static final Logger log = Logger.getLogger(ObjectConverter.class
044: .getName());
045: static final L10N L = new L10N(ObjectConverter.class);
046:
047: /**
048: * Returns an object converted to a boolean.
049: */
050: public static boolean toBoolean(Object obj) throws JMSException {
051: if (obj instanceof Boolean)
052: return ((Boolean) obj).booleanValue();
053: else if (obj == null || obj instanceof String)
054: return Boolean.valueOf((String) obj).booleanValue();
055: else
056: throw new MessageFormatException(L.l(
057: "can't convert '{0}' to boolean", obj.getClass()
058: .getName()));
059: }
060:
061: /**
062: * Returns a property as a byte
063: */
064: public static byte toByte(Object obj) throws JMSException {
065: // jms/2252
066: if (obj instanceof Byte)
067: return ((Number) obj).byteValue();
068: else if (obj == null || obj instanceof String)
069: return (byte) Long.parseLong((String) obj);
070: else
071: throw new MessageFormatException(L.l(
072: "can't convert '{0}' to byte", obj.getClass()
073: .getName()));
074: }
075:
076: /**
077: * Returns a property as a short
078: */
079: public static short toShort(Object obj) throws JMSException {
080: // jms/2252
081: if (obj instanceof Short || obj instanceof Byte)
082: return ((Number) obj).shortValue();
083: else if (obj == null || obj instanceof String)
084: return (short) Long.parseLong((String) obj);
085: else
086: throw new MessageFormatException(L.l(
087: "can't convert '{0}' to short", obj.getClass()
088: .getName()));
089: }
090:
091: /**
092: * Returns a property as an integer
093: */
094: public static int toInt(Object obj) throws JMSException {
095: if (obj instanceof Integer || obj instanceof Short
096: || obj instanceof Byte)
097: return ((Number) obj).intValue();
098: else if (obj == null || obj instanceof String)
099: return (int) Long.parseLong((String) obj);
100: else
101: throw new MessageFormatException(L.l(
102: "can't convert '{0}' to int", obj.getClass()
103: .getName()));
104: }
105:
106: /**
107: * Returns a property as a long
108: */
109: public static long toLong(Object obj) throws JMSException {
110: if (obj instanceof Long || obj instanceof Integer
111: || obj instanceof Short || obj instanceof Byte)
112: return ((Number) obj).longValue();
113: else if (obj == null || obj instanceof String)
114: return Long.parseLong((String) obj);
115: else
116: throw new MessageFormatException(L.l(
117: "can't convert '{0}' to long", obj.getClass()
118: .getName()));
119: }
120:
121: /**
122: * Returns a property as a float
123: */
124: public static float toFloat(Object obj) throws JMSException {
125: if (obj == null || obj instanceof Float)
126: return ((Number) obj).floatValue();
127: else if (obj == null || obj instanceof String)
128: return (float) Double.parseDouble((String) obj);
129: else
130: throw new MessageFormatException(L.l(
131: "can't convert '{0}' to float", obj.getClass()
132: .getName()));
133: }
134:
135: /**
136: * Returns a property as a double
137: */
138: public static double toDouble(Object obj) throws JMSException {
139: if (obj == null || obj instanceof Float
140: || obj instanceof Double)
141: return ((Number) obj).doubleValue();
142: else if (obj == null || obj instanceof String)
143: return Double.parseDouble((String) obj);
144: else
145: throw new MessageFormatException(L.l(
146: "can't convert '{0}' to double", obj.getClass()
147: .getName()));
148: }
149:
150: /**
151: * Returns a property as a string
152: */
153: public static String toString(Object obj) throws JMSException {
154: if (obj == null)
155: return null;
156: else if (!(obj instanceof byte[]))
157: return obj.toString();
158: else
159: throw new MessageFormatException(L.l(
160: "can't convert '{0}' to String", obj.getClass()
161: .getName()));
162: }
163:
164: /**
165: * Returns a property as a char
166: */
167: public static char toChar(Object obj) throws JMSException {
168: if (obj == null)
169: throw new NullPointerException();
170: else if (obj instanceof Character)
171: return ((Character) obj).charValue();
172: else if (obj instanceof String) {
173: String s = (String) obj;
174:
175: if (s.length() != 1)
176: throw new MessageFormatException(L.l(
177: "bad property {0}", obj));
178:
179: return s.charAt(0);
180: } else
181: throw new MessageFormatException(L.l("bad property {0}",
182: obj));
183:
184: }
185:
186: /**
187: * Returns a property as a byte[]
188: */
189: public static byte[] toBytes(Object obj) throws JMSException {
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: /*
200: else if (obj instanceof String) {
201: String string = toString(obj);
202: try {
203: return string.getBytes("UTF-8");
204: } catch (Exception e) {
205: throw new MessageFormatException(e.toString());
206: }
207: }
208: */
209: else
210: throw new MessageFormatException(L.l(
211: "can't convert {0} to byte[]", obj.getClass()
212: .getName()));
213: }
214: }
|