1 17 package org.apache.servicemix.jbi.management; 18 19 import java.beans.PropertyChangeEvent ; 20 import java.beans.PropertyChangeListener ; 21 import javax.jbi.JBIException; 22 import javax.jbi.management.LifeCycleMBean; 23 import javax.management.JMException ; 24 import javax.management.MBeanAttributeInfo ; 25 import javax.management.MBeanOperationInfo ; 26 27 32 public abstract class BaseLifeCycle implements LifeCycleMBean, MBeanInfoProvider { 33 34 public static final String INITIALIZED = "Initialized"; 35 36 protected String currentState = LifeCycleMBean.UNKNOWN; 37 38 protected PropertyChangeListener listener; 39 40 41 45 public String getName() { 46 String name = getClass().getName(); 47 int index = name.lastIndexOf("."); 48 if (index >= 0 && (index+1) < name.length()) { 49 name = name.substring(index+1); 50 } 51 return name; 52 } 53 54 58 public String getType() { 59 String name = getClass().getName(); 60 int index = name.lastIndexOf("."); 61 if (index >= 0 && (index+1) < name.length()) { 62 name = name.substring(index+1); 63 } 64 return name; 65 } 66 67 public String getSubType() { 68 return null; 69 } 70 71 76 protected void init() throws JBIException{ 77 setCurrentState(INITIALIZED); 78 } 79 80 85 public void start() throws javax.jbi.JBIException { 86 setCurrentState(LifeCycleMBean.STARTED); 87 } 88 89 94 public void stop() throws javax.jbi.JBIException { 95 setCurrentState(LifeCycleMBean.STOPPED); 96 } 97 98 103 public void shutDown() throws javax.jbi.JBIException { 104 setCurrentState(LifeCycleMBean.SHUTDOWN); 105 } 106 107 113 public String getCurrentState() { 114 return currentState; 115 } 116 117 121 protected void setCurrentState(String newValue){ 122 String oldValue = currentState; 123 this.currentState = newValue; 124 firePropertyChanged("currentState",oldValue,newValue); 125 } 126 127 130 public boolean isStarted(){ 131 return currentState != null && currentState.equals(LifeCycleMBean.STARTED); 132 } 133 134 137 public boolean isStopped(){ 138 return currentState != null && currentState.equals(LifeCycleMBean.STOPPED); 139 } 140 141 144 public boolean isShutDown(){ 145 return currentState != null && currentState.equals(LifeCycleMBean.SHUTDOWN); 146 } 147 148 151 public boolean isInitialized(){ 152 return currentState != null && currentState.equals(INITIALIZED); 153 } 154 155 158 public boolean isUnknown(){ 159 return currentState == null || currentState.equals(LifeCycleMBean.UNKNOWN); 160 } 161 162 168 public MBeanAttributeInfo [] getAttributeInfos() throws JMException { 169 AttributeInfoHelper helper = new AttributeInfoHelper(); 170 helper.addAttribute(getObjectToManage(), "currentState", "Current State of Managed Item"); 171 helper.addAttribute(getObjectToManage(), "name", "name of the Item"); 172 helper.addAttribute(getObjectToManage(), "description", "description of the Item"); 173 return helper.getAttributeInfos(); 174 } 175 176 182 public MBeanOperationInfo [] getOperationInfos() throws JMException { 183 OperationInfoHelper helper = new OperationInfoHelper(); 184 helper.addOperation(getObjectToManage(), "start", "start the item"); 185 helper.addOperation(getObjectToManage(), "stop", "stop the item"); 186 helper.addOperation(getObjectToManage(), "shutDown", "shutdown the item"); 187 return helper.getOperationInfos(); 188 } 189 190 195 public Object getObjectToManage() { 196 return this; 197 } 198 199 203 public void setPropertyChangeListener(PropertyChangeListener l){ 204 this.listener = l; 205 } 206 207 protected void firePropertyChanged(String name,Object oldValue, Object newValue){ 208 PropertyChangeListener l = listener; 209 if (l != null){ 210 PropertyChangeEvent event = new PropertyChangeEvent (this,name,oldValue,newValue); 211 l.propertyChange(event); 212 } 213 } 214 } | Popular Tags |