1 46 50 package org.mr.kernel.world; 51 52 53 54 import java.util.ArrayList ; 55 56 import java.util.Iterator ; 57 import java.util.LinkedHashMap ; 58 import java.util.List ; 59 import java.util.Map ; 60 import java.util.Set ; 61 62 63 import org.w3c.dom.*; 64 65 import org.mr.MantaAgentConstants; 66 import org.mr.kernel.services.MantaService; 67 import org.mr.kernel.services.topics.VirtualTopicManager; 68 import org.mr.api.jms.MantaConnection; 69 import org.mr.core.net.TransportInfo; 70 import org.mr.core.net.TransportType; 71 79 public class WorldModelerXMLUtils { 80 81 84 public WorldModelerXMLUtils() { 85 super(); 86 87 } 88 89 90 95 public static String getAttribute(Node node, String attribName) { 96 String result = null; 97 if(node != null){ 98 NamedNodeMap attributes = node.getAttributes(); 99 Node agentNameAttribute= attributes.getNamedItem(attribName); 100 if(agentNameAttribute != null) 101 result = agentNameAttribute.getNodeValue(); 102 } 103 104 return result; 105 } 106 107 110 public static List getChildTags(Node papaTag ,String childTagName ){ 111 ArrayList list = new ArrayList (); 112 NodeList tags = papaTag.getChildNodes(); 113 int size = tags.getLength(); 114 for (int i = 0; i < size; i++) { 115 Node tag = tags.item(i); 116 if(tag.getNodeType() != Node.TEXT_NODE){ 117 String tagName = tag.getNodeName(); 118 if(tagName.equalsIgnoreCase(childTagName)){ 119 list.add(tag); 120 } } } 124 return list; 125 } 126 127 128 129 public static String fromWorldMapToXML(WorldModeler src){ 130 StringBuffer buff = new StringBuffer (); 131 LinkedHashMap attrib = new LinkedHashMap (); 132 attrib.put(WorldModelerLoader.XML_ATTRIB_VER , new Long (src.getVersion())); 134 buff.append(createTag(WorldModelerLoader.XML_TAG_WORLD ,attrib , false )); 135 Iterator domains = src.getDomains().iterator(); 137 while(domains.hasNext()){ 138 attrib.clear(); 139 MantaDomain dom = (MantaDomain)domains.next(); 140 String domName = dom.getDomainName(); 141 String domDesc = dom.getDesc(); 142 attrib.put(WorldModelerLoader.XML_ATTRIB_NAME,domName ); 143 attrib.put(WorldModelerLoader.XML_ATTRIB_DESC,domDesc ); 144 buff.append(createTag(WorldModelerLoader.XML_TAG_DOMAIN , attrib, false)); 145 Iterator services = dom.getServices().iterator(); 147 while(services.hasNext()){ 148 MantaService service = (MantaService)services.next(); 149 String serviceName = service.getServiceName(); 150 if(!VirtualTopicManager.isWildCardTopic(serviceName) 151 && !serviceName.startsWith(MantaConnection.TMP_DESTINATION_PREFIX) ){ 152 String persis = String.valueOf(service.getPersistentMode() == MantaAgentConstants.PERSISTENT); 153 attrib.clear(); 154 attrib.put(WorldModelerLoader.XML_ATTRIB_NAME ,serviceName ); 155 if(service.getServiceType() ==MantaService.SERVICE_TYPE_QUEUE ){ 156 attrib.put(WorldModelerLoader.XML_ATTRIB_SERVICE_TYPE ,WorldModelerLoader.XML_QUEUE_SERVICE_TYPE_ATTRIB_VALUE ); 157 }else{ 158 attrib.put(WorldModelerLoader.XML_ATTRIB_SERVICE_TYPE ,WorldModelerLoader.XML_TOPIC_SERVICE_TYPE_ATTRIB_VALUE ); 159 } 160 attrib.put(WorldModelerLoader.XML_ATTRIB_PERSISTENT ,persis); 161 buff.append(createTag(WorldModelerLoader.XML_TAG_SERVICE , attrib, true)); 162 } 163 164 165 166 } 167 Iterator agents = dom.getAgents().iterator(); 169 while(agents.hasNext()){ 170 String agentName =(String )agents.next(); 171 attrib.clear(); 172 attrib.put(WorldModelerLoader.XML_ATTRIB_NAME ,agentName ); 173 buff.append(createTag(WorldModelerLoader.XML_TAG_PEER , attrib, false)); 174 Set transports = dom.getAgentTransportInfoList(agentName); 176 Iterator i = transports.iterator(); 177 while (i.hasNext()) { 178 TransportInfo info =(TransportInfo) i.next(); 179 if (info.getTransportInfoType() == TransportType.MWB && 182 !agentName.equals("mwb")) { 183 continue; 184 } 185 String ip =info.getIp().getHostAddress(); 186 String port = String.valueOf( info.getPort()); 187 String type = info.getTransportInfoType().toString(); 188 attrib.clear(); 189 attrib.put(WorldModelerLoader.XML_ATTRIB_IP , ip); 190 attrib.put(WorldModelerLoader.XML_ATTRIB_PORT , port); 191 attrib.put(WorldModelerLoader.XML_ATTRIB_TRANSPORT_TYPE ,type ); 192 buff.append(createTag(WorldModelerLoader.XML_TAG_TRANSPORT , attrib, true)); 193 } 194 195 buff.append(getClosingTag(WorldModelerLoader.XML_TAG_PEER)); 197 } 198 buff.append(getClosingTag(WorldModelerLoader.XML_TAG_DOMAIN)); 200 } 201 202 buff.append(getClosingTag(WorldModelerLoader.XML_TAG_WORLD)); 204 return buff.toString(); 205 } 206 207 private static String getClosingTag(String tagName){ 208 StringBuffer buff = new StringBuffer (); 209 buff.append("</"); 210 buff.append(tagName); 211 buff.append(">"); 212 buff.append("\r\n"); 214 return buff.toString(); 215 } 216 217 private static String createTag(String tagName , Map attrib , boolean closedTag){ 218 StringBuffer buff = new StringBuffer (); 219 buff.append("<"); 220 buff.append(tagName); 221 buff.append(" "); 222 if(attrib != null){ 223 Iterator attribIter = attrib.keySet().iterator(); 224 while(attribIter.hasNext()){ 225 String attName = (String )attribIter.next(); 226 Object attValue = (Object )attrib.get(attName); 227 buff.append(attName); 228 buff.append("="); 229 buff.append("'"); 230 buff.append(attValue); 231 buff.append("' "); 232 } 233 } 234 235 if(closedTag) 236 buff.append("/"); 237 buff.append(">"); 238 buff.append("\r\n"); 240 return buff.toString(); 241 } 243 } 244 | Popular Tags |