KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Created on Jan 21, 2004
48  * Manta LTD
49  */

50 package org.mr.kernel.world;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.Collection JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.HashSet JavaDoc;
56 import java.util.Set JavaDoc;
57
58 import org.mr.kernel.services.MantaService;
59
60     /**
61      * Internal object domain holds the data of this logical collection of agents and services
62      * @since Jan 14, 2004
63      * @version 1.0
64      * @author Amir Shevat
65      *
66      */

67  public class MantaDomain{
68     WorldModeler world = null;
69     String JavaDoc domainName;
70     String JavaDoc desc = "";
71     
72     public MantaDomain(WorldModeler myworld , String JavaDoc domainName){
73         world = myworld;
74         this.domainName = domainName;
75     }
76     private HashMap JavaDoc agentToTransportInfo = new HashMap JavaDoc();
77     private HashMap JavaDoc serviceNameToService = new HashMap JavaDoc();
78     
79     
80     /**
81      * TRANSPORT
82      */

83     
84     public Set JavaDoc getAgentTransportInfoList(String JavaDoc agentName){
85         return (Set JavaDoc)agentToTransportInfo.get(agentName);
86     }
87     
88     
89     protected Set JavaDoc addAgent(String JavaDoc agentName){
90         Set JavaDoc result = new HashSet JavaDoc();
91         agentToTransportInfo.put(agentName ,result);
92         // notify listener
93
if(world.getNetworkListener()!= null){
94             world.getNetworkListener().handleAgentAdded(agentName);
95         }
96             
97         return result;
98     }
99     
100     protected void removeAgent(String JavaDoc agentName){
101         agentToTransportInfo.remove(agentName);
102         if(world.getNetworkListener()!= null){
103             world.getNetworkListener().handleAgentRemoved(agentName);
104         }
105     }
106     
107     protected Set JavaDoc addAgentIfNeeded(String JavaDoc agentName){
108         Set JavaDoc result =null;
109         result = getAgentTransportInfoList(agentName);
110         if(result == null){
111             result =addAgent(agentName);
112         }
113         
114         return result;
115     }//addAgentIfNeeded
116

117     /**
118      * @return a set of all the agents in this domain
119      *
120      */

121     public Set JavaDoc getAgents(){
122         return agentToTransportInfo.keySet();
123     }
124     /**
125      * @return a set of all services in the domain
126      */

127     public synchronized Collection JavaDoc getServices(){
128         return serviceNameToService.values();
129     }
130     
131     
132     /**
133      * LOGIC
134      */

135     
136     protected synchronized void addService(String JavaDoc serviceName ,MantaService service){
137         serviceNameToService.put(serviceName , service);
138         if(world.getLogicListener() != null)
139             world.getLogicListener().handleServiceUp(serviceName,service.getServiceType(),domainName);
140     }
141     
142     protected synchronized void removeService(String JavaDoc serviceName){
143         MantaService service=(MantaService) serviceNameToService.remove(serviceName);
144         if(world.getLogicListener() != null)
145             world.getLogicListener().handleServiceDown(serviceName,service.getServiceType(),domainName);
146     }
147
148     protected MantaService getService(String JavaDoc serviceName){
149         MantaService result = (MantaService)serviceNameToService.get(serviceName);
150         return result;
151     }
152     
153     
154     
155     
156     public String JavaDoc toString(){
157         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
158         buff.append("domain :");
159         buff.append("net map :");
160         buff.append(agentToTransportInfo);
161         
162         buff.append("logical map :");
163         buff.append(serviceNameToService);
164         
165         
166         
167         return buff.toString();
168     }
169     
170
171     /**
172      * @return Returns the domainName.
173      */

174     public String JavaDoc getDomainName() {
175         return domainName;
176         
177     }
178     /**
179      * removes all the agents and services
180      *
181      */

182     protected void clear(){
183         
184         ArrayList JavaDoc toBeRemoved = new ArrayList JavaDoc(serviceNameToService.keySet());
185         for (int i = 0; i < toBeRemoved.size(); i++) {
186             removeService((String JavaDoc)toBeRemoved.get(i));
187         }
188         serviceNameToService.clear();
189         
190         toBeRemoved = new ArrayList JavaDoc(agentToTransportInfo.keySet());
191         for (int i = 0; i < toBeRemoved.size(); i++) {
192             removeAgent((String JavaDoc)toBeRemoved.get(i));
193         }
194         agentToTransportInfo.clear();
195         
196     }
197     
198     public boolean equals(Object JavaDoc other){
199         if(other == this )
200             return true;
201         if(!(other instanceof MantaDomain))
202             return false;
203         return(((MantaDomain)other).getDomainName().equals(this.getDomainName()));
204     }
205     
206      /*
207      * the next method was added by lital kasif
208      */

209     public int hashCode(){
210         return this.getDomainName().hashCode();
211     }
212     
213     /**
214      * the description of the domain (plain text)
215      * @return Returns the desc.
216      */

217     public String JavaDoc getDesc() {
218         return desc;
219     }
220
221     /**
222      * the description of the domain (plain text)
223      * @param desc The desc to set.
224      */

225     public void setDesc(String JavaDoc desc) {
226         this.desc = desc;
227     }
228
229 }// domain
230

231  
232  
233  
234  
235  
236  
237
Popular Tags