KickJava   Java API By Example, From Geeks To Geeks.

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


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 14, 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.Iterator JavaDoc;
55
56 import org.apache.commons.logging.LogFactory;
57 import org.mr.MantaAgent;
58 import org.mr.MantaAgentConstants;
59 import org.mr.core.configuration.ConfigManager;
60 import org.mr.core.configuration.ConfigurationElement;
61 import org.mr.core.log.LoggerLoader;
62 import org.mr.core.log.StartupLogger;
63 import org.mr.core.net.LocalTransport;
64 import org.mr.core.net.TransportInfo;
65 import org.mr.kernel.services.MantaService;
66 import org.mr.plugins.discovery.ADControlSender;
67 import org.mr.core.log.StartupLogger;
68
69
70 /**
71  * WorldModelerLoader load the world modeler from a xml file at start up
72  * and updates the world on runtime
73  * @since Jan 14, 2004
74  * @version 1.0
75  * @author Amir Shevat
76  *
77  */

78 public class WorldModelerLoader {
79     
80     public static final String JavaDoc XML_TAG_TRANSPORT = "transport";
81     public static final String JavaDoc XML_TAG_WORLD = "world";
82     public static final String JavaDoc XML_TAG_SERVICE = "service";
83     public static final String JavaDoc XML_TAG_SERVICE_LINK = "service_link";
84     public static final String JavaDoc XML_TAG_DOMAIN = "world.domain";
85     public static final String JavaDoc XML_TAG_PEER = "peer";
86     public static final String JavaDoc XML_ATTRIB_NAME = "name";
87     public static final String JavaDoc XML_ATTRIB_DESC = "desc";
88     public static final String JavaDoc XML_ATTRIB_VER = "ver";
89     public static final String JavaDoc XML_ATTRIB_PERSISTENT = "persistent";
90     public static final String JavaDoc XML_ATTRIB_IP = "ip";
91     public static final String JavaDoc XML_ATTRIB_PORT = "port";
92     public static final String JavaDoc XML_ATTRIB_TRANSPORT_TYPE = "type";
93     public static final String JavaDoc XML_ATTRIB_ACTOR_TYPE = "actorType";
94     public static final String JavaDoc XML_ATTRIB_SERVICE_TYPE = "serviceType";
95     public static final String JavaDoc XML_MY_TRANSPOT = "network.transports.transport";
96     // the values for attribute type in service tag
97

98     public static final String JavaDoc XML_CONSUMER_TYPE_ATTRIB_VALUE = "consumer";
99     public static final String JavaDoc XML_QUEUE_SERVICE_TYPE_ATTRIB_VALUE = "queue";
100     public static final String JavaDoc XML_TOPIC_SERVICE_TYPE_ATTRIB_VALUE = "topic";
101     
102     public static String JavaDoc WORLD_XML_FILE_LOCATION = null;
103     
104         
105     public static void init(WorldModeler world){
106         ConfigManager config =MantaAgent.getInstance().getSingletonRepository().getConfigManager();
107         
108         // set the agent name and default domain
109
String JavaDoc myAgentName = config.getStringProperty("my-peer.name", "m%ip%%port%");
110         myAgentName = generateRealName(myAgentName);
111         String JavaDoc defaultDomain = config.getStringProperty("my-peer.domain", "myDomain");
112         boolean dynamicServiceCreation = config.getBooleanProperty("jms.dynamic_service_creation",true);
113         world.setDynamicServiceCreation(dynamicServiceCreation);
114         //System.out.println("Dynamic Service Creation is "+(dynamicServiceCreation?"ENABLED.":"DISABLED."));
115
StartupLogger.log.info("Dynamic Service Creation is "+(dynamicServiceCreation?"ENABLED.":"DISABLED."), "WorldModelerLoader");
116         world.setMyAgentName(myAgentName);
117         String JavaDoc title = "*** MantaRay Layer Name: "+myAgentName+" ***";
118         // this standard output print is required to know where to look for the log.
119
System.out.println(title);
120         //StartupLogger.log.info(title, "WorldModelerLoader");
121
if(config.hasLog4JConfig()){
122             LoggerLoader.init(myAgentName);
123             config.initLogger();
124         }
125         StartupLogger.log.flush();
126         world.setDefaultDomainName(defaultDomain);
127         MantaAgent.getInstance().getSingletonRepository().getNetworkManager().setMyAgentName(myAgentName);
128         
129     }
130     
131     private static String JavaDoc generateRealName(String JavaDoc myAgentName) {
132         String JavaDoc result = null;
133         if(myAgentName.indexOf("%")==-1){
134             result = myAgentName;
135         }else{
136             Collection JavaDoc transports = MantaAgent.getInstance().getSingletonRepository().getNetworkManager().getLocalTransports();
137             Iterator JavaDoc i = transports.iterator();
138             if (i.hasNext()) {
139                 LocalTransport lt = (LocalTransport) i.next();
140                 String JavaDoc port = String.valueOf(lt.getInfo().getPort()) ;
141                 String JavaDoc ip = lt.getInfo().getIp().getHostAddress();
142                 if(ip.equals("0.0.0.0")){
143                     ip =ADControlSender.getValidLocalAddress();
144                 }
145                 myAgentName = myAgentName.replaceAll("%port%",port );
146                 myAgentName = myAgentName.replaceAll("%ip%",ip );
147                 result = myAgentName;
148             }
149             else {
150                 //System.out.println("FATAL Could not find transport information for this layer");
151
StartupLogger.log.fatal("Could not find transport information for this layer.", "WorldModelerLoader");
152                 result = "UNKNOWN";
153             }
154             
155         }
156         
157         return result;
158     }
159
160     public static void load(WorldModeler world) {
161         ConfigManager config =MantaAgent.getInstance().getSingletonRepository().getConfigManager();
162         
163         //init(world);
164
try{
165             
166             String JavaDoc ver = "0";
167             if(ver != null && ver.length()>0){
168                 world.setVersion(Long.parseLong(ver));
169             }
170             ArrayList JavaDoc domainNodeList= config.getConfigurationElements(XML_TAG_DOMAIN);
171             int limit = domainNodeList.size();
172             // go over the domain
173
for (int count =0 ; count<limit ; count++){
174                 ConfigurationElement domainConfigElement = (ConfigurationElement) domainNodeList.get(count);
175                 
176                 // get domain name
177
String JavaDoc domain = world.getDefaultDomainName();
178                 world.addDomain(domain);
179                 String JavaDoc domainDesc = null;
180                 
181                 if(domainConfigElement.getSubConfigurationElementByName(XML_ATTRIB_DESC)!= null){
182                     domainDesc =domainConfigElement.getSubConfigurationElementByName(XML_ATTRIB_DESC).getValue();
183                 }
184                     
185                 if(domainDesc != null){
186                     world.getDomain(domain).setDesc(domainDesc);
187                 }
188                 // parse services
189
ArrayList JavaDoc services = domainConfigElement.getSubConfigurationElementsByName(XML_TAG_SERVICE);
190                 if(services!= null){
191                     int serviceNum = services.size();
192                     for (int i = 0; i < serviceNum; i++) {
193                         ConfigurationElement serviceConfigElement =
194                             (ConfigurationElement)services.get(i) ;
195                         createService(serviceConfigElement ,domain , world );
196                         
197                     }
198                 }
199                 
200                 //parse other peers
201
ArrayList JavaDoc peers = domainConfigElement.getSubConfigurationElementsByName(XML_TAG_PEER);
202                 //NodeList agents = domainNode.getChildNodes();
203
if(peers!=null){
204                     int peersNum =peers.size();
205                     for (int peersCount =0 ; peersCount<peersNum ; peersCount++){
206                         ConfigurationElement peer =(ConfigurationElement)peers.get(peersCount) ;
207                         parsePeer(peer , domain , world);
208                     }//for
209
}
210                 
211                 
212             }//for
213

214         }catch(Exception JavaDoc e){
215                 //if (MantaAgent.getInstance().geLogLevel()<=MantaAgentConstants.FATAL)
216
LogFactory.getLog("WorldModelerLoader").fatal("can not load world modeler ", e);
217
218         }
219         
220         //if (MantaAgent.getInstance().geLogLevel()<=MantaAgentConstants.DEBUG)
221
LogFactory.getLog("WorldModelerLoader").debug("world modeler loaded"+world);
222         
223     }// load
224

225     
226     /**
227      * @param world
228      * @param domain
229      * @param service
230      * @return
231      */

232     private static MantaService createService(ConfigurationElement serviceConfigElement, String JavaDoc domain, WorldModeler world) {
233         String JavaDoc serviceType = serviceConfigElement.getSubConfigurationElementByName(XML_ATTRIB_SERVICE_TYPE).getValue() ;
234         String JavaDoc serviceName = serviceConfigElement.getSubConfigurationElementByName(XML_ATTRIB_NAME).getValue() ;
235         String JavaDoc persistent = serviceConfigElement.getSubConfigurationElementByName(XML_ATTRIB_PERSISTENT).getValue() ;
236         
237         MantaService service;
238         if(serviceType.equalsIgnoreCase(XML_QUEUE_SERVICE_TYPE_ATTRIB_VALUE)){
239             service = world.createService(domain,serviceName, MantaService.SERVICE_TYPE_QUEUE);//new QueueService(serviceName );
240
}else{
241             service = world.createService(domain,serviceName, MantaService.SERVICE_TYPE_TOPIC);
242         }
243             
244         if(persistent!= null){
245             if(persistent.equalsIgnoreCase("true")){
246                 service.setPersistentMode(MantaAgentConstants.PERSISTENT);
247             }else{
248                 service.setPersistentMode(MantaAgentConstants.NON_PERSISTENT);
249             }
250         }
251         
252         return service;
253     }
254
255     private static void parsePeer(ConfigurationElement peer ,String JavaDoc domain , WorldModeler world){
256         
257         String JavaDoc agentName = peer.getSubConfigurationElementByName(XML_ATTRIB_NAME).getValue() ;
258         //parse the ransport info
259
ArrayList JavaDoc transports = peer.getSubConfigurationElementsByName(XML_TAG_TRANSPORT);
260         
261         int size =transports.size();
262         for (int i = 0; i < size; i++) {
263             ConfigurationElement TransProp = (ConfigurationElement) transports.get(i);
264             TransportInfo info = getTransportInfo(TransProp);
265             world.addTransportInfoToAgent(domain ,agentName , info );
266             
267         }
268         
269     }//parseAgent
270

271     
272     
273     
274
275     /**
276      * @param agentProp
277      * @return
278      */

279     private static TransportInfo getTransportInfo(ConfigurationElement agentProp) {
280         TransportInfo info ;
281         
282         String JavaDoc ip = agentProp.getSubConfigurationElementByName(XML_ATTRIB_IP).getValue() ;
283         
284         String JavaDoc port = agentProp.getSubConfigurationElementByName(XML_ATTRIB_PORT).getValue() ;
285         
286         String JavaDoc type = agentProp.getSubConfigurationElementByName(XML_ATTRIB_TRANSPORT_TYPE).getValue() ;
287         
288         info = new TransportInfo(ip,Integer.parseInt(port),type);
289         return info;
290     }
291     
292     /**
293      * after changes are made to the world in the console the new world is now distributed
294      * to all the agents , this method updates the old system world with the deltas from the new world
295      * @param oldWorld the current world
296      * @param newWorld the new world from the console
297     
298     public static void updateWorld(WorldModeler oldWorld , WorldModeler newWorld){
299         removeUnneededElements(oldWorld ,newWorld );
300         addNeededElements(oldWorld , newWorld);
301         oldWorld.setVersion(newWorld.getVersion());
302         
303     }
304      
305     private static void removeUnneededElements(WorldModeler oldWorld , WorldModeler newWorld){
306         
307         Set oldDomains = oldWorld.getDomains();
308         // remove the elements that are in the old but not in the new
309         Iterator oldDomIter = oldDomains.iterator();
310         MantaDomain dom;
311         while(oldDomIter.hasNext()){
312             // remove domain if needed
313             dom = (MantaDomain)oldDomIter.next();
314             
315             if(newWorld.getDomain(dom.getDomainName())== null){
316                 dom.clear();
317                 oldWorld.removeDomain(dom.getDomainName());
318                 continue;
319             }
320             // remove services if needed
321             Set oldServices = oldWorld.getServices(dom.getDomainName());
322             Iterator oldSerIter = oldServices.iterator();
323             while(oldSerIter.hasNext()){
324                 MantaService curService = (MantaService)oldSerIter.next();
325                 MantaService newService = newWorld.getService(dom.getDomainName(),curService.getServiceName(),curService.getServiceType());
326                 if(newService == null){
327                     oldWorld.removeService(dom.getDomainName() ,curService.getServiceName());
328                 }
329             }//while(newSerIter
330             // remove agents if needed
331             Set oldAgents= oldWorld.getAgents(dom.getDomainName());
332             // done to prevent concurent modification exception
333             Set tempSet = new HashSet();
334             tempSet.addAll(oldAgents);
335             Iterator agentsIter =tempSet.iterator();
336             while(agentsIter.hasNext()){
337                 String agentName = (String)agentsIter.next();
338                 Set newAgentTransports = newWorld.getAgentTransportInfo(dom.getDomainName(),agentName);
339                 Set curAgentTransports = oldWorld.getAgentTransportInfo(dom.getDomainName(),agentName );
340                 if(newAgentTransports == null || newAgentTransports.isEmpty()){
341                     oldWorld.removeAgent(dom.getDomainName(), agentName);
342                 }else{
343                     Iterator i = curAgentTransports.iterator();
344                     while (i.hasNext()) {
345                         TransportInfo info = (TransportInfo) i.next();
346                         if(!newAgentTransports.contains(info))
347                             oldWorld.removeTransportInfoFromAgent(dom.getDomainName(), agentName,info);
348                     }
349                 }
350             }
351         }//while(newDomIter
352     }
353     */

354     /*
355     private static void addNeededElements(WorldModeler oldWorld , WorldModeler newWorld){
356         Set newDomains = newWorld.getDomains();
357         
358         // add the elements that are in the new and are not in the old\
359         Iterator newDomIter = newDomains.iterator();
360         MantaDomain dom;
361         while(newDomIter.hasNext()){
362             // add domain if needed
363             dom = (MantaDomain)newDomIter.next();
364             MantaDomain oldDomain =oldWorld.getDomain(dom.getDomainName());
365             if(oldDomain== null){
366                 oldDomain = oldWorld.addDomain(dom.getDomainName());
367             }
368             oldDomain.setDesc(dom.getDesc());
369         
370             
371             // add services if needed
372             Set newServices = newWorld.getServices(dom.getDomainName());
373             Iterator newSerIter = newServices.iterator();
374             while(newSerIter.hasNext()){
375                 MantaService newService = (MantaService)newSerIter.next();
376                 MantaService curService = oldWorld.getService(dom.getDomainName(),newService.getServiceName(), newService.getServiceType());
377                 if(curService == null){
378                     oldWorld.addService(dom.getDomainName() ,newService );
379                 }else if(newService.getServiceType() != curService.getServiceType()){
380                     // same name not same service type
381                     oldWorld.removeService(dom.getDomainName(),curService.getServiceName() );
382                     oldWorld.addService(dom.getDomainName() , newService);
383                 }
384             }//while(newSerIter
385             // add agents if needed
386             Set newAgents= newWorld.getAgents(dom.getDomainName());
387             Iterator agentsIter =newAgents.iterator();
388             while(agentsIter.hasNext()){
389                 String agentName = (String)agentsIter.next();
390                 Set newAgentTransports= newWorld.getAgentTransportInfo(dom.getDomainName(),agentName);
391                 Set curAgentTransports = oldWorld.getAgentTransportInfo(dom.getDomainName(),agentName );
392                 Iterator i = newAgentTransports.iterator();
393                 while (i.hasNext()) {
394                     TransportInfo newInfo = (TransportInfo) i.next();;
395                     if(curAgentTransports == null || !curAgentTransports.contains(newInfo)){
396                         oldWorld.addTransportInfoToAgent(dom.getDomainName() ,agentName ,newInfo);
397                     }
398                 }
399                 
400             }
401         }//while(newDomIter
402     }
403     */

404     /*
405     public static void main(String args[]){
406         WorldModelerLoader loader = new WorldModelerLoader();
407         File f = new File("C:/manta/software/config/world.xml");
408         loader.Load(f);
409     }
410     */

411     
412     
413
414
415     
416
417 }
418
Popular Tags