KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > AbstractDestination


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;
30
31 import com.caucho.jms.message.ObjectMessageImpl;
32 import com.caucho.jms.message.TextMessageImpl;
33 import com.caucho.jms.selector.Selector;
34 import com.caucho.jms.session.MessageAvailableListener;
35 import com.caucho.jms.session.MessageProducerImpl;
36 import com.caucho.jms.session.QueueSenderImpl;
37 import com.caucho.jms.session.SessionImpl;
38 import com.caucho.jms.session.TopicPublisherImpl;
39 import com.caucho.loader.Environment;
40 import com.caucho.services.message.MessageSender;
41 import com.caucho.services.message.MessageServiceException;
42 import com.caucho.util.Alarm;
43 import com.caucho.util.Base64;
44 import com.caucho.util.CharBuffer;
45 import com.caucho.util.NullEnumeration;
46 import com.caucho.util.RandomUtil;
47
48 import javax.jms.*;
49 import java.io.Serializable JavaDoc;
50 import java.lang.ref.SoftReference JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Enumeration JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 /**
58  * An abstract destination, including the needed send/receive.
59  */

60 abstract public class AbstractDestination implements Destination, MessageSender
61 {
62   protected static Logger JavaDoc log
63     = Logger.getLogger(AbstractDestination.class.getName());
64
65   private String JavaDoc _idPrefix;
66   private long _idCount;
67
68   private volatile long _consumerSequenceId;
69   
70   private ArrayList JavaDoc<SoftReference JavaDoc<MessageAvailableListener>> _listenerRefs =
71     new ArrayList JavaDoc<SoftReference JavaDoc<MessageAvailableListener>>();
72
73   /**
74    * Creates the destination.
75    */

76   protected AbstractDestination()
77   {
78     CharBuffer cb = new CharBuffer();
79
80     cb.append("ID:");
81     Object JavaDoc serverId = Environment.getAttribute("caucho.server-id");
82     if (serverId != null)
83       cb.append(serverId);
84     Base64.encode(cb, RandomUtil.getRandomLong());
85     Base64.encode(cb, Alarm.getCurrentTime());
86
87     _idPrefix = cb.toString();
88   }
89
90   /**
91    * Generates a message id.
92    */

93   public synchronized String JavaDoc generateMessageID()
94   {
95     return _idPrefix + _idCount++;
96   }
97
98   /**
99    * Returns the current received sequence id.
100    */

101   public long getConsumerSequenceId()
102   {
103     return _consumerSequenceId;
104   }
105
106   /**
107    * Returns the current received sequence id.
108    */

109   protected synchronized long nextConsumerSequenceId()
110   {
111     return ++_consumerSequenceId;
112   }
113
114   public MessageProducer createProducer(SessionImpl session)
115   {
116     if (this instanceof Queue)
117       return new QueueSenderImpl(session, (Queue) this);
118     else if (this instanceof Topic)
119       return new TopicPublisherImpl(session, (Topic) this);
120     else
121       return new MessageProducerImpl(session, (Destination) this);
122   }
123   
124   /**
125    * Send a message to the destination.
126    *
127    * @param message the message to add.
128    */

129   public void send(Message message)
130     throws JMSException
131   {
132     throw new UnsupportedOperationException JavaDoc();
133   }
134
135   /**
136    * Adds a message available listener.
137    */

138   public void addListener(MessageAvailableListener listener)
139   {
140     synchronized (_listenerRefs) {
141       _listenerRefs.add(new SoftReference JavaDoc<MessageAvailableListener>(listener));
142     }
143
144     listener.messageAvailable();
145   }
146
147   /**
148    * Removes a message available listener.
149    */

150   public void removeListener(MessageAvailableListener listener)
151   {
152     synchronized (_listenerRefs) {
153       for (int i = _listenerRefs.size() - 1; i >= 0; i--) {
154     SoftReference JavaDoc<MessageAvailableListener> ref = _listenerRefs.get(i);
155
156     MessageAvailableListener oldListener = ref.get();
157     
158     if (oldListener == null)
159       _listenerRefs.remove(i);
160     else if (oldListener == listener) {
161       _listenerRefs.remove(i);
162       return;
163     }
164       }
165     }
166   }
167
168   /**
169    * Called when a new message is available.
170    */

171   protected void messageAvailable()
172   {
173     synchronized (_listenerRefs) {
174       for (int i = _listenerRefs.size() - 1; i >= 0; i--) {
175     SoftReference JavaDoc<MessageAvailableListener> ref = _listenerRefs.get(i);
176
177     MessageAvailableListener listener = ref.get();
178
179     if (listener != null) {
180       listener.messageAvailable();
181     }
182     else
183       _listenerRefs.remove(i);
184       }
185     }
186   }
187   
188   /**
189    * Creates a consumer.
190    *
191    * @param session the owning session
192    * @param selector the consumer's selector
193    * @param noLocal true if pub/sub should not send local requests
194    */

195   public MessageConsumer createConsumer(SessionImpl session,
196                     String JavaDoc selector,
197                     boolean noLocal)
198     throws JMSException
199   {
200     throw new UnsupportedOperationException JavaDoc(getClass().getName());
201   }
202   
203   /**
204    * Creates a queue browser
205    *
206    * @param session the owning session
207    * @param selector the browser's selector
208    */

209   public QueueBrowser createBrowser(SessionImpl session,
210                     String JavaDoc selector)
211     throws JMSException
212   {
213     throw new UnsupportedOperationException JavaDoc(getClass().getName());
214   }
215   
216   /**
217    * Creates a subscriber.
218    *
219    * @param session the owning session
220    * @param selector the consumer's selector
221    * @param noLocal true if pub/sub should not send local requests
222    * @param name the durable subscriber's name
223    */

224   public TopicSubscriber createDurableSubscriber(SessionImpl session,
225                          String JavaDoc selector,
226                          boolean noLocal,
227                          String JavaDoc name)
228     throws JMSException
229   {
230     throw new UnsupportedOperationException JavaDoc(getClass().getName());
231   }
232
233   /**
234    * Lists the available messages.
235    */

236   public Enumeration getEnumeration(Selector selector)
237     throws JMSException
238   {
239     return NullEnumeration.create();
240   }
241
242   // com.caucho.services.message API
243

244   /**
245    * Sends a message to the destination.
246    */

247   public void send(HashMap JavaDoc headers, Object JavaDoc data)
248     throws MessageServiceException
249   {
250     try {
251       Message message;
252       
253       if (data instanceof String JavaDoc) {
254         TextMessage msg = new TextMessageImpl();
255         msg.setText((String JavaDoc) data);
256         
257         message = msg;
258       }
259       else if (data instanceof Serializable JavaDoc) {
260         ObjectMessage msg = new ObjectMessageImpl();
261         msg.setObject((Serializable JavaDoc) data);
262         message = msg;
263       }
264       else
265         throw new MessageServiceException("not a serializable object: " + data);
266
267       if (headers != null) {
268         Iterator JavaDoc<String JavaDoc> iter = headers.keySet().iterator();
269         while (iter.hasNext()) {
270           String JavaDoc key = iter.next();
271           Object JavaDoc value = headers.get(key);
272
273           message.setObjectProperty(key, value);
274         }
275       }
276
277       send(message);
278     } catch (MessageServiceException e) {
279       throw e;
280     } catch (Exception JavaDoc e) {
281       throw new MessageServiceException(e);
282     }
283   }
284 }
285
286
Popular Tags