1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: MessageFactory.java,v 1.2 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import javax.jms.BytesMessage;
48 import javax.jms.JMSException;
49 import javax.jms.MapMessage;
50 import javax.jms.Message;
51 import javax.jms.ObjectMessage;
52 import javax.jms.StreamMessage;
53 import javax.jms.TextMessage;
54
55
56 /***
57 * A factory for creating, and optionally populating messages
58 *
59 * @version $Revision: 1.2 $ $Date: 2004/01/31 13:44:24 $
60 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
61 * @see MessagePopulator
62 */
63 public abstract class MessageFactory {
64
65 /***
66 * The message populator, or null if messages aren't to be populated
67 */
68 private final MessagePopulator _populator;
69
70
71 /***
72 * Construct a new <code>MessageFactory</code>
73 */
74 public MessageFactory() {
75 this(null);
76 }
77
78 /***
79 * Construct a new <code>MessageFactory</code>
80 *
81 * @param populator the message populator, or null, if messages aren't to
82 * be populated
83 */
84 public MessageFactory(MessagePopulator populator) {
85 _populator = populator;
86 }
87
88 /***
89 * Create a message of the specified type. If a populator was supplied at
90 * construction, it will be used to populate the message with data.
91 *
92 * @param type the JMS message class type
93 * @return the new message
94 * @throws Exception for any error
95 */
96 public Message create(Class type) throws Exception {
97 Message message = null;
98 if (type.equals(BytesMessage.class)) {
99 message = createBytesMessage();
100 } else if (type.equals(MapMessage.class)) {
101 message = createMapMessage();
102 } else if (type.equals(ObjectMessage.class)) {
103 message = createObjectMessage();
104 } else if (type.equals(StreamMessage.class)) {
105 message = createStreamMessage();
106 } else if (type.equals(TextMessage.class)) {
107 message = createTextMessage();
108 } else if (type.equals(Message.class)) {
109 message = createMessage();
110 } else {
111 throw new IllegalArgumentException(
112 "Argument type=" + type + " is not a valid JMS Message type");
113 }
114
115 if (_populator != null) {
116 populate(message);
117 }
118 return message;
119 }
120
121 /***
122 * Create a new <code>BytesMessage</code>
123 *
124 * @return the new message
125 * @throws JMSException for any error
126 */
127 protected abstract BytesMessage createBytesMessage() throws JMSException;
128
129 /***
130 * Create a new <code>MapMessage</code>
131 *
132 * @return the new message
133 * @throws JMSException for any error
134 */
135 protected abstract MapMessage createMapMessage() throws JMSException;
136
137 /***
138 * Create a new <code>Message</code>
139 *
140 * @return the new message
141 * @throws JMSException for any error
142 */
143 protected abstract Message createMessage() throws JMSException;
144
145 /***
146 * Create a new <code>ObjectMessage</code>
147 *
148 * @return the new message
149 * @throws JMSException for any error
150 */
151 protected abstract ObjectMessage createObjectMessage() throws JMSException;
152
153 /***
154 * Create a new <code>StreamMessage</code>
155 *
156 * @return the new message
157 * @throws JMSException for any error
158 */
159 protected abstract StreamMessage createStreamMessage() throws JMSException;
160
161 /***
162 * Create a new <code>TextMessage</code>
163 *
164 * @return the new message
165 * @throws JMSException for any error
166 */
167 protected abstract TextMessage createTextMessage() throws JMSException;
168
169 /***
170 * Populate a message
171 *
172 * @param message the message to populate
173 * @throws Exception for any error
174 */
175 protected void populate(Message message) throws Exception {
176 _populator.populate(message);
177 }
178
179 }
This page was automatically generated by Maven