1 22 package org.jboss.management.j2ee; 23 24 import java.security.InvalidParameterException ; 25 import java.util.Hashtable ; 26 import java.util.Set ; 27 28 import javax.management.InstanceNotFoundException ; 29 import javax.management.JMException ; 30 import javax.management.MBeanRegistration ; 31 import javax.management.MBeanServer ; 32 import javax.management.MalformedObjectNameException ; 33 import javax.management.Notification ; 34 import javax.management.ObjectName ; 35 36 import org.jboss.logging.Logger; 37 import org.jboss.management.j2ee.statistics.StatisticsProvider; 38 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 39 import org.jboss.mx.util.ObjectNameConverter; 40 41 50 public abstract class J2EEManagedObject extends JBossNotificationBroadcasterSupport 51 implements J2EEManagedObjectMBean, MBeanRegistration 52 { 53 public static final String TYPE = "j2eeType"; 55 public static final String NAME = "name"; 56 57 private static Logger log = Logger.getLogger(J2EEManagedObject.class); 59 60 private ObjectName parentName = null; 61 private ObjectName name = null; 62 63 protected MBeanServer server; 64 65 67 75 protected static String getType(String pName) 76 { 77 String lType = null; 78 if (pName != null) 79 { 80 ObjectName oname = newObjectName(pName); 81 lType = (String ) oname.getKeyPropertyList().get(TYPE); 82 } 83 return lType == null ? "" : lType; 85 } 86 87 95 protected static String getType(ObjectName pName) 96 { 97 String lType = null; 98 if (pName != null) 99 { 100 lType = (String ) pName.getKeyPropertyList().get(TYPE); 101 } 102 return lType == null ? "" : lType; 104 } 105 106 111 protected static ObjectName newObjectName(String pName) 112 { 113 try 114 { 115 return new ObjectName (pName); 116 } 117 catch (MalformedObjectNameException e) 118 { 119 throw new IllegalArgumentException ("Invalid object name: " + pName); 120 } 121 } 122 123 protected static ObjectName removeObject(MBeanServer pServer, String pSearchCriteria) 124 throws JMException 125 { 126 ObjectName lSearch = ObjectNameConverter.convert(pSearchCriteria); 127 log.debug("removeObject(), search for: " + pSearchCriteria + ", search criteria: " + lSearch); 128 Set lNames = pServer.queryNames(lSearch, null); 129 if (!lNames.isEmpty()) 130 { 131 ObjectName lName = (ObjectName ) lNames.iterator().next(); 132 pServer.unregisterMBean(lName); 133 return lName; 134 } 135 return null; 136 } 137 138 protected static ObjectName removeObject(MBeanServer pServer, String pName, String pSearchCriteria) 139 throws JMException 140 { 141 String lEncryptedName = ObjectNameConverter.convertCharacters(pName, true); 142 ObjectName lSearch = new ObjectName (pSearchCriteria + "," + NAME + "=" + lEncryptedName); 143 log.debug("removeObject(), name: " + pName + ", encrypted name: " + lEncryptedName + ", search criteria: " + lSearch); 144 Set lNames = pServer.queryNames(lSearch, null); 145 if (!lNames.isEmpty()) 146 { 147 ObjectName lName = (ObjectName ) lNames.iterator().next(); 148 pServer.unregisterMBean(lName); 149 return lName; 150 } 151 return null; 152 } 153 154 156 164 public J2EEManagedObject(String domainName, String j2eeType, String resName) 165 throws MalformedObjectNameException 166 { 167 log = Logger.getLogger(getClass()); 168 if (domainName == null) 169 { 170 throw new InvalidParameterException ("Domain Name must be set"); 171 } 172 Hashtable lProperties = new Hashtable (); 173 lProperties.put(TYPE, j2eeType); 174 lProperties.put(NAME, resName); 175 name = ObjectNameConverter.convert(domainName, lProperties); 176 log.debug("ctor, name: " + name); 177 } 178 179 188 public J2EEManagedObject(String j2eeType, String resName, ObjectName jsr77ParentName) 189 throws MalformedObjectNameException , 190 InvalidParentException 191 { 192 log = Logger.getLogger(getClass()); 193 Hashtable lProperties = getParentKeys(jsr77ParentName); 194 lProperties.put(TYPE, j2eeType); 195 lProperties.put(NAME, resName); 196 name = ObjectNameConverter.convert(J2EEDomain.getDomainName(), lProperties); 197 setparent(jsr77ParentName.getCanonicalName()); 198 } 199 200 202 public Logger getLog() 203 { 204 return log; 205 } 206 207 public MBeanServer getServer() 208 { 209 return server; 210 } 211 212 public ObjectName getObjectName() 213 { 214 return name; 215 } 216 217 219 222 public String getobjectName() 223 { 224 return name.getCanonicalName(); 225 } 226 227 230 public String getparent() 231 { 232 String parent = null; 233 234 if (parentName != null) 235 parent = parentName.getCanonicalName(); 236 237 return parent; 238 } 239 240 243 public void setparent(String pParent) throws InvalidParentException 244 { 245 if (pParent == null) 246 { 247 throw new InvalidParameterException ("Parent must be set"); 248 } 249 parentName = newObjectName(pParent); 250 } 251 252 255 public void addChild(ObjectName pChild) 256 { 257 } 258 259 262 public void removeChild(ObjectName pChild) 263 { 264 } 265 266 269 public boolean isstateManageable() 270 { 271 return this instanceof StateManageable; 272 } 273 274 277 public boolean isstatisticsProvider() 278 { 279 return this instanceof StatisticsProvider; 280 } 281 282 285 public boolean iseventProvider() 286 { 287 return this instanceof EventProvider; 288 } 289 290 292 public ObjectName preRegister(MBeanServer server, ObjectName name) 293 { 294 this.server = server; 295 return name; 296 } 297 298 304 public final void postRegister(Boolean registrationDone) 305 { 306 try 309 { 310 log.debug("postRegister(), parent: " + parentName); 311 if (registrationDone.booleanValue()) 312 { 313 postCreation(); 315 if (parentName != null) 316 { 317 try 318 { 319 if (parentName.getKeyProperty("name").compareTo("null") != 0) 321 { 322 getServer().invoke(parentName, 323 "addChild", 324 new Object []{name}, 325 new String []{ObjectName .class.getName()}); 326 } 327 else 328 { 329 ObjectName j2eeServerName = J2EEDomain.getDomainServerName(server); 330 server.invoke(j2eeServerName, 331 "addChild", 332 new Object []{name}, 333 new String []{ObjectName .class.getName()}); 334 } 335 } 336 catch (JMException e) 337 { 338 log.debug("Failed to add child", e); 339 registrationDone = Boolean.FALSE; 340 } 341 } 342 } 343 } 344 catch (RuntimeException re) 345 { 346 log.debug("postRegister() caught this exception", re); 347 throw re; 348 } 349 } 350 351 357 public final void preDeregister() 358 throws Exception 359 { 360 log.debug("preDeregister(), parent: " + parentName); 361 if (parentName != null) 363 { 364 try 365 { 366 server.invoke(parentName, 368 "removeChild", 369 new Object []{name}, 370 new String []{ObjectName .class.getName()}); 371 } 372 catch (InstanceNotFoundException infe) 373 { 374 } 375 preDestruction(); 376 } 377 } 378 public void postDeregister() 379 { 380 server = null; 381 } 382 383 392 public void sendNotification(String type, String info) 393 { 394 Notification msg = new Notification (type, this.getObjectName(), 395 this.getNextNotificationSequenceNumber(), 396 System.currentTimeMillis(), 397 info); 398 super.sendNotification(msg); 399 } 400 401 403 public String toString() 404 { 405 return "J2EEManagedObject [ name: " + name + ", parent: " + parentName + " ];"; 406 } 407 408 410 412 protected void postCreation() 413 { 414 } 415 416 protected void preDestruction() 417 { 418 } 419 420 428 protected Hashtable getParentKeys(ObjectName pParent) 429 { 430 return new Hashtable (); 431 } 432 433 439 protected long getNextNotificationSequenceNumber() 440 { 441 return super.nextNotificationSequenceNumber(); 442 } 443 444 446 } 448 | Popular Tags |