1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.system.ServiceMBean; 25 26 import javax.management.AttributeChangeNotification ; 27 import javax.management.Notification ; 28 import javax.management.NotificationListener ; 29 import java.security.InvalidParameterException ; 30 31 37 public class StateManagement implements NotificationListener 38 { 39 public static final int CREATED = 5; 42 public static final int DESTROYED = 6; 43 public static final int REGISTERED = 7; 44 public static final int UNREGISTERED = 8; 45 46 49 public static final String [] stateTypes = new String [] 50 { 51 NotificationConstants.STATE_STARTING, 52 NotificationConstants.STATE_RUNNING, 53 NotificationConstants.STATE_STOPPING, 54 NotificationConstants.STATE_STOPPED, 55 NotificationConstants.STATE_FAILED, 56 NotificationConstants.OBJECT_CREATED, 57 NotificationConstants.OBJECT_DELETED, 58 NotificationConstants.OBJECT_REGISTERED, 59 NotificationConstants.OBJECT_DELETED 60 }; 61 62 64 private long startTime = -1; 65 private int state = StateManageable.UNREGISTERED; 66 private J2EEManagedObject managedObject; 67 68 70 76 public static int convertJBossState(int theState) 77 { 78 int jsr77State = -1; 79 switch (theState) 80 { 81 case ServiceMBean.STARTING: 82 jsr77State = StateManageable.STARTING; 83 break; 84 case ServiceMBean.STARTED: 85 jsr77State = StateManageable.RUNNING; 86 break; 87 case ServiceMBean.STOPPING: 88 jsr77State = StateManageable.STOPPING; 89 break; 90 case ServiceMBean.STOPPED: 91 jsr77State = StateManageable.STOPPED; 92 break; 93 case ServiceMBean.FAILED: 94 jsr77State = StateManageable.FAILED; 95 break; 96 case ServiceMBean.CREATED: 97 jsr77State = CREATED; 98 break; 99 case ServiceMBean.DESTROYED: 100 jsr77State = DESTROYED; 101 break; 102 case ServiceMBean.REGISTERED: 103 jsr77State = REGISTERED; 104 break; 105 case ServiceMBean.UNREGISTERED: 106 jsr77State = UNREGISTERED; 107 break; 108 default: 109 jsr77State = -1; 110 break; 111 } 112 return jsr77State; 113 } 114 115 121 public static int convertJSR77State(int theState) 122 { 123 int jbossState = -1; 124 switch (theState) 125 { 126 case StateManageable.STARTING: 127 jbossState = ServiceMBean.STARTING; 128 break; 129 case StateManageable.RUNNING: 130 jbossState = ServiceMBean.STARTED; 131 break; 132 case StateManageable.STOPPING: 133 jbossState = ServiceMBean.STOPPING; 134 break; 135 case StateManageable.STOPPED: 136 jbossState = ServiceMBean.STOPPED; 137 break; 138 case StateManageable.FAILED: 139 jbossState = ServiceMBean.FAILED; 140 break; 141 case CREATED: 142 jbossState = ServiceMBean.CREATED; 143 break; 144 case DESTROYED: 145 jbossState = ServiceMBean.DESTROYED; 146 break; 147 case REGISTERED: 148 jbossState = ServiceMBean.REGISTERED; 149 break; 150 case UNREGISTERED: 151 jbossState = ServiceMBean.UNREGISTERED; 152 break; 153 } 154 return jbossState; 155 } 156 157 162 public StateManagement(J2EEManagedObject managedObject) 163 { 164 if (managedObject == null) 165 { 166 throw new InvalidParameterException ("managedObject must not be null"); 167 } 168 this.managedObject = managedObject; 169 this.startTime = System.currentTimeMillis(); 170 } 171 172 174 public long getStartTime() 175 { 176 return startTime; 177 } 178 179 public void setStartTime(long pTime) 180 { 181 startTime = pTime; 182 } 183 184 public int getState() 185 { 186 return state; 187 } 188 189 public String getStateString() 190 { 191 String stateName = stateTypes[state]; 192 return stateName; 193 } 194 195 203 public void setState(int newState) 204 { 205 if (0 <= newState && newState < stateTypes.length) 207 { 208 if (newState != state) 209 { 210 state = newState; 211 String type = stateTypes[state]; 213 managedObject.sendNotification(type, "State changed"); 214 } 215 } 216 } 217 218 220 227 public void handleNotification(Notification msg, Object handback) 228 { 229 if (msg instanceof AttributeChangeNotification ) 230 { 231 AttributeChangeNotification change = (AttributeChangeNotification ) msg; 232 String attrName = change.getAttributeName(); 233 Object newValue = change.getNewValue(); 234 if ("State".equals(attrName) && newValue != null && newValue instanceof Integer ) 235 { 236 int newState = ((Integer ) newValue).intValue(); 237 long eventTime = -1; 238 if (newState == ServiceMBean.STARTED) 239 { 240 eventTime = change.getTimeStamp(); 241 } 242 if (newState == ServiceMBean.STARTED) 243 setStartTime(eventTime); 244 int jsr77State = convertJBossState(newState); 245 setState(jsr77State); 246 } 247 } 248 } 249 250 252 public String toString() 253 { 254 return "StateManagement [ " + 255 "State: " + state + 256 ", Start Time: " + startTime + 257 " ]"; 258 } 259 } 260 | Popular Tags |