KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.mr.kernel.services;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51
52 import org.mr.MantaAgent;
53 import org.mr.MantaException;
54 import org.mr.core.protocol.RecipientAddress;
55 import org.mr.indexing.WBAdvertiser;
56 import org.mr.kernel.control.ControlSignalMessageConsumer;
57 import org.mr.kernel.control.ControlSignalMessageSender;
58
59 /**
60  * controls the Advertisements and recall of all the service actors
61  * and acts as service actor repository
62  *
63  * @author Amir Shevat
64  *
65  */

66 public class ServiceActorControlCenter {
67     ControlSignalMessageSender defaultSender;
68     WBAdvertiser wbSender;
69     
70     static ArrayList JavaDoc consumerStatusListeners = new ArrayList JavaDoc();
71     // holds the consumers that are up now
72
private static HashMap JavaDoc upConsumers = new HashMap JavaDoc();
73     
74     static synchronized void addUpConsumer(ServiceConsumer consumer ){
75         upConsumers.put(consumer.getId(),consumer);
76         Iterator JavaDoc consumerListeners = consumerStatusListeners.iterator();
77         while(consumerListeners.hasNext()){
78             ServiceActorStatusListener listener = (ServiceActorStatusListener) consumerListeners.next();
79             listener.handleConsumerUp(consumer);
80         }
81     }
82     
83     public static synchronized void removeUpConsumer(ServiceConsumer consumer ){
84         upConsumers.remove(consumer.getId());
85         Iterator JavaDoc consumerListeners = consumerStatusListeners.iterator();
86         while(consumerListeners.hasNext()){
87             ServiceActorStatusListener listener = (ServiceActorStatusListener) consumerListeners.next();
88             listener.handleConsumerDown(consumer);
89         }
90     }
91     
92     public static synchronized void addConsumerStatusListeners(ServiceActorStatusListener listener){
93         consumerStatusListeners.add(listener);
94     }
95     
96     public static synchronized void removeConsumerStatusListeners(ServiceActorStatusListener listener){
97         consumerStatusListeners.remove(listener);
98     }
99     
100     public static boolean isConsumerUp(RecipientAddress recipient){
101         if(recipient instanceof ServiceConsumer){
102             // if recipient is control then it is online
103
if(((ServiceConsumer)recipient).getServiceName()
104                     .equals(ControlSignalMessageConsumer
105                             .CONTROL_PSEUDO_SERVICE_NAME)){
106                 return true;
107             }else{
108                 return upConsumers.containsKey(recipient.getId());
109             }
110             
111                 
112         }
113             
114         return true;
115     }
116     
117
118     public ServiceActorControlCenter() {
119         this.defaultSender = new ControlSignalMessageSender();
120         this.wbSender = null;
121     }
122
123     public void setDefaultSender(ControlSignalMessageSender sender) {
124         this.defaultSender = sender;
125     }
126
127     public void setWBSender(WBAdvertiser wbSender) {
128         this.wbSender = wbSender;
129     }
130
131     /**
132      * Tells all the other layer that this layer is a producer or a consumer
133      * (role) of a queue or a topic (service) this method MUST be called before
134      * you perform operations on a service. example: before publishing stock
135      * messages to a stock topic you call this method with a producer role,
136      * after then you can as many message as you want on this topic. if you
137      * publish a message to a topic without using this method no one will
138      * consume this message and it will be lost.
139      *
140      * for a given service and a given role a one time advertise Should be done
141      * unless you recall the role using the recallService(ServiceActor
142      * serviceActor). you could at any time recall your role to a service.
143      *
144      * @param serviceActor
145      * hold the service name and the role of the invoker of this
146      * method
147      * @see org.mr.kernel.services.ServiceProducer
148      * @see org.mr.kernel.services.ServiceConsumer
149      * @throws MantaException
150      * if service not found
151      */

152     public void advertiseService(ServiceActor serviceActor, MantaAgent agent)
153         throws MantaException
154     {
155         if (this.defaultSender != null) {
156             this.defaultSender.advertiseService(serviceActor, agent);
157         }
158         if (wbSender != null) {
159             this.wbSender.advertiseService(serviceActor, agent);
160         }
161     }
162     
163     
164     /**
165      * Recalls a previously advertised role (consumer or producer) in service
166      * (queue or a topic) which is not longer accessible from this layer. if at
167      * runtime you want to stop been a consumer or a producer (i.e when going
168      * offline) you SHOULD use this method to recalls the role in the service
169      *
170      * @see org.mr.kernel.services.ServiceProducer
171      * @see org.mr.kernel.services.ServiceConsumer
172      * @throws MantaException
173      * if service not found
174      */

175     public void recallService(ServiceActor serviceActor, MantaAgent agent)
176         throws MantaException
177     {
178         if (this.defaultSender != null) {
179             this.defaultSender.recallService(serviceActor, agent);
180         }
181         if (wbSender != null) {
182             this.wbSender.recallService(serviceActor, agent);
183         }
184     }
185     
186     public void recallDurableSubscription(ServiceActor serviceActor, MantaAgent agent) throws MantaException
187     {
188         if (this.defaultSender != null) {
189             this.defaultSender.recallDurableSubscription(serviceActor, agent);
190         }
191         if (wbSender != null) {
192             this.wbSender.recallDurableSubscription(serviceActor, agent);
193         }
194     }
195
196     /**
197      * @return
198      */

199     public ControlSignalMessageSender getDefaultSender() {
200         return this.defaultSender;
201     }
202 }
203
Popular Tags