Source Code Cross Referenced for SimpleMessageConverter.java in  » J2EE » spring-framework-2.5 » org » springframework » jms » support » converter » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.5 » org.springframework.jms.support.converter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.jms.support.converter;
018:
019:        import java.io.Serializable;
020:        import java.util.Enumeration;
021:        import java.util.HashMap;
022:        import java.util.Iterator;
023:        import java.util.Map;
024:
025:        import javax.jms.BytesMessage;
026:        import javax.jms.JMSException;
027:        import javax.jms.MapMessage;
028:        import javax.jms.Message;
029:        import javax.jms.ObjectMessage;
030:        import javax.jms.Session;
031:        import javax.jms.TextMessage;
032:
033:        import org.springframework.util.ObjectUtils;
034:
035:        /**
036:         * A simple message converter which is able to handle TextMessages, BytesMessages,
037:         * MapMessages, and ObjectMessages. Used as default conversion strategy
038:         * by {@link org.springframework.jms.core.JmsTemplate}, for
039:         * <code>convertAndSend</code> and <code>receiveAndConvert</code> operations.
040:         *
041:         * <p>Converts a String to a {@link javax.jms.TextMessage}, a byte array to a
042:         * {@link javax.jms.BytesMessage}, a Map to a {@link javax.jms.MapMessage}, and
043:         * a Serializable object to a {@link javax.jms.ObjectMessage} (or vice versa).
044:         *
045:         * <p>This converter implementation works for both JMS 1.1 and JMS 1.0.2,
046:         * except when extracting a byte array from a BytesMessage. So for converting
047:         * BytesMessages with a JMS 1.0.2 provider, use {@link SimpleMessageConverter102}.
048:         * (As you would expect, {@link org.springframework.jms.core.JmsTemplate102}
049:         * uses SimpleMessageConverter102 as default.)
050:         *
051:         * @author Juergen Hoeller
052:         * @since 1.1
053:         * @see org.springframework.jms.core.JmsTemplate#convertAndSend
054:         * @see org.springframework.jms.core.JmsTemplate#receiveAndConvert
055:         */
056:        public class SimpleMessageConverter implements  MessageConverter {
057:
058:            /**
059:             * This implementation creates a TextMessage for a String, a
060:             * BytesMessage for a byte array, a MapMessage for a Map,
061:             * and an ObjectMessage for a Serializable object.
062:             * @see #createMessageForString
063:             * @see #createMessageForByteArray
064:             * @see #createMessageForMap
065:             * @see #createMessageForSerializable
066:             */
067:            public Message toMessage(Object object, Session session)
068:                    throws JMSException, MessageConversionException {
069:                if (object instanceof  Message) {
070:                    return (Message) object;
071:                } else if (object instanceof  String) {
072:                    return createMessageForString((String) object, session);
073:                } else if (object instanceof  byte[]) {
074:                    return createMessageForByteArray((byte[]) object, session);
075:                } else if (object instanceof  Map) {
076:                    return createMessageForMap((Map) object, session);
077:                } else if (object instanceof  Serializable) {
078:                    return createMessageForSerializable(
079:                            ((Serializable) object), session);
080:                } else {
081:                    throw new MessageConversionException(
082:                            "Cannot convert object [" + object
083:                                    + "] to JMS message");
084:                }
085:            }
086:
087:            /**
088:             * This implementation converts a TextMessage back to a String, a
089:             * ByteMessage back to a byte array, a MapMessage back to a Map,
090:             * and an ObjectMessage back to a Serializable object. Returns
091:             * the plain Message object in case of an unknown message type.
092:             * @see #extractStringFromMessage
093:             * @see #extractByteArrayFromMessage
094:             * @see #extractMapFromMessage
095:             * @see #extractSerializableFromMessage
096:             */
097:            public Object fromMessage(Message message) throws JMSException,
098:                    MessageConversionException {
099:                if (message instanceof  TextMessage) {
100:                    return extractStringFromMessage((TextMessage) message);
101:                } else if (message instanceof  BytesMessage) {
102:                    return extractByteArrayFromMessage((BytesMessage) message);
103:                } else if (message instanceof  MapMessage) {
104:                    return extractMapFromMessage((MapMessage) message);
105:                } else if (message instanceof  ObjectMessage) {
106:                    return extractSerializableFromMessage((ObjectMessage) message);
107:                } else {
108:                    return message;
109:                }
110:            }
111:
112:            /**
113:             * Create a JMS TextMessage for the given String.
114:             * @param text the String to convert
115:             * @param session current JMS session
116:             * @return the resulting message
117:             * @throws JMSException if thrown by JMS methods
118:             * @see javax.jms.Session#createTextMessage
119:             */
120:            protected TextMessage createMessageForString(String text,
121:                    Session session) throws JMSException {
122:                return session.createTextMessage(text);
123:            }
124:
125:            /**
126:             * Create a JMS BytesMessage for the given byte array.
127:             * @param bytes the byyte array to convert
128:             * @param session current JMS session
129:             * @return the resulting message
130:             * @throws JMSException if thrown by JMS methods
131:             * @see javax.jms.Session#createBytesMessage
132:             */
133:            protected BytesMessage createMessageForByteArray(byte[] bytes,
134:                    Session session) throws JMSException {
135:                BytesMessage message = session.createBytesMessage();
136:                message.writeBytes(bytes);
137:                return message;
138:            }
139:
140:            /**
141:             * Create a JMS MapMessage for the given Map.
142:             * @param map the Map to convert
143:             * @param session current JMS session
144:             * @return the resulting message
145:             * @throws JMSException if thrown by JMS methods
146:             * @see javax.jms.Session#createMapMessage
147:             */
148:            protected MapMessage createMessageForMap(Map map, Session session)
149:                    throws JMSException {
150:                MapMessage message = session.createMapMessage();
151:                for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
152:                    Map.Entry entry = (Map.Entry) it.next();
153:                    if (!(entry.getKey() instanceof  String)) {
154:                        throw new MessageConversionException(
155:                                "Cannot convert non-String key of type ["
156:                                        + ObjectUtils.nullSafeClassName(entry
157:                                                .getKey())
158:                                        + "] to JMS MapMessage entry");
159:                    }
160:                    message
161:                            .setObject((String) entry.getKey(), entry
162:                                    .getValue());
163:                }
164:                return message;
165:            }
166:
167:            /**
168:             * Create a JMS ObjectMessage for the given Serializable object.
169:             * @param object the Serializable object to convert
170:             * @param session current JMS session
171:             * @return the resulting message
172:             * @throws JMSException if thrown by JMS methods
173:             * @see javax.jms.Session#createObjectMessage
174:             */
175:            protected ObjectMessage createMessageForSerializable(
176:                    Serializable object, Session session) throws JMSException {
177:                return session.createObjectMessage(object);
178:            }
179:
180:            /**
181:             * Extract a String from the given TextMessage.
182:             * @param message the message to convert
183:             * @return the resulting String
184:             * @throws JMSException if thrown by JMS methods
185:             */
186:            protected String extractStringFromMessage(TextMessage message)
187:                    throws JMSException {
188:                return message.getText();
189:            }
190:
191:            /**
192:             * Extract a byte array from the given {@link BytesMessage}.
193:             * @param message the message to convert
194:             * @return the resulting byte array
195:             * @throws JMSException if thrown by JMS methods
196:             */
197:            protected byte[] extractByteArrayFromMessage(BytesMessage message)
198:                    throws JMSException {
199:                byte[] bytes = new byte[(int) message.getBodyLength()];
200:                message.readBytes(bytes);
201:                return bytes;
202:            }
203:
204:            /**
205:             * Extract a Map from the given {@link MapMessage}.
206:             * @param message the message to convert
207:             * @return the resulting Map
208:             * @throws JMSException if thrown by JMS methods
209:             */
210:            protected Map extractMapFromMessage(MapMessage message)
211:                    throws JMSException {
212:                Map map = new HashMap();
213:                Enumeration en = message.getMapNames();
214:                while (en.hasMoreElements()) {
215:                    String key = (String) en.nextElement();
216:                    map.put(key, message.getObject(key));
217:                }
218:                return map;
219:            }
220:
221:            /**
222:             * Extract a Serializable object from the given {@link ObjectMessage}.
223:             * @param message the message to convert
224:             * @return the resulting Serializable object
225:             * @throws JMSException if thrown by JMS methods
226:             */
227:            protected Serializable extractSerializableFromMessage(
228:                    ObjectMessage message) throws JMSException {
229:                return message.getObject();
230:            }
231:
232:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.