KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > services > MantaService


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.kernel.services;
48
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.Collections JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.List JavaDoc;
54
55 import org.mr.MantaAgent;
56 import org.mr.MantaAgentConstants;
57 import org.apache.commons.logging.LogFactory;
58 import org.mr.core.protocol.MantaBusMessage;
59 import org.mr.core.protocol.MantaBusMessageConsts;
60 import org.mr.core.util.SystemTime;
61
62 /**
63  * This class represents the super class for a queue or a topic
64  *
65  *
66  * @version 1.0
67  * @since Dec 25, 2003
68  * @author Amir Shevat
69  *
70  */

71 public abstract class MantaService{// implements Serializable {
72

73     public static final byte SERVICE_TYPE_QUEUE = 1;
74     public static final byte SERVICE_TYPE_TOPIC = 2;
75     
76     private boolean serviceUpdated = false;
77         
78     protected String JavaDoc logicalName;
79     
80     //DURABLE MODE
81
private byte persistentMode = MantaAgentConstants.AGENT_DEFAULT_DELIVERY_MODE;
82     private boolean blocking = MantaAgentConstants.DEFAULT_PERSISTENCY_BLOCKING_MODE;
83     
84     protected ArrayList JavaDoc consumers = new ArrayList JavaDoc();
85     protected ArrayList JavaDoc producers = new ArrayList JavaDoc();
86     
87     protected HashMap JavaDoc serviceActorMap = new HashMap JavaDoc();
88     //private Log log;
89

90     /**
91      * Constractor for MantaService
92      * @param serviceName - the name of the queue or the topic
93      */

94     public MantaService(String JavaDoc serviceName ){
95         this.logicalName = serviceName;
96         //log=LogFactory.getLog("MantaService");
97
}
98     
99     /**
100      * @return Returns the logicalName.
101      */

102     public String JavaDoc getServiceName() {
103         return logicalName;
104     }
105     
106
107     
108     /**
109      * checks is a given service actor is a consumer of the service
110      * @param actor a consumer of this service
111      * @return true if there is a consumer with the same id as the one passed to the method else false
112      */

113     public boolean isConsumer(ServiceActor actor){
114         return consumers.contains(actor);
115     }
116     /**
117      * checks is a given service actor is a producer of the service
118      * @param actor a producer of this service
119      * @return true if there is a producer with the same id as the one passed to the method else false
120      */

121     public boolean isProducer(ServiceActor actor){
122         return producers.contains(actor);
123     }
124
125     /**
126      * @return Returns the ServiceConsumer.
127      */

128     public List JavaDoc getConsumers() {
129         return new ArrayList JavaDoc(consumers);
130     }
131     
132     /**
133      *
134      * @param agentId the id of the agent we want the consumer of
135      * @return all the Consumers of this agent name
136      */

137     public ArrayList JavaDoc getConsumersByAgentId(String JavaDoc agentId){
138         ArrayList JavaDoc list = new ArrayList JavaDoc();
139         int size = consumers.size();
140         for (int i = 0; i < size; i++) {
141             ServiceConsumer consumer = (ServiceConsumer)consumers.get(i);
142             if(consumer.getAgentName().equals(agentId))
143                 list.add(consumer);
144         }
145         
146         return list;
147     }//getConsumersByAgentId
148

149     /**
150      * @return Returns the ServiceProducer list of the service.
151      */

152     public List JavaDoc getProducers() {
153         return Collections.unmodifiableList(producers) ;
154     }
155     
156     /**
157      *
158      * @param agentId the id of the agent we want the Producer of
159      * @return all the Producer of this agent name or empty list if non
160      */

161     public ArrayList JavaDoc getProducersByAgentId(String JavaDoc agentId){
162         synchronized(producers){
163             ArrayList JavaDoc list = new ArrayList JavaDoc();
164             int size = producers.size();
165             for (int i = 0; i < size; i++) {
166                 ServiceProducer producer = (ServiceProducer)producers.get(i);
167                 if(producer.getAgentName().equals(agentId))
168                     list.add(producer);
169             }
170             
171             return list;
172         }
173         
174     }//getProducersByAgentId
175

176     /**
177      * @param actorId the unique id of the actor
178      * @return a service actor if found in the service
179      */

180     public ServiceActor getActor(String JavaDoc actorId){
181         return (ServiceActor)serviceActorMap.get(actorId);
182     }
183     
184     /**
185      * called by the control layer to indicate that a remote agent has become a producer of this service
186      * @param producer the address of the remote agent
187      */

188     public void addProducer(ServiceProducer producer){
189         if (serviceActorMap.get(producer.getId()) != null) {
190             return;
191         }
192         synchronized(producers){
193             producers.add(producer);
194         }
195         serviceActorMap.put(producer.getId() ,producer );
196     }
197     
198     
199     /**
200      * called by the control layer to indicate that a remote agent has stopped been a producer of this service
201      * @param producer the address of the remote agent
202      */

203     public void removeProducer(ServiceProducer producer){
204         
205         synchronized(producers){
206             producers.remove(producer);
207         }
208         serviceActorMap.remove(producer.getId());
209     }
210     
211     /**
212      * called by the control layer to indicate that a remote agent has become a consumer of this service
213      * @param consumer the address of the remote agent
214      */

215     public void addConsumer(ServiceConsumer consumer){
216         // for durable subscribers we need to update the agent name and other properties
217
// so we remove
218
if(consumer.isDurable()){
219             ServiceConsumer old = (ServiceConsumer) serviceActorMap.get(consumer.getId());
220             if(old != null){
221                 consumers.remove(old);
222                 serviceActorMap.remove(old.getId());
223             }
224         }
225         
226         if(serviceActorMap.get(consumer.getId())!= null){
227             if(LogFactory.getLog("MantaService").isInfoEnabled()){
228                 LogFactory.getLog("MantaService").info("Consumer with the same id is already registered to this service consumer id ="+consumer.getId()+".");
229             }
230             return;
231         }
232         synchronized(consumers){
233             consumers.add(consumer);
234         }
235         
236         serviceActorMap.put(consumer.getId() ,consumer );
237         
238         ServiceActorControlCenter.addUpConsumer(consumer);
239         
240     }
241     
242     /**
243      * called by the control layer to indicate that a remote agent has stopped been a consumer of this service
244      * @param consumer the address of the remote agent
245      */

246     public void removeConsumer(ServiceConsumer consumer){
247         synchronized (consumers) {
248             consumers.remove(consumer);
249         }
250         serviceActorMap.remove(consumer.getId());
251         ServiceActorControlCenter.removeUpConsumer(consumer);
252         
253     }
254     
255     
256     
257     
258     
259     
260
261     /**
262      * @return Returns the serviceType.
263      */

264     public abstract byte getServiceType();
265
266     
267     
268     public String JavaDoc toString(){
269         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
270         buff.append(" service{");
271         buff.append(" service name=");
272         buff.append(logicalName);
273         buff.append(" serviceType=");
274         buff.append(getServiceType());
275         buff.append(" consumers=");
276         buff.append(consumers);
277         buff.append(" producers=");
278         buff.append(producers);
279         buff.append(" persistentMode=");
280         buff.append(persistentMode);
281         buff.append(" }");
282         return buff.toString();
283     }
284
285     /**
286      * @return Returns the deliveryMode.
287      */

288     public byte getPersistentMode() {
289         return persistentMode;
290     }
291
292     /**
293      * @param deliveryMode The deliveryMode to set.
294      */

295     public void setPersistentMode(byte deliveryMode) {
296         this.persistentMode = deliveryMode;
297     }
298
299     /**
300      * service is updated if it holds the info of its producers and consumers
301      * @return Returns the serviceUpdated.
302      */

303     public boolean isServiceUpdated() {
304         return serviceUpdated;
305     }
306
307     /**
308      * @param serviceUpdated The serviceUpdated to set.
309      */

310     public void setServiceUpdated(boolean serviceUpdated) {
311         this.serviceUpdated = serviceUpdated;
312     }
313
314     /**
315      * persistent services will block if true until saved to disk
316      * @return Returns the blocking.
317      */

318     public boolean isBlocking() {
319         return blocking;
320     }
321
322     /**
323      * persistent services will block if true until saved to disk
324      * @param blocking The blocking to set.
325      */

326     public void setBlocking(boolean blocking) {
327         this.blocking = blocking;
328     }
329     
330     /**
331      * checks if the message is valid for a given consumer
332      * @param msg the message to be checked
333      * @param consumer the ServiceConsumer with the selector statement
334      * @return true if message valid until is still good and payload selector returns true else this method will return false
335      */

336     public boolean checkValidMessage(MantaBusMessage msg ,ServiceConsumer consumer){
337         // filter messages
338
// here we look at the payload select a payload selector that will
339
// inspect the payload with the consumer select statement
340
String JavaDoc payloadType =msg.getHeader(MantaBusMessageConsts.HEADER_NAME_PAYLOAD_TYPE);
341         SelectorsManager manager = MantaAgent.getInstance().getSingletonRepository().getSelectorsManager();
342         PayLoadSelector select = manager.getSelector(payloadType);
343         // time is valid if smaller then now
344
boolean validTime = msg.getValidUntil() > SystemTime.gmtCurrentTimeMillis();
345         if(!validTime){
346             if(LogFactory.getLog("MantaService").isInfoEnabled()){
347                     LogFactory.getLog("MantaService").info("Not sending message "+msg +" msg.getValidUntil()=" +msg.getValidUntil()+ " SystemTime.gmtCurrentTimeMillis()=" +SystemTime.gmtCurrentTimeMillis()+".");
348             }
349         }
350         // select is valid if no selector or selector accepts
351
boolean validSelect = select == null ||select.accept(consumer.getSelectorStatment() , msg);
352         
353         return validTime && validSelect;
354     }//checkValidMessage
355

356 }
357
Popular Tags