1 22 package org.jboss.services.binding; 23 24 import java.net.URL ; 25 26 import javax.management.Attribute ; 27 import javax.management.MBeanRegistration ; 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.logging.Logger; 32 import org.jboss.mx.util.JMXExceptionDecoder; 33 import org.jboss.mx.util.MBeanProxyExt; 34 import org.jboss.system.ServiceControllerMBean; 35 import org.jboss.system.server.ServerConfigLocator; 36 37 53 public class ServiceBindingManager 54 implements MBeanRegistration , ServiceBindingManagerMBean 55 { 56 private static Logger log = Logger.getLogger(ServiceBindingManager.class); 57 58 60 private MBeanServer server; 61 62 64 private ObjectName serviceName; 65 66 69 private String serverName; 70 71 74 private String storeFactoryClassName = "org.jboss.services.binding.XMLServicesStoreFactory"; 75 76 78 private ObjectName serviceController; 79 80 82 private ServicesStore store; 83 84 86 private URL storeURL; 87 88 90 93 public String getServerName() 94 { 95 return this.serverName; 96 } 97 98 101 public void setServerName(String serverName) 102 { 103 this.serverName = serverName; 104 } 105 106 109 public String getStoreFactoryClassName() 110 { 111 return this.storeFactoryClassName; 112 } 113 114 117 public void setStoreFactoryClassName(String storeFactoryClassName) 118 { 119 this.storeFactoryClassName = storeFactoryClassName; 120 } 121 122 125 public URL getStoreURL() 126 { 127 return storeURL; 128 } 129 130 133 public void setStoreURL(URL storeURL) 134 { 135 this.storeURL = storeURL; 136 } 137 138 142 public ObjectName getServiceController() 143 { 144 return serviceController; 145 } 146 147 155 public void setServiceController(ObjectName serviceController) throws Exception 156 { 157 this.serviceController = serviceController; 158 159 registerProxy(); 161 } 162 163 165 public ObjectName preRegister(MBeanServer server, ObjectName name) 166 throws Exception 167 { 168 this.server = server; 169 this.serviceName = name; 170 171 return name; 172 } 173 174 public void postRegister(Boolean registrationDone) 175 { 176 if (registrationDone.booleanValue()) 177 { 178 try 180 { 181 setServiceController(ServiceControllerMBean.OBJECT_NAME); 182 } 183 catch (Exception ignore) 184 { 185 } 188 } 189 } 190 191 public void preDeregister() 192 throws Exception 193 { 194 this.unregisterProxy(); 196 197 if (store != null) 199 { 200 store.store(storeURL); 201 } 202 } 203 204 public void postDeregister() 205 { 206 } 208 209 211 220 public ServiceConfig getServiceConfig(ObjectName serviceName) 221 throws Exception 222 { 223 log.debug("getServiceConfig, server:" + serverName + ", serviceName:" + serviceName); 224 225 if (store == null) 226 { 227 initStore(); 228 } 229 230 ServiceConfig svcConfig = store.getService(serverName, serviceName); 232 ServiceConfig configCopy = null; 233 if (svcConfig != null) 234 { 235 configCopy = (ServiceConfig) svcConfig.clone(); 236 } 237 return configCopy; 238 } 239 240 251 public void applyServiceConfig(ObjectName serviceName) 252 throws Exception 253 { 254 if (store == null) 255 { 256 initStore(); 257 } 258 ServiceConfig svcConfig = store.getService(serverName, serviceName); 260 if (svcConfig != null) 261 { 262 log.debug("applyServiceConfig, server:" + serverName + ", serviceName:" + serviceName + ", config=" + svcConfig); 263 264 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 265 String delegateName = svcConfig.getServiceConfigDelegateClassName(); 266 if (delegateName != null) 267 { 268 Class delegateClass = loader.loadClass(delegateName); 269 ServicesConfigDelegate delegate = (ServicesConfigDelegate) delegateClass.newInstance(); 270 delegate.applyConfig(svcConfig, server); 271 } 272 } 273 } 274 275 277 280 private void registerProxy() throws Exception 281 { 282 if (serviceController != null) 283 { 284 log.debug("Registering with ServiceController: " + serviceController); 285 286 org.jboss.system.ServiceBinding proxy = (org.jboss.system.ServiceBinding) 288 MBeanProxyExt.create(org.jboss.system.ServiceBinding.class, serviceName, server); 289 290 try 291 { 292 server.setAttribute(serviceController, new Attribute ("ServiceBinding", proxy)); 293 } 294 catch (Exception e) 295 { 296 Throwable t = JMXExceptionDecoder.decode(e); 298 log.debug("Failed to register with ServiceController: " + serviceController + ", reason: " + t.getMessage()); 299 throw (Exception )t; 300 } 301 } 302 } 303 304 308 private void unregisterProxy() 309 { 310 if (serviceController != null) 311 { 312 log.debug("Unregistering with ServiceController: " + serviceController); 313 try 314 { 315 server.setAttribute(serviceController, new Attribute ("ServiceBinding", null)); 316 } 317 catch (Exception e) 318 { 319 Throwable t = JMXExceptionDecoder.decode(e); 321 log.debug("Failed to unregister with ServiceController: " + serviceController + ", reason: " + t.getMessage()); 322 } 323 } 324 } 325 326 329 private void initStore() throws Exception 330 { 331 log.info("Initializing store"); 332 333 if (this.storeURL == null) 335 { 336 this.storeURL = ServerConfigLocator.locate().getServerConfigURL(); 337 } 338 log.info("Using StoreURL: "+storeURL); 339 340 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 341 Class factoryClass = loader.loadClass(storeFactoryClassName); 342 ServicesStoreFactory storeFactory = (ServicesStoreFactory) factoryClass.newInstance(); 343 344 store = storeFactory.newInstance(); 346 store.load(storeURL); 347 } 348 } 349 | Popular Tags |