1 22 package org.jboss.management.j2ee; 23 24 import java.util.Hashtable ; 25 26 import javax.management.JMException ; 27 import javax.management.MBeanServer ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.logging.Logger; 32 33 41 public class MBean extends J2EEManagedObject 42 implements MBeanMBean 43 { 44 45 private static Logger log = Logger.getLogger(MBean.class); 47 48 private ObjectName jbossServiceName; 49 private StateManagement mState; 50 private boolean monitorsStateChanges = false; 51 52 private boolean hasState; 53 54 56 64 public static ObjectName create(MBeanServer pServer, String pServiceModule, 65 ObjectName pTarget) 66 { 67 String pName = pTarget.toString(); 68 ObjectName mbeanName = null; 69 try 70 { 71 if (pServiceModule == null) 72 { 73 log.debug("Parent SAR Module not defined"); 74 return null; 75 } 76 77 MBean mbean = new MBean(pName, new ObjectName (pServiceModule), pTarget); 78 mbeanName = mbean.getObjectName(); 79 pServer.registerMBean(mbean, mbeanName); 80 } 81 catch (Exception e) 82 { 83 log.debug("Could not create JSR-77 MBean: " + pName, e); 84 } 85 return mbeanName; 86 } 87 88 public static void destroy(MBeanServer pServer, String pName) 89 { 90 try 91 { 92 if (pName.indexOf(J2EEManagedObject.TYPE + "=" + J2EETypeConstants.MBean) >= 0) 93 { 94 J2EEManagedObject.removeObject(pServer, 95 pName); 96 } 97 else 98 { 99 J2EEManagedObject.removeObject(pServer, 100 pName, 101 J2EEDomain.getDomainName() + ":" + 102 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.MBean + 103 "," + "*"); 104 } 105 } 106 catch (Exception e) 107 { 108 log.error("Could not destroy JSR-77 MBean: " + pName, e); 109 } 110 } 111 112 114 117 public MBean(String pName, ObjectName pServiceModule, ObjectName pTarget) 118 throws 119 MalformedObjectNameException , 120 InvalidParentException 121 { 122 super(J2EETypeConstants.MBean, pName, pServiceModule); 123 mState = new StateManagement(this); 124 jbossServiceName = pTarget; 125 } 126 127 134 public boolean isstateMonitored() 135 { 136 return monitorsStateChanges; 137 } 138 139 public boolean isstateManageable() 140 { 141 return hasState; 142 } 143 144 146 public void postCreation() 147 { 148 try 149 { 150 monitorsStateChanges = getServer().isInstanceOf(jbossServiceName, 152 "javax.management.NotificationBroadcaster"); 153 if (monitorsStateChanges) 154 { 155 getServer().addNotificationListener(jbossServiceName, mState, null, null); 156 } 157 } 158 catch (Exception jme) 159 { 160 log.debug("Failed to register as listener of: " + jbossServiceName, jme); 161 } 162 sendNotification(NotificationConstants.OBJECT_CREATED, "MBean created"); 163 164 try 166 { 167 Integer jbossState = (Integer )getServer().getAttribute(jbossServiceName, "State"); 168 int jsr77State = StateManagement.convertJBossState(jbossState.intValue()); 169 mState.setState(jsr77State); 170 } 171 catch (Exception e) 172 { 173 log.trace("Failed to initialize state from: '" + jbossServiceName + 174 "' : " + e.getClass().getName() + " : " + e.getMessage()); 175 hasState = false; 176 } 177 } 178 179 public void preDestruction() 180 { 181 sendNotification(NotificationConstants.OBJECT_DELETED, "MBean destroyed"); 182 try 184 { 185 if( monitorsStateChanges ) 186 getServer().removeNotificationListener(jbossServiceName, mState); 187 } 188 catch (JMException jme) 189 { 190 } 192 } 193 194 196 public String [] getEventTypes() 197 { 198 return StateManagement.stateTypes; 199 } 200 201 public String getEventType(int pIndex) 202 { 203 if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length) 204 { 205 return StateManagement.stateTypes[pIndex]; 206 } 207 else 208 { 209 return null; 210 } 211 } 212 213 215 public long getStartTime() 216 { 217 return mState.getStartTime(); 218 } 219 220 public int getState() 221 { 222 return mState.getState(); 223 } 224 225 public String getStateString() 226 { 227 return mState.getStateString(); 228 } 229 230 public void mejbStart() 231 { 232 try 233 { 234 getServer().invoke(jbossServiceName, 235 "start", 236 new Object []{}, 237 new String []{}); 238 } 239 catch (Exception e) 240 { 241 getLog().error("Start of MBean failed", e); 242 } 243 } 244 245 public void mejbStartRecursive() 246 { 247 mejbStart(); 249 } 250 251 public void mejbStop() 252 { 253 try 254 { 255 getServer().invoke(jbossServiceName, 256 "stop", 257 new Object []{}, 258 new String []{}); 259 } 260 catch (Exception e) 261 { 262 getLog().error("Stop of MBean failed", e); 263 } 264 } 265 266 268 public String toString() 269 { 270 return "MBean { " + super.toString() + " } []"; 271 } 272 273 275 277 280 protected Hashtable getParentKeys(ObjectName pParent) 281 { 282 Hashtable lReturn = new Hashtable (); 283 Hashtable lProperties = pParent.getKeyPropertyList(); 284 lReturn.put(J2EETypeConstants.ServiceModule, lProperties.get("name")); 285 lReturn.put(J2EETypeConstants.J2EEApplication, "null"); 287 lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer)); 290 291 return lReturn; 292 } 293 294 296 298 } 299 | Popular Tags |