01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * 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.jca;
30:
31: import com.caucho.log.Log;
32: import com.caucho.services.message.MessageSender;
33: import com.caucho.services.message.MessageServiceException;
34: import com.caucho.util.L10N;
35:
36: import javax.jms.Message;
37: import javax.jms.ObjectMessage;
38: import javax.resource.spi.ConnectionManager;
39: import java.util.HashMap;
40: import java.util.logging.Logger;
41:
42: /**
43: * The managed factory implementation.
44: */
45: class MessageSenderImpl implements MessageSender {
46: protected static final Logger log = Log
47: .open(MessageSenderImpl.class);
48: private static final L10N L = new L10N(MessageSenderImpl.class);
49:
50: private MessageSenderManager _manager;
51: private ConnectionManager _cm;
52:
53: MessageSenderImpl(MessageSenderManager manager, ConnectionManager cm) {
54: _manager = manager;
55: _cm = cm;
56: }
57:
58: public void send(HashMap header, Object value)
59: throws MessageServiceException {
60: ManagedSessionImpl session = null;
61:
62: try {
63: session = (ManagedSessionImpl) _cm.allocateConnection(
64: _manager, null);
65:
66: Message message;
67:
68: if (value == null) {
69: message = session.getSession().createMessage();
70: } else if (value instanceof String) {
71: message = session.getSession().createTextMessage(
72: (String) value);
73: } else if (value instanceof java.io.Serializable) {
74: ObjectMessage objMessage = session.getSession()
75: .createObjectMessage();
76: objMessage.setObject((java.io.Serializable) value);
77: message = objMessage;
78: } else {
79: throw new MessageServiceException(L.l(
80: "value '{0}' must be serializable", value));
81: }
82:
83: session.send(message);
84: } catch (MessageServiceException e) {
85: throw e;
86: } catch (Exception e) {
87: e.printStackTrace();
88: throw new MessageServiceException(e);
89: } finally {
90: if (session != null)
91: session.close();
92: }
93: }
94: }
|