KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > rmi > thin > ThinRMIJMSImpl


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.rmi.thin;
47
48 import java.rmi.RemoteException JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.LinkedList JavaDoc;
53
54 import javax.jms.Connection JavaDoc;
55 import javax.jms.ConnectionFactory JavaDoc;
56 import javax.jms.JMSException JavaDoc;
57 import javax.jms.Message JavaDoc;
58 import javax.jms.MessageConsumer JavaDoc;
59 import javax.jms.MessageListener JavaDoc;
60 import javax.jms.MessageProducer JavaDoc;
61 import javax.jms.Queue JavaDoc;
62 import javax.jms.Session JavaDoc;
63 import javax.jms.TextMessage JavaDoc;
64 import javax.jms.Topic JavaDoc;
65
66 import org.mr.MantaAgent;
67 import org.mr.api.jms.MantaConnectionFactory;
68 import org.mr.kernel.services.MantaService;
69 import org.mr.kernel.world.WorldModeler;
70
71 public class ThinRMIJMSImpl implements ThinMessagingInterface {
72     /*
73      * This inner class defines the key for all internal tables
74      * containing service producers and consumers. The key is a
75      * combination of a user name and destination (topic or queue)
76      * name.
77      */

78     class Key {
79         String JavaDoc user;
80         String JavaDoc destination;
81
82         public Key(String JavaDoc user, String JavaDoc destination) {
83             this.user = user;
84             this.destination = destination;
85         }
86
87         public int hashCode() {
88             return user.hashCode() + destination.hashCode();
89         }
90
91         public boolean equals(Object JavaDoc other) {
92             if (!(other instanceof Key)) {
93                 return false;
94             }
95             if (other == null) {
96                 return false;
97             }
98             if (this == other) {
99                 return true;
100             }
101             return
102                 this.user.equals(((Key) other).user) &&
103                 this.destination.equals(((Key) other).destination);
104         }
105     }
106
107     class ConsumerPlus implements MessageListener JavaDoc {
108         MessageConsumer JavaDoc consumer;
109         LinkedList JavaDoc messages;
110         int maxMessages;
111
112         public void onMessage(Message JavaDoc message) {
113             synchronized (this.messages) {
114                 if (this.messages.size() < this.maxMessages) {
115                     this.messages.add(message);
116                 }
117             }
118         }
119     }
120
121     private ConnectionFactory JavaDoc factory;
122     private Connection JavaDoc connection;
123     private Session JavaDoc session;
124     private HashMap JavaDoc consumers;
125     private HashMap JavaDoc producers;
126     private boolean initJMS;
127     public ThinRMIJMSImpl() {
128         this.consumers = new HashMap JavaDoc();
129         this.producers = new HashMap JavaDoc();
130         this.initJMS = false;
131     }
132
133     private void initJMS() throws RemoteException JavaDoc {
134         if (!initJMS) {
135             try {
136                 this.factory = new MantaConnectionFactory();
137                 this.connection = factory.createConnection();
138                 this.session =
139                     connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
140                 connection.start();
141                 initJMS = true;
142             } catch (JMSException JavaDoc e) {
143                 throw new RemoteException JavaDoc("Error in Manta", e);
144             }
145         }
146     }
147
148     public boolean enqueueMessage(String JavaDoc userKey, String JavaDoc queueName,
149                                   String JavaDoc text)
150         throws RemoteException JavaDoc
151     {
152         initJMS();
153         try {
154             TextMessage JavaDoc message = this.session.createTextMessage();
155             Key key = new Key(userKey, queueName);
156             MessageProducer JavaDoc producer =
157                 (MessageProducer JavaDoc) this.producers.get(key);
158
159             if (producer == null) {
160                 Queue JavaDoc queue = this.session.createQueue(queueName);
161                 producer = this.session.createProducer(queue);
162                 this.producers.put(key, producer);
163             }
164
165             message.setText(text);
166             producer.send(message);
167             return true;
168         } catch (JMSException JavaDoc e) {
169             throw new RemoteException JavaDoc("Error in Manta", e);
170         }
171     }
172
173     public String JavaDoc denqueueMessage(String JavaDoc userKey, String JavaDoc queueName)
174         throws RemoteException JavaDoc
175     {
176         initJMS();
177         try {
178             String JavaDoc result = null;
179             Key key = new Key(userKey, queueName);
180             MessageConsumer JavaDoc consumer =
181                 (MessageConsumer JavaDoc) this.consumers.get(key);
182
183             if (consumer == null) {
184                 Queue JavaDoc queue = this.session.createQueue(queueName);
185                 consumer = this.session.createConsumer(queue);
186                 this.consumers.put(key, consumer);
187             }
188
189             TextMessage JavaDoc message = (TextMessage JavaDoc) consumer.receive(1000);
190             if (message != null) {
191                 result = message.getText();
192             }
193             return result;
194         } catch (JMSException JavaDoc e) {
195             throw new RemoteException JavaDoc("Error in Manta", e);
196         }
197     }
198
199     public void publishMessage(String JavaDoc userKey, String JavaDoc topicName, String JavaDoc text)
200         throws RemoteException JavaDoc
201     {
202         initJMS();
203         try {
204             TextMessage JavaDoc message = this.session.createTextMessage();
205             Key key = new Key(userKey, topicName);
206             MessageProducer JavaDoc producer =
207                 (MessageProducer JavaDoc) this.producers.get(key);
208
209             if (producer == null) {
210                 Topic JavaDoc topic = this.session.createTopic(topicName);
211                 producer = this.session.createProducer(topic);
212                 this.producers.put(key, producer);
213             }
214
215             message.setText(text);
216             producer.send(message);
217         } catch (JMSException JavaDoc e) {
218             throw new RemoteException JavaDoc("Error in Manta", e);
219         }
220     }
221
222     public void subscribe(String JavaDoc userKey, String JavaDoc topicName, int cacheSize)
223         throws RemoteException JavaDoc
224     {
225         initJMS();
226         try {
227             Key key = new Key(userKey, topicName);
228             ConsumerPlus cp = (ConsumerPlus) this.consumers.get(key);
229             if (cp != null) {
230                 return;
231             }
232
233
234             Topic JavaDoc topic = this.session.createTopic(topicName);
235             MessageConsumer JavaDoc consumer = this.session.createConsumer(topic);
236
237             cp = new ConsumerPlus();
238             cp.consumer = consumer;
239             cp.messages = new LinkedList JavaDoc();
240             cp.maxMessages = cacheSize;
241
242             consumer.setMessageListener(cp);
243
244             this.consumers.put(key, cp);
245         } catch (JMSException JavaDoc e) {
246             throw new RemoteException JavaDoc("Error in Manta", e);
247         }
248     }
249
250     public void unsubscribe(String JavaDoc userKey, String JavaDoc topicName)
251         throws RemoteException JavaDoc
252     {
253         initJMS();
254         try {
255             Key key = new Key(userKey, topicName);
256             ConsumerPlus cp = (ConsumerPlus) this.consumers.get(key);
257             if (cp != null) {
258                 cp.consumer.close();
259             }
260         } catch (JMSException JavaDoc e) {
261             throw new RemoteException JavaDoc("Error in Manta", e);
262         }
263     }
264
265     public String JavaDoc[] getMessageFromTopic(String JavaDoc userKey, String JavaDoc topicName)
266         throws RemoteException JavaDoc
267     {
268         initJMS();
269         try {
270             String JavaDoc[] results = null;
271             Key key = new Key(userKey, topicName);
272             ConsumerPlus cp = (ConsumerPlus) this.consumers.get(key);
273             if (cp != null) {
274                 synchronized (cp.messages) {
275                     results = new String JavaDoc[cp.messages.size()];
276                     Iterator JavaDoc iter = cp.messages.iterator();
277                     int index = 0;
278                     while (iter.hasNext()) {
279                         TextMessage JavaDoc message = (TextMessage JavaDoc) iter.next();
280                         results[index++] = message.getText();
281                     }
282                 }
283             }
284
285             return results;
286         } catch (JMSException JavaDoc e) {
287             throw new RemoteException JavaDoc("Error in Manta", e);
288         }
289     }
290
291     /* (non-Javadoc)
292      * @see org.mr.api.rmi.thin.ThinMessagingInterface#getTopics()
293      */

294     public String JavaDoc[] getTopics() throws RemoteException JavaDoc {
295         initJMS();
296         WorldModeler world = MantaAgent.getInstance().getSingletonRepository().getWorldModeler();
297         
298         ArrayList JavaDoc list = new ArrayList JavaDoc();
299         Iterator JavaDoc iter = world.getServices(world.getDefaultDomainName()).iterator();
300         while(iter.hasNext()){
301             MantaService service = (MantaService)iter.next();
302             if(service.getServiceType() == MantaService.SERVICE_TYPE_TOPIC){
303                 list.add(service.getServiceName());
304             }
305         }
306         String JavaDoc[] result = new String JavaDoc[list.size()];
307         for (int i = 0; i < list.size(); i++) {
308             result[i] = (String JavaDoc)list.get(i);
309         }
310         return result;
311     }
312
313     public String JavaDoc[] getQueues() throws RemoteException JavaDoc{
314         initJMS();
315         WorldModeler world = MantaAgent.getInstance().getSingletonRepository().getWorldModeler();
316         
317         ArrayList JavaDoc list = new ArrayList JavaDoc();
318         Iterator JavaDoc iter = world.getServices(world.getDefaultDomainName()).iterator();
319         while(iter.hasNext()){
320             MantaService service = (MantaService)iter.next();
321             if(service.getServiceType() == MantaService.SERVICE_TYPE_QUEUE){
322                 list.add(service.getServiceName());
323             }
324         }
325         String JavaDoc[] result = new String JavaDoc[list.size()];
326         for (int i = 0; i < list.size(); i++) {
327             result[i] = (String JavaDoc)list.get(i);
328         }
329         return result;
330     }
331 }
Popular Tags