1 7 package org.ejtools.jmx.browser.model; 8 9 import java.beans.beancontext.BeanContextServices; 10 import java.util.Collection; 11 import java.util.Enumeration; 12 import java.util.Hashtable; 13 import java.util.Iterator; 14 import java.util.ResourceBundle; 15 import java.util.Set; 16 17 import javax.management.ObjectInstance; 18 import javax.management.ObjectName; 19 import javax.management.QueryExp; 20 21 import org.apache.log4j.Logger; 22 import org.ejtools.jmx.browser.model.service.CacheService; 23 import org.ejtools.jmx.browser.model.service.ConnectionMetaData; 24 import org.ejtools.jmx.browser.model.service.ConnectionService; 25 import org.ejtools.util.service.Profile; 26 import org.ejtools.util.service.ProfileHolder; 27 28 79 public class Server extends Node implements ProfileHolder 80 { 81 82 protected int count = 0; 83 84 protected String defaultDomain = ""; 85 86 protected Profile profile = null; 87 88 private static Logger logger = Logger.getLogger(Server.class); 89 90 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources"); 91 92 93 94 public Server() 95 { 96 super(); 97 this.setName(resources.getString("connection.text.untitled")); 98 } 99 100 101 108 public void connect() 109 { 110 logger.debug("In connect"); 111 this.refresh(); 112 } 113 114 115 126 public ObjectInstance createMBean(java.lang.String className, ObjectName name, ObjectName loaderName, java.lang.Object[] params, java.lang.String[] signature) 127 throws Exception 128 { 129 ObjectInstance result = null; 130 131 BeanContextServices context = (BeanContextServices) getBeanContext(); 132 133 if (context.hasService(ConnectionService.class)) 134 { 135 try 136 { 137 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 138 139 if (loaderName == null) 140 { 141 if ((params == null) || (signature == null)) 142 { 143 logger.debug("Creating MBean with <" + className + "," + name + ">"); 144 result = service.getMBeanServer().createMBean(className, name); 145 } 146 else 147 { 148 logger.debug("Creating MBean with <" + className + "," + name + "," + params + "," + signature + ">"); 149 result = service.getMBeanServer().createMBean(className, name, params, signature); 150 } 151 } 152 else 153 { 154 if ((params == null) || (signature == null)) 155 { 156 logger.debug("Creating MBean with <" + className + "," + name + "," + loaderName + ">"); 157 result = service.getMBeanServer().createMBean(className, name, loaderName); 158 } 159 else 160 { 161 logger.debug("Creating MBean with <" + className + "," + name + "," + loaderName + "," + params + "," + signature + ">"); 162 result = service.getMBeanServer().createMBean(className, name, loaderName, params, signature); 163 } 164 } 165 166 context.releaseService(this, this, ConnectionService.class); 167 } 168 catch (Exception e) 169 { 170 logger.error("Error during utilisation of service ConnectionService", e); 171 throw e; 172 } 173 } 174 175 return result; 176 } 177 178 179 187 public void disconnect() 188 { 189 logger.debug("In disconnect"); 190 BeanContextServices context = (BeanContextServices) getBeanContext(); 191 192 if (context.hasService(ConnectionService.class) && context.hasService(CacheService.class)) 193 { 194 try 195 { 196 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 197 CacheService cache = (CacheService) context.getService(this, this, CacheService.class, this, this); 198 199 Enumeration enum = cache.keys(CacheService.RESOURCE_TYPE); 201 while (enum.hasMoreElements()) 202 { 203 String name = (String) enum.nextElement(); 204 205 Resource resource = (Resource) cache.get(CacheService.RESOURCE_TYPE, name); 206 resource.getBeanContext().remove(resource); 207 } 208 209 enum = cache.keys(CacheService.DOMAIN_TYPE); 211 while (enum.hasMoreElements()) 212 { 213 String name = (String) enum.nextElement(); 214 Domain domain = (Domain) cache.get(CacheService.DOMAIN_TYPE, name); 215 216 if (domain.isEmpty()) 217 { 218 domain.getBeanContext().remove(domain); 219 } 220 } 221 222 if (service.isConnected()) 224 { 225 service.disconnect(); 226 } 227 228 this.setCount(0); 229 this.setDefaultDomain(""); 230 231 context.releaseService(this, this, CacheService.class); 232 context.releaseService(this, this, ConnectionService.class); 233 } 234 catch (Exception e) 235 { 236 logger.error("Error during utilisation of service ConnectionService or CacheService", e); 237 } 238 } 239 } 240 241 242 247 public String getBinding() 248 { 249 return this.profile.getProperty(ConnectionMetaData.CONTEXT); 250 } 251 252 253 258 public int getCount() 259 { 260 return this.count; 261 } 262 263 264 269 public String getCredentials() 270 { 271 return this.profile.getProperty(ConnectionMetaData.CREDENTIALS); 272 } 273 274 275 280 public String getDefaultDomain() 281 { 282 return this.defaultDomain; 283 } 284 285 286 291 public String getFactory() 292 { 293 return this.profile.getProperty(ConnectionMetaData.FACTORY); 294 } 295 296 public String getService() 297 { 298 return this.profile.getProperty(ConnectionMetaData.SERVICE); 299 } 300 301 306 public String getPackages() 307 { 308 return this.profile.getProperty(ConnectionMetaData.PACKAGES); 309 } 310 311 312 317 public String getPrincipal() 318 { 319 return this.profile.getProperty(ConnectionMetaData.PRINCIPAL); 320 } 321 322 323 328 public String getURL() 329 { 330 return this.profile.getProperty(ConnectionMetaData.URL); 331 } 332 333 334 339 public boolean isConnected() 340 { 341 BeanContextServices context = (BeanContextServices) getBeanContext(); 342 boolean result = false; 343 if (context.hasService(ConnectionService.class)) 344 { 345 try 346 { 347 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 348 result = service.isConnected(); 349 context.releaseService(this, this, ConnectionService.class); 350 } 351 catch (Exception e) 352 { 353 logger.error("Error during utilisation of service ConnectionService", e); 354 } 355 } 356 return result; 357 } 358 359 360 368 public Set query(ObjectName name, QueryExp exp) 369 throws Exception 370 { 371 Set result = null; 372 373 BeanContextServices context = (BeanContextServices) getBeanContext(); 374 375 if (context.hasService(ConnectionService.class)) 376 { 377 try 378 { 379 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 380 381 result = service.getMBeanServer().queryNames(name, exp); 382 383 context.releaseService(this, this, ConnectionService.class); 384 } 385 catch (Exception e) 386 { 387 logger.error("Error during utilisation of service ConnectionService", e); 388 throw e; 389 } 390 } 391 392 return result; 393 } 394 395 396 404 public void refresh() 405 { 406 logger.debug("In refresh"); 407 BeanContextServices context = (BeanContextServices) getBeanContext(); 408 409 if (context.hasService(ConnectionService.class) && context.hasService(CacheService.class)) 410 { 411 try 412 { 413 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 414 CacheService cache = (CacheService) context.getService(this, this, CacheService.class, this, this); 415 416 if (!service.isConnected()) 417 { 418 service.connect(this.profile); 419 } 420 421 this.setDefaultDomain(service.getMBeanServer().getDefaultDomain()); 422 423 Hashtable newContent = new Hashtable(); 424 Collection result = service.getMBeanServer().queryMBeans(null, null); 425 426 int counter = result.size(); 427 this.setCount(counter); 428 429 Iterator it = result.iterator(); 431 while (it.hasNext()) 432 { 433 ObjectInstance oi = (ObjectInstance) it.next(); 434 newContent.put(oi.getObjectName().getCanonicalName(), oi); 435 } 437 438 Enumeration enum = cache.keys(CacheService.RESOURCE_TYPE); 440 while (enum.hasMoreElements()) 441 { 442 String name = (String) enum.nextElement(); 443 Object o = newContent.get(name); 444 445 if (o == null) 446 { 447 Resource resource = (Resource) cache.get(CacheService.RESOURCE_TYPE, name); 448 resource.getBeanContext().remove(resource); 449 } 451 } 452 453 enum = cache.keys(CacheService.DOMAIN_TYPE); 455 while (enum.hasMoreElements()) 456 { 457 String name = (String) enum.nextElement(); 458 Domain domain = (Domain) cache.get(CacheService.DOMAIN_TYPE, name); 459 460 if (domain.isEmpty()) 461 { 462 domain.getBeanContext().remove(domain); 463 } 465 } 466 467 it = result.iterator(); 469 while (it.hasNext()) 470 { 471 ObjectInstance oi = (ObjectInstance) it.next(); 472 473 String domainName = oi.getObjectName().getDomain(); 474 Domain d = (Domain) cache.get(CacheService.DOMAIN_TYPE, domainName); 475 476 if (d == null) 477 { 478 d = new Domain(); 479 d.setName(domainName); 480 add(d); 481 } 483 484 String name = oi.getObjectName().getCanonicalName(); 485 Resource resource = (Resource) cache.get(CacheService.RESOURCE_TYPE, name); 486 487 if (resource == null) 488 { 489 resource = new Resource(); 490 resource.setReference(oi); 491 d.add(resource); 492 } 494 } 495 496 context.releaseService(this, this, CacheService.class); 497 context.releaseService(this, this, ConnectionService.class); 498 } 499 catch (Exception e) 500 { 501 logger.error("Error during utilisation of service ConnectionService or CacheService", e); 502 } 503 } 504 } 505 506 507 508 513 public void setBinding(String binding) 514 { 515 this.profile.setProperty(ConnectionMetaData.CONTEXT, binding); 516 } 517 518 519 524 public void setCredentials(String credentials) 525 { 526 this.profile.setProperty(ConnectionMetaData.CREDENTIALS, credentials); 527 } 528 529 530 535 public void setFactory(String factory) 536 { 537 this.profile.setProperty(ConnectionMetaData.FACTORY, factory); 538 } 539 540 public void setService(String service) 541 { 542 this.profile.setProperty(ConnectionMetaData.SERVICE, service); 543 } 544 545 546 551 public void setName(String name) 552 { 553 super.setName(name); 554 } 555 556 557 562 public void setPackages(String pkgs) 563 { 564 this.profile.setProperty(ConnectionMetaData.PACKAGES, pkgs); 565 } 566 567 568 573 public void setPrincipal(String principal) 574 { 575 this.profile.setProperty(ConnectionMetaData.PRINCIPAL, principal); 576 } 577 578 579 584 public void setProfile(Profile profile) 585 { 586 this.profile = profile; 587 } 588 589 590 595 public void setURL(String url) 596 { 597 this.profile.setProperty(ConnectionMetaData.URL, url); 598 } 599 600 601 606 protected void setCount(int count) 607 { 608 int oldCount = this.count; 609 this.count = count; 610 this.firePropertyChange("count", new Integer(oldCount), new Integer(count)); 611 } 612 613 614 619 protected void setDefaultDomain(String defaultDomain) 620 { 621 String oldDomain = this.defaultDomain; 622 this.defaultDomain = defaultDomain; 623 this.firePropertyChange("defaultDomain", oldDomain, defaultDomain); 624 } 625 } 626 | Popular Tags |