KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > hessian > HessianQueue


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.hessian;
30
31 import com.caucho.ejb.hessian.HessianWriter;
32 import com.caucho.hessian.io.HessianInput;
33 import com.caucho.jms.session.MessageAvailableListener;
34 import com.caucho.services.message.MessageSender;
35 import com.caucho.services.message.MessageServiceException;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.L10N;
38 import com.caucho.util.Log;
39 import com.caucho.vfs.Path;
40 import com.caucho.vfs.ReadStream;
41 import com.caucho.vfs.ReadWritePair;
42 import com.caucho.vfs.Vfs;
43 import com.caucho.vfs.WriteStream;
44
45 import javax.jms.JMSException JavaDoc;
46 import javax.jms.Message JavaDoc;
47 import javax.jms.ObjectMessage JavaDoc;
48 import javax.jms.TextMessage JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.util.Enumeration JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * The Hessian queue is a write-only queue.
56  */

57 public class HessianQueue extends com.caucho.jms.AbstractQueue
58   implements MessageSender {
59   private final static Logger JavaDoc log = Log.open(HessianQueue.class);
60   static L10N L = new L10N(HessianQueue.class);
61
62   private String JavaDoc queueName;
63   private String JavaDoc url;
64   private Path path;
65
66   public HessianQueue()
67   {
68   }
69
70   /**
71    * Returns the queue's name.
72    */

73   public String JavaDoc getQueueName()
74   {
75     return queueName;
76   }
77
78   /**
79    * Sets the queue's name.
80    */

81   public void setQueueName(String JavaDoc name)
82   {
83     this.queueName = name;
84   }
85
86   public void setURL(String JavaDoc url)
87   {
88     this.url = url;
89   }
90
91   public String JavaDoc getURL()
92   {
93     return url;
94   }
95
96   public void setPath(Path path)
97   {
98     this.path = path;
99   }
100
101   public Path getPath()
102   {
103     if (path == null)
104       path = Vfs.lookup(url);
105     
106     return path;
107   }
108   
109   /**
110    * Adds a message available listener.
111    */

112   public void addListener(MessageAvailableListener listener)
113   {
114     throw new UnsupportedOperationException JavaDoc();
115   }
116
117   public void send(Message JavaDoc message)
118     throws JMSException JavaDoc
119   {
120     try {
121       HashMap JavaDoc<String JavaDoc,Object JavaDoc> headers = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
122
123       Enumeration JavaDoc names = message.getPropertyNames();
124       while (names != null && names.hasMoreElements()) {
125         String JavaDoc name = (String JavaDoc) names.nextElement();
126
127         Object JavaDoc value = message.getObjectProperty(name);
128
129         headers.put(name, value);
130       }
131
132       if (message instanceof TextMessage JavaDoc)
133         send(headers, ((TextMessage JavaDoc) message).getText());
134       else if (message instanceof ObjectMessage JavaDoc)
135         send(headers, ((ObjectMessage JavaDoc) message).getObject());
136       else
137         send(headers, message);
138     } catch (Exception JavaDoc e) {
139       throw new JMSException JavaDoc(String.valueOf(e));
140     }
141   }
142
143   public void send(HashMap JavaDoc headers, Object JavaDoc data)
144     throws MessageServiceException
145   {
146     ReadStream is = null;
147     WriteStream os = null;
148     
149     try {
150       ReadWritePair pair = getPath().openReadWrite();
151       is = pair.getReadStream();
152       os = pair.getWriteStream();
153
154       HessianWriter out = new HessianWriter(os);
155       HessianInput in = new HessianInput(is);
156
157       out.startCall("send");
158       
159       out.writeObject(headers);
160       out.writeObject(data);
161
162       out.completeCall();
163
164       os.flush();
165
166       String JavaDoc status = (String JavaDoc) is.getAttribute("status");
167
168       if (! "200".equals(status)) {
169     CharBuffer errorMsg = new CharBuffer();
170     int ch;
171     
172     while ((ch = is.readChar()) >= 0) {
173       errorMsg.append((char) ch);
174     }
175
176     throw new MessageServiceException(errorMsg.toString());
177       }
178
179       Object JavaDoc result = in.readReply(null);
180     } catch (Throwable JavaDoc e) {
181       e.printStackTrace();
182       throw new MessageServiceException(e);
183     } finally {
184       if (os != null) {
185         try {
186           os.close();
187         } catch (IOException JavaDoc e) {
188         }
189       }
190       if (is != null) {
191     is.close();
192       }
193     }
194   }
195
196   /**
197    * Returns a printable view of the queue.
198    */

199   public String JavaDoc toString()
200   {
201     return "[HessianQueue " + queueName + "]";
202   }
203 }
204
205
Popular Tags