KickJava   Java API By Example, From Geeks To Geeks.

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


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 Amir Shevat.
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
47 package org.mr.api.rmi.thin;
48
49 import java.rmi.RemoteException JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54
55 import org.mr.MantaAgent;
56 import org.mr.MantaAgentConstants;
57 import org.mr.api.rmi.MantaRMIServer;
58 import org.mr.core.protocol.MantaBusMessage;
59 import org.mr.core.protocol.MantaBusMessageConsts;
60 import org.mr.core.util.SystemTime;
61 import org.mr.core.util.byteable.ByteableText;
62 import org.mr.kernel.services.MantaService;
63 import org.mr.kernel.services.ServiceConsumer;
64 import org.mr.kernel.services.ServiceProducer;
65 import org.mr.kernel.services.topics.TopicGatherListener;
66 import org.mr.kernel.world.WorldModeler;
67
68 /**
69  *
70  * * inplaments ThinMessagingInterface for RMI thin API
71  * Note: if you are compliing the mataray from the CVS you need to run RMIC on this class after you compile it with javac
72  *
73  * @author Amir Shevat
74  *
75  */

76 public class ThinRMIMantarayImpl implements ThinMessagingInterface {
77     private HashMap JavaDoc consumers = new HashMap JavaDoc();
78     private HashMap JavaDoc producers = new HashMap JavaDoc();
79     private HashMap JavaDoc listeners = new HashMap JavaDoc();
80     
81     
82     public ThinRMIMantarayImpl(){
83         
84     }
85     
86     
87     public boolean enqueueMessage(String JavaDoc userKey, String JavaDoc queueName ,String JavaDoc msg ) throws RemoteException JavaDoc{
88         try{
89             
90             MantaBusMessage message = MantaBusMessage.getInstance();
91             ByteableText str = new ByteableText(msg);
92             message.setPayload(str);
93             // message is client message only
94
message.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CLIENT);
95             ServiceProducer producer = (ServiceProducer)producers.get(userKey+queueName);
96             if(producer == null){
97                 MantaService service = MantaRMIServer.manta.getService(queueName, MantaService.SERVICE_TYPE_QUEUE);
98                 producer = ServiceProducer.createNew(service);
99                 MantaRMIServer.manta.advertiseService(producer);
100                 producers.put(userKey+queueName, producer);
101             }
102             MantaRMIServer.manta.enqueueMessage(message, producer, MantaAgentConstants.NON_PERSISTENT,MantaAgentConstants.NORMAL,600000+SystemTime.gmtCurrentTimeMillis() );
103         }catch(Exception JavaDoc e){
104             e.printStackTrace();
105             throw new RemoteException JavaDoc("error in manta", e);
106         }
107         return true;
108         
109     }
110     
111     public String JavaDoc[] getQueues() throws RemoteException JavaDoc{
112         WorldModeler world = MantaAgent.getInstance().getSingletonRepository().getWorldModeler();
113         
114         ArrayList JavaDoc list = new ArrayList JavaDoc();
115         Iterator JavaDoc iter = world.getServices(world.getDefaultDomainName()).iterator();
116         while(iter.hasNext()){
117             MantaService service = (MantaService)iter.next();
118             if(service.getServiceType() == MantaService.SERVICE_TYPE_QUEUE){
119                 list.add(service.getServiceName());
120             }
121         }
122         String JavaDoc[] result = new String JavaDoc[list.size()];
123         for (int i = 0; i < list.size(); i++) {
124             result[i] = (String JavaDoc)list.get(i);
125         }
126         return result;
127     }
128     
129     public String JavaDoc denqueueMessage(String JavaDoc userKey, String JavaDoc queueName ) throws RemoteException JavaDoc{
130         String JavaDoc result = null;
131         try{
132             ServiceConsumer consumer = (ServiceConsumer)consumers.get(userKey+queueName);
133             
134             if(consumer == null){
135                 MantaService service = MantaRMIServer.manta.getService(queueName, MantaService.SERVICE_TYPE_QUEUE);
136                 consumer = new ServiceConsumer(MantaRMIServer.manta.getAgentName(), MantaRMIServer.manta.getDomainName(), service.getServiceName(), service.getServiceType(),MantaAgentConstants.AUTO_ACK );
137                 MantaRMIServer.manta.advertiseService(consumer);
138                 consumers.put(userKey+queueName, consumer);
139             }
140             MantaBusMessage msg = MantaRMIServer.manta.receive(consumer,1000);
141             if(msg != null)
142                 result = String.valueOf(msg.getPayload()) ;
143             return result;
144         }catch(Exception JavaDoc e){
145             e.printStackTrace();
146             throw new RemoteException JavaDoc("error in manta", e);
147         }
148         
149     }
150
151
152     /* (non-Javadoc)
153      * @see org.mr.api.rmi.thin.ThinMessagingInterface#publishMessage(java.lang.String, java.lang.String, java.lang.String)
154      */

155     public void publishMessage(String JavaDoc userKey, String JavaDoc topicName, String JavaDoc msg) throws RemoteException JavaDoc {
156         try{
157             
158             MantaBusMessage message = MantaBusMessage.getInstance();
159             ByteableText str = new ByteableText(msg);
160             message.setPayload(str);
161             // message is client message only
162
message.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CLIENT);
163             ServiceProducer producer = (ServiceProducer)producers.get(userKey+topicName);
164             if(producer == null){
165                 MantaService service = MantaRMIServer.manta.getService(topicName, MantaService.SERVICE_TYPE_TOPIC);
166                 producer = ServiceProducer.createNew(service);
167                 MantaRMIServer.manta.advertiseService(producer);
168                 producers.put(userKey+topicName, producer);
169             }
170             MantaRMIServer.manta.publish(message, producer, MantaAgentConstants.NON_PERSISTENT,MantaAgentConstants.NORMAL,600000 +SystemTime.gmtCurrentTimeMillis());
171         }catch(Exception JavaDoc e){
172             e.printStackTrace();
173             throw new RemoteException JavaDoc("error in manta", e);
174         }
175         
176     }
177
178
179     /* (non-Javadoc)
180      * @see org.mr.api.rmi.thin.ThinMessagingInterface#subscribe(java.lang.String, java.lang.String, int, java.lang.String)
181      */

182     public synchronized void subscribe(String JavaDoc userKey, String JavaDoc topicName, int messagesToCash) throws RemoteException JavaDoc {
183         TopicGatherListener listener = (TopicGatherListener) listeners.get(userKey+topicName);
184         if(listener != null)
185             return;
186         
187         try{
188             ServiceConsumer consumer = (ServiceConsumer)consumers.get(userKey+topicName);
189             
190             if(consumer == null){
191                 MantaService service = MantaRMIServer.manta.getService(topicName, MantaService.SERVICE_TYPE_TOPIC);
192                 if(service == null){
193                     throw new RemoteException JavaDoc("no service "+topicName);
194                 }
195                 consumer = new ServiceConsumer(MantaRMIServer.manta.getAgentName(), MantaRMIServer.manta.getDomainName(), service.getServiceName(), service.getServiceType(),MantaAgentConstants.AUTO_ACK );
196                 MantaRMIServer.manta.advertiseService(consumer);
197                 consumers.put(userKey+topicName, consumer);
198             }
199             listener = new TopicGatherListener(messagesToCash);
200             listeners.put(userKey+topicName ,listener );
201             MantaRMIServer.manta.subscribeToTopic(listener , consumer.getServiceName());
202         }catch(Exception JavaDoc e){
203             e.printStackTrace();
204             throw new RemoteException JavaDoc("error in manta", e);
205         }
206         
207     }
208
209
210     /* (non-Javadoc)
211      * @see org.mr.api.rmi.thin.ThinMessagingInterface#unsubscribe(java.lang.String, java.lang.String)
212      */

213     public void unsubscribe(String JavaDoc userKey, String JavaDoc topicName) throws RemoteException JavaDoc {
214         TopicGatherListener listener = (TopicGatherListener) listeners.get(userKey+topicName);
215         if(listener != null){
216             MantaRMIServer.manta.unsubscribeFromTopic(listener ,topicName );
217             listeners.remove(listener);
218         }
219         
220     }
221
222
223     /* (non-Javadoc)
224      * @see org.mr.api.rmi.thin.ThinMessagingInterface#getMessageFromTopic(java.lang.String, java.lang.String)
225      */

226     public String JavaDoc[] getMessageFromTopic(String JavaDoc userKey, String JavaDoc topicName) throws RemoteException JavaDoc {
227         TopicGatherListener listener = (TopicGatherListener) listeners.get(userKey+topicName);
228         List JavaDoc messages = listener.getMessages();
229         
230         String JavaDoc[] result = new String JavaDoc[messages.size()];
231         for (int i = 0; i < messages.size(); i++) {
232             result[i] = String.valueOf(((MantaBusMessage)messages.get(i)).getPayload());
233         }
234         
235         return result;
236     }
237
238
239     /* (non-Javadoc)
240      * @see org.mr.api.rmi.thin.ThinMessagingInterface#getTopics()
241      */

242     public String JavaDoc[] getTopics() throws RemoteException JavaDoc {
243         WorldModeler world = MantaAgent.getInstance().getSingletonRepository().getWorldModeler();
244         
245         ArrayList JavaDoc list = new ArrayList JavaDoc();
246         Iterator JavaDoc iter = world.getServices(world.getDefaultDomainName()).iterator();
247         while(iter.hasNext()){
248             MantaService service = (MantaService)iter.next();
249             if(service.getServiceType() == MantaService.SERVICE_TYPE_TOPIC){
250                 list.add(service.getServiceName());
251             }
252         }
253         String JavaDoc[] result = new String JavaDoc[list.size()];
254         for (int i = 0; i < list.size(); i++) {
255             result[i] = (String JavaDoc)list.get(i);
256         }
257         return result;
258     }
259
260
261     
262
263 }
264
Popular Tags