KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > jca > MessageSenderImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * 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 JavaDoc;
37 import javax.jms.ObjectMessage JavaDoc;
38 import javax.resource.spi.ConnectionManager JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * The managed factory implementation.
44  */

45 class MessageSenderImpl implements MessageSender {
46   protected static final Logger JavaDoc log = Log.open(MessageSenderImpl.class);
47   private static final L10N L = new L10N(MessageSenderImpl.class);
48
49   private MessageSenderManager _manager;
50   private ConnectionManager JavaDoc _cm;
51
52   MessageSenderImpl(MessageSenderManager manager, ConnectionManager JavaDoc cm)
53   {
54     _manager = manager;
55     _cm = cm;
56   }
57
58   public void send(HashMap JavaDoc header, Object JavaDoc value)
59     throws MessageServiceException
60   {
61     ManagedSessionImpl session = null;
62     
63     try {
64       session = (ManagedSessionImpl) _cm.allocateConnection(_manager, null);
65
66       Message JavaDoc message;
67       
68       if (value == null) {
69     message = session.getSession().createMessage();
70       }
71       else if (value instanceof String JavaDoc) {
72     message = session.getSession().createTextMessage((String JavaDoc) value);
73       }
74       else if (value instanceof java.io.Serializable JavaDoc) {
75     ObjectMessage JavaDoc objMessage = session.getSession().createObjectMessage();
76     objMessage.setObject((java.io.Serializable JavaDoc) value);
77     message = objMessage;
78       }
79       else {
80     throw new MessageServiceException(L.l("value '{0}' must be serializable",
81                           value));
82       }
83
84       session.send(message);
85     } catch (MessageServiceException e) {
86       throw e;
87     } catch (Exception JavaDoc e) {
88       e.printStackTrace();
89       throw new MessageServiceException(e);
90     } finally {
91       if (session != null)
92     session.close();
93     }
94   }
95 }
96
97
Popular Tags