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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jms.message;
030:
031: import java.io.*;
032: import java.util.logging.*;
033:
034: import javax.jms.*;
035:
036: import com.caucho.vfs.*;
037:
038: /**
039: * A message factory
040: */
041: public class MessageFactory {
042: private static final Logger log = Logger
043: .getLogger(MessageFactory.class.getName());
044:
045: private static final MessageType[] MESSAGE_TYPE = MessageType
046: .values();
047:
048: /**
049: * Creates a new JMS text message.
050: */
051: public TextMessage createTextMessage() {
052: return new TextMessageImpl();
053: }
054:
055: /**
056: * Creates a new JMS text message.
057: *
058: * @param msg initial message text
059: */
060: public TextMessage createTextMessage(String msg)
061: throws JMSException {
062: TextMessage textMessage = new TextMessageImpl();
063:
064: textMessage.setText(msg);
065:
066: return textMessage;
067: }
068:
069: /**
070: * Returns the message type.
071: */
072: public MessageType getMessageType(Message msg) {
073: if (msg instanceof TextMessage)
074: return MessageType.TEXT;
075: else if (msg instanceof BytesMessage)
076: return MessageType.BYTES;
077: else if (msg instanceof MapMessage)
078: return MessageType.MAP;
079: else if (msg instanceof ObjectMessage)
080: return MessageType.OBJECT;
081: else if (msg instanceof StreamMessage)
082: return MessageType.STREAM;
083: else
084: return MessageType.NULL;
085: }
086:
087: /**
088: * Creates a message based on the type.
089: */
090: public MessageImpl createMessage(int type) {
091: if (type < 0 && MESSAGE_TYPE.length <= type)
092: return null;
093:
094: switch (MESSAGE_TYPE[type]) {
095: case NULL:
096: return new MessageImpl();
097:
098: case BYTES:
099: return new BytesMessageImpl();
100:
101: case MAP:
102: return new MapMessageImpl();
103:
104: case OBJECT:
105: return new ObjectMessageImpl();
106:
107: case STREAM:
108: return new StreamMessageImpl();
109:
110: case TEXT:
111: return new TextMessageImpl();
112:
113: default:
114: return new MessageImpl();
115: }
116: }
117:
118: /**
119: * Copy the message.
120: */
121: public MessageImpl copy(Message msg) throws JMSException {
122: if (msg instanceof MessageImpl)
123: return ((MessageImpl) msg).copy();
124: else if (msg instanceof TextMessage) {
125: return new TextMessageImpl((TextMessage) msg);
126: } else if (msg instanceof MapMessage) {
127: return new MapMessageImpl((MapMessage) msg);
128: } else if (msg instanceof BytesMessage) {
129: return new BytesMessageImpl((BytesMessage) msg);
130: } else if (msg instanceof StreamMessage) {
131: return new StreamMessageImpl((StreamMessage) msg);
132: } else if (msg instanceof ObjectMessage) {
133: return new ObjectMessageImpl((ObjectMessage) msg);
134: } else if (msg != null)
135: return new MessageImpl(msg);
136: else
137: throw new NullPointerException();
138: }
139:
140: /**
141: * Creates an input stream from the header.
142: */
143: public InputStream headerToInputStream(Message msg) {
144: try {
145: TempStream ts = new TempStream();
146: ts.openWrite();
147: WriteStream out = new WriteStream(ts);
148:
149: writeHeader(out, msg);
150:
151: out.close();
152:
153: return ts.openRead();
154: } catch (IOException e) {
155: throw new RuntimeException(e);
156: }
157: }
158:
159: public void writeHeader(WriteStream out, Message msg) {
160: }
161: }
|