1 22 package org.jboss.services.binding; 23 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import javax.management.ObjectName ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NodeList ; 37 38 import org.jboss.util.StringPropertyReplacer; 39 import org.jboss.logging.Logger; 40 41 50 public class XMLServicesStore implements ServicesStore 51 { 52 private final Logger log = Logger.getLogger(getClass()); 53 54 56 private Map servers = Collections.synchronizedMap(new HashMap ()); 57 58 66 public void addService(String serverName, ObjectName serviceName, ServiceConfig config) 67 throws DuplicateServiceException 68 { 69 throw new UnsupportedOperationException ("XMLServiceStore is read-only"); 70 } 71 72 81 public ServiceConfig getService(String serverName, ObjectName serviceName) 82 { 83 Map serverMap = (Map ) this.servers.get(serverName); 84 ServiceConfig config = null; 85 if( serverMap != null ) 86 { 87 config = (ServiceConfig) serverMap.get(serviceName); 88 } 89 return config; 90 } 91 92 98 public void removeService(String serverName, ObjectName serviceName) 99 { 100 throw new UnsupportedOperationException ("XMLServiceStore is read-only"); 101 } 102 103 108 public void load(URL cfgURL) 109 throws Exception 110 { 111 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 112 DocumentBuilder parser = factory.newDocumentBuilder(); 113 InputStream cfgIS = cfgURL.openStream(); 114 Document configDoc = parser.parse(cfgIS, cfgURL.toString()); 115 Element serviceBindings = configDoc.getDocumentElement(); 116 NodeList servers = serviceBindings.getElementsByTagName("server"); 117 int length = servers.getLength(); 118 for(int s = 0; s < length; s ++) 119 { 120 Element server = (Element ) servers.item(s); 121 parseServer(server); 122 } 123 } 124 125 127 public void store(URL cfgURL) 128 throws Exception 129 { 130 } 131 132 136 private void parseServer(Element server) 137 throws Exception 138 { 139 String serverName = server.getAttribute("name"); 140 HashMap serverConfigurations = new HashMap (); 141 NodeList serviceConfigs = server.getElementsByTagName("service-config"); 142 int length = serviceConfigs.getLength(); 143 for(int c = 0; c < length; c ++) 144 { 145 Element config = (Element ) serviceConfigs.item(c); 146 ServiceConfig serviceConfig = new ServiceConfig(); 147 ObjectName serviceObjectName = parseConfig(config, serviceConfig); 148 serverConfigurations.put(serviceObjectName, serviceConfig); 149 } 150 this.servers.put(serverName, serverConfigurations); 151 } 152 153 156 private ObjectName parseConfig(Element config, ServiceConfig serviceConfig) 157 throws Exception 158 { 159 String serviceName = config.getAttribute("name"); 160 ObjectName serviceObjectName = new ObjectName (serviceName); 161 serviceConfig.setServiceName(serviceName); 162 163 String delegateClass = config.getAttribute("delegateClass"); 165 if( delegateClass.length() == 0 ) 166 delegateClass = "org.jboss.services.binding.AttributeMappingDelegate"; 167 Element delegateConfig = null; 168 NodeList delegateConfigs = config.getElementsByTagName("delegate-config"); 169 if( delegateConfigs.getLength() > 0 ) 170 delegateConfig = (Element ) delegateConfigs.item(0); 171 serviceConfig.setServiceConfigDelegateClassName(delegateClass); 172 serviceConfig.setServiceConfigDelegateConfig(delegateConfig); 173 174 ArrayList bindingsArray = new ArrayList (); 176 NodeList bindings = config.getElementsByTagName("binding"); 177 int length = bindings.getLength(); 178 for(int b = 0; b < length; b ++) 179 { 180 Element binding = (Element ) bindings.item(b); 181 ServiceBinding sb = parseBinding(binding); 182 bindingsArray.add(sb); 183 } 184 ServiceBinding[] tmp = new ServiceBinding[bindingsArray.size()]; 185 bindingsArray.toArray(tmp); 186 serviceConfig.setBindings(tmp); 187 return serviceObjectName; 188 } 189 190 195 private ServiceBinding parseBinding(Element binding) 196 throws Exception 197 { 198 String name = binding.getAttribute("name"); 199 if (name != null) 200 { 201 name = StringPropertyReplacer.replaceProperties(name); 202 } 203 String hostName = binding.getAttribute("host"); 204 if (hostName != null) 205 { 206 hostName = StringPropertyReplacer.replaceProperties(hostName); 207 } 208 if (hostName.length() == 0) 209 hostName = null; 210 String portStr = binding.getAttribute("port"); 211 if (portStr != null) 212 { 213 portStr = StringPropertyReplacer.replaceProperties(portStr); 214 } 215 if (portStr.length() == 0) 216 portStr = "0"; 217 log.debug("parseBinding, name='" + name + "', host='" + hostName + "'" 218 + ", port='" + portStr + "'"); 219 int port = Integer.parseInt(portStr); 220 ServiceBinding sb = new ServiceBinding(name, hostName, port); 221 return sb; 222 } 223 } 224 | Popular Tags |