KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > world > WorldModeler


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.world;
47
48 import java.io.File JavaDoc;
49 import java.io.FileWriter JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.Writer JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.HashSet JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Set JavaDoc;
56
57 import org.apache.commons.logging.LogFactory;
58 import org.mr.api.jms.MantaConnection;
59 import org.mr.core.net.TransportInfo;
60 import org.mr.kernel.services.MantaService;
61 import org.mr.kernel.services.ServiceFactory;
62
63 /**
64  * This class represents the manta world - hold the data about services and other remote
65  * manta layers.
66  * in the manta layer there should be only 1 world modular (singleton) but in cases of updates a new world can
67  * be sent by the net
68  *
69  * @version 1.0
70  * @since Jan 13, 2004
71  * @author Amir Shevat
72  *
73  */

74 public class WorldModeler implements WorldModelerMBean{
75         
76         private String JavaDoc myAgentName;
77         
78         /**
79          * the consumers of this layer. This is used by the GetServiceParticipation class and by the
80          * ServiceRecallShutdownHook class
81          */

82         private Set JavaDoc myConsumedServices = new HashSet JavaDoc();
83         /**
84          * the producers of this layer. This is used by the GetServiceParticipation class and by the
85          * ServiceRecallShutdownHook class
86          */

87         private Set JavaDoc myProducedServices = new HashSet JavaDoc();
88         
89         /**
90          * the coordinated services of this layer.This is used by the GetServiceParticipation class and by the
91          * ServiceRecallShutdownHook class
92          */

93         private Set JavaDoc myCoordinatedServices = new HashSet JavaDoc();
94         /**
95          * for now the only domain
96          */

97         private String JavaDoc defaultDomain;
98         /**
99          * a map of all domains
100          */

101         private HashMap JavaDoc domains =new HashMap JavaDoc();
102         
103         private static WorldModeler instance = new WorldModeler();
104         /**
105          * will be notified of changes in the network data (ports,ip ...)
106          */

107         private WorldModelerNetListenerMultiplexer netListener = null;
108         /**
109          * will be notified of logical data changes (topics queues ....)
110          */

111         private WorldModelerLogicListener logicListener = null;
112         /**
113          * the version of update of the world map
114          */

115         private long version =0;
116         
117         /**
118          * if this is true services will be dynamically created when needed
119          */

120         public boolean dynamicServiceCreation ;
121         
122         /**
123          *
124          * constructor for WorldModeler
125          */

126         private WorldModeler() {
127             netListener = new WorldModelerNetListenerMultiplexer();
128         }
129         
130         /**
131          * a singleton method
132          * @return the world instance
133          */

134         public static WorldModeler getInstance(){
135             return instance;
136         }
137         
138         
139         
140         
141         //*************
142
//* transport *
143
//*************
144

145         /**
146          * adds an agent to a domain with his transport info
147          * if an agent already exist he will be overwritten
148          * @param domain the domain this agent belongs to
149          * @param agentName the unique id of this agent
150          * @param transportInfo the transport Information Known about this agent
151          */

152         public void addAgent(String JavaDoc domain , String JavaDoc agentName , List JavaDoc transportInfo){
153             
154             createDomainIfNeeded(domain).addAgent(agentName).addAll(transportInfo);
155             if(netListener!= null)
156                 netListener.handleAgentsTransportsAdded(agentName ,transportInfo);
157             
158         }
159         
160         
161         /**
162          * removes an agent and all of it transport info
163          * @param domain the domain of the agent
164          * @param agentName the name of the agent
165          */

166         public void removeAgent(String JavaDoc domain, String JavaDoc agentName) {
167             MantaDomain dom = getDomain(domain);
168             dom.removeAgent(agentName);
169             if (netListener != null) {
170                 netListener.handleAgentRemoved(agentName);
171             }
172         }
173
174         
175         /**
176          * adds transport info to an existing agent or creating an agent entry and adding it to it
177          * @param domain the domain this agent belongs to
178          * @param agentName the unique id of this agent
179          * @param info the transport information known about this agent
180          * @returns <code>true</code> if the world modular didn't
181          * already contain the transport information.
182          */

183     public boolean addTransportInfoToAgent(String JavaDoc domain, String JavaDoc agentName,
184                                            TransportInfo info) {
185         boolean retval =
186             createDomainIfNeeded(domain).addAgentIfNeeded(agentName).add(info);
187         if (netListener!= null && retval == true) {
188             netListener.handleAgentsTransportAdded(agentName , info);
189         }
190         return retval;
191     }
192         
193     /**
194      * removes an agent transport info from his transport list
195      * @param domain the domain this agent belongs too
196      * @param agentName the agent name
197      * @param info the transport info to be removed
198      * @return <code>true</code> if the world modeler contained
199      * the specified transport info.
200      */

201     public boolean removeTransportInfoFromAgent(String JavaDoc domain, String JavaDoc agentName,
202                                                 TransportInfo info)
203     {
204         Set JavaDoc transportList = getAgentTransportInfo(domain, agentName);
205         boolean retval = false;
206         if (transportList != null) {
207             retval = transportList.remove(info);
208             if (netListener!= null)
209                 netListener.handleAgentsTransportRemoved(agentName , info);
210         }
211         return retval;
212     }//removeTransportInfoFromAgent
213

214                 
215         /**
216          * returns a set of all the transport information know about this agent
217          * @param domain the domain this agent belongs to
218          * @param agentName the unique id of this agent
219          * @return a list of TransportInfo objects or null if not found
220          */

221         public Set JavaDoc getAgentTransportInfo(String JavaDoc domain , String JavaDoc agentName){
222             MantaDomain curDomain = getDomain(domain);
223             if(curDomain != null)
224                 return curDomain.getAgentTransportInfoList(agentName);
225             return null;
226         }
227         
228         
229         
230         /**
231          * returns a set of agents name of a given domain
232          * @param domain - the domain we want the list of
233          * @return a set of agents name for this domain
234          */

235         public Set JavaDoc getAgents(String JavaDoc domain){
236             Set JavaDoc result = null;
237             MantaDomain dom = getDomain(domain);
238             if(dom != null)
239                 result = dom.getAgents();
240             if(result == null)
241                 result = new HashSet JavaDoc();
242             return result;
243         }
244         
245         
246         
247         //**********
248
//* logic *
249
//**********
250

251         /**
252          * @return a set of services of a given domain
253          */

254         public Set JavaDoc getServices(String JavaDoc domain){
255             HashSet JavaDoc result = new HashSet JavaDoc();
256             MantaDomain dom = getDomain(domain);
257             if(dom != null){
258                 synchronized(dom){
259                     result.addAll(dom.getServices()) ;
260                 }
261             }
262             return result;
263         }
264         
265         
266         /**
267          * @param domain currently only default domain
268          * @param service the name of the Queue or Topic
269          * @param serviceType needed for dynamic service creation can be MantaService.SERVICE_TYPE_QUEUE or SERVICE_TYPE_TOPIC
270          *
271          * @return the manta service with that name in that domain or null if not found
272          */

273         public synchronized MantaService getService(String JavaDoc domain ,String JavaDoc service, byte serviceType){
274             MantaDomain dom = getDomain(domain);
275             MantaService result = null;
276             if(dom != null){
277                 
278                 result = dom.getService(service);
279                 if(result == null){
280                     if(dynamicServiceCreation || service.startsWith(MantaConnection.TMP_DESTINATION_PREFIX)){
281                         return createService(domain, service, serviceType);
282                     }else{
283                         LogFactory.getLog("WorldModeler").warn("service "+service+" not created dynamicServiceCreation = "+dynamicServiceCreation);
284                     }
285                     
286                 }// if
287
}// if
288
return result;
289         }//getService
290

291         /**
292          * @param domain currently only default domain
293          * @param service the name of the Queue or Topic
294          * @param serviceType needed for dynamic service creation can be MantaService.SERVICE_TYPE_QUEUE or SERVICE_TYPE_TOPIC
295          *
296          * @return the manta service with that name in that domain or a new one if not found
297          */

298          synchronized MantaService createService(String JavaDoc domain ,String JavaDoc service, byte serviceType){
299             MantaDomain dom = getDomain(domain);
300             MantaService result = null;
301             if(dom != null){
302                 
303                 result = dom.getService(service);
304                 if(result == null ){
305                     result = ServiceFactory.createService(service, serviceType);
306                     addService(domain, result);
307                     
308                 }// if
309
}// if
310
return result;
311         }//getService
312

313         /**
314          * returns true if service is in the world modeler else false
315          * @param domain currently only default domain
316          * @param service the name of the Queue or Topic
317          * @return true if service is in the world modeler else false
318          */

319         public synchronized boolean containsService(String JavaDoc domain ,String JavaDoc service){
320             MantaDomain dom = getDomain(domain);
321             if(dom != null){
322                 return dom.getService(service) != null;
323             }
324             return false;
325         }
326         
327         /**
328          * adds the service to the world map if the service is not already there
329          * @param domain the domain of the service
330          * @param service the service to be added
331          */

332         public void addService(String JavaDoc domain ,MantaService service){
333             MantaDomain dom = createDomainIfNeeded(domain);
334             MantaService ser = dom.getService(service.getServiceName());
335             if(ser == null)
336                 dom.addService(service.getServiceName() , service);
337             
338         }
339
340         /**
341          * removes a queue or a topic from the data container and notifies the
342          * logical listener
343          * @param domain the domain of the service
344          * @param serviceName the name of the queue or the topic
345          */

346         public void removeService(String JavaDoc domain, String JavaDoc serviceName) {
347             MantaDomain dom = getDomain(domain);
348             if(dom!= null){
349                 dom.removeService(serviceName);
350             }
351                 
352             
353             
354         }
355         
356         //*********************
357
//* end of logic *
358
//*********************
359
public String JavaDoc toString(){
360             
361             return " myAgent="+myAgentName+" defaultDomain="+defaultDomain+" world map:"+domains;
362         }
363         
364         
365         //////////////////////////
366
// domain methods //
367
//////////////////////////
368

369         /**
370          * @return Returns the defaultDomain.
371          */

372         public String JavaDoc getDefaultDomainName() {
373             return defaultDomain;
374         }
375
376         /**
377          * @param defaultDomain The defaultDomain to set.
378          */

379         public void setDefaultDomainName(String JavaDoc defaultDomain) {
380             this.defaultDomain = defaultDomain;
381         }
382
383         /**
384          * @return Returns the myAgentName.
385          */

386         public String JavaDoc getMyAgentName() {
387             return myAgentName;
388         }
389
390         /**
391          * @param myAgentName The myAgentName to set.
392          */

393         public void setMyAgentName(String JavaDoc myAgentName) {
394             this.myAgentName = myAgentName;
395         }
396         
397         /**
398          * adds a domain with no agent
399          * @param domainName
400          */

401         public MantaDomain addDomain(String JavaDoc domainName){
402             if(!domains.containsKey(domainName)){
403                 domains.put(domainName , new MantaDomain(this ,domainName));
404             }
405             return (MantaDomain)domains.get(domainName);
406         }
407         
408         public void removeDomain(String JavaDoc domainName){
409             domains.remove(domainName);
410         }
411         
412         
413         
414         /**
415          * gets the domains from the world map
416          * @return a set of MantaDomain objects
417          */

418         public Set JavaDoc getDomains(){
419             Set JavaDoc result = new HashSet JavaDoc();
420             
421             if(domains != null){
422                 result.addAll(domains.values()) ;
423             }
424             return result;
425         }
426         
427         
428         public final MantaDomain getDomain(String JavaDoc domainName){
429             return (MantaDomain)domains.get(domainName);
430         }
431         
432         private MantaDomain createDomainIfNeeded(String JavaDoc domainName){
433                             
434             return addDomain(domainName);
435             
436         }
437         
438         //////////////////
439
// listeners //
440
//////////////////
441
/**
442          * adds a listener to be notified of network changes in the world map
443          * @param listener this object will be notified
444         */

445         
446         public WorldModelerNetListenerMultiplexer getNetworkListener(){
447             return netListener;
448         }
449         
450         /**
451          * adds a listener to be notified of logical changes in the world map
452          * @param listener this object will be notified
453          */

454         public void setLogicListener(WorldModelerLogicListener listener){
455             logicListener = listener;
456         }
457         
458         public WorldModelerLogicListener getLogicListener(){
459             return logicListener;
460         }
461
462
463         
464
465
466         
467         
468
469         /**
470          * @return Returns the version.
471          */

472         public long getVersion() {
473             return version;
474         }
475
476         /**
477          * @param version The version to set.
478          */

479         public void setVersion(long version) {
480             this.version = version;
481         }
482
483         /**
484          * @return Returns a copy of the myConsumedServices.
485          */

486         public synchronized Set JavaDoc getMyConsumedServices() {
487             return new HashSet JavaDoc(myConsumedServices);
488         }
489         
490         public synchronized void removeConsumedServices(MantaService service) {
491             myConsumedServices.remove(service);
492         }
493         
494         /**
495          * adds a service that is consumed by this peer
496          * @param service -the service that is consumed by this peer
497          */

498         public synchronized void addConsumedServices(MantaService service) {
499             myConsumedServices.add(service);
500         }
501         /**
502          * @return Returns a copy of the myProducedServices.
503          */

504         public synchronized Set JavaDoc getMyProducedServices() {
505             return new HashSet JavaDoc(myProducedServices);
506         }
507         
508         public synchronized void removeProducedService(MantaService service) {
509             myProducedServices.remove(service);
510         }
511         
512         
513         public synchronized void addProducedService(MantaService service) {
514             myProducedServices.add(service);
515         }
516         
517         /**
518          * @return Returns the myCoordinatedServices.
519          */

520         public Set JavaDoc getMyCoordinatedServices() {
521           return new HashSet JavaDoc(myCoordinatedServices);
522         }
523         
524         public synchronized void removeCoordinatedService(MantaService service) {
525             myCoordinatedServices.remove(service);
526         }
527         
528         
529         public synchronized void addCoordinatedService(MantaService service) {
530             myCoordinatedServices.add(service);
531         }
532
533     /**
534      * Save this world modeler as XML to a Writer object. The
535      * Writer is flushed but not closed.
536      */

537     public void save(Writer JavaDoc writer) throws IOException JavaDoc {
538         String JavaDoc worldXML = WorldModelerXMLUtils.fromWorldMapToXML(this);
539         writer.write(worldXML);
540         writer.flush();
541     }
542
543     /**
544      * Save this world modeler as XML to a file.
545      */

546     public void save(File JavaDoc file) throws IOException JavaDoc {
547         FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
548         save(writer);
549         writer.close();
550     }
551
552     
553     protected void setDynamicServiceCreation(boolean dynamicServiceCreation) {
554         this.dynamicServiceCreation = dynamicServiceCreation;
555     }
556     
557     protected void setNetListener(WorldModelerNetListenerMultiplexer netListener) {
558         this.netListener = netListener;
559     }
560 }
561
Popular Tags