1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.beans.PropertyChangeEvent ; 20 import java.beans.PropertyChangeListener ; 21 import java.io.File ; 22 23 import javax.jbi.component.ServiceUnitManager; 24 import javax.jbi.management.DeploymentException; 25 import javax.management.JMException ; 26 import javax.management.MBeanAttributeInfo ; 27 import javax.management.MBeanOperationInfo ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.servicemix.jbi.deployment.Descriptor; 32 import org.apache.servicemix.jbi.deployment.DescriptorFactory; 33 import org.apache.servicemix.jbi.deployment.ServiceUnit; 34 import org.apache.servicemix.jbi.deployment.Services; 35 import org.apache.servicemix.jbi.event.ServiceUnitEvent; 36 import org.apache.servicemix.jbi.event.ServiceUnitListener; 37 import org.apache.servicemix.jbi.management.AttributeInfoHelper; 38 import org.apache.servicemix.jbi.management.MBeanInfoProvider; 39 import org.apache.servicemix.jbi.management.OperationInfoHelper; 40 41 public class ServiceUnitLifeCycle implements ServiceUnitMBean, MBeanInfoProvider { 42 43 private static final Log log = LogFactory.getLog(ServiceUnitLifeCycle.class); 44 45 private ServiceUnit serviceUnit; 46 47 private String currentState = SHUTDOWN; 48 49 private String serviceAssembly; 50 51 private Registry registry; 52 53 private PropertyChangeListener listener; 54 55 private Services services; 56 57 private File rootDir; 58 59 public ServiceUnitLifeCycle(ServiceUnit serviceUnit, 60 String serviceAssembly, 61 Registry registry, 62 File rootDir) { 63 this.serviceUnit = serviceUnit; 64 this.serviceAssembly = serviceAssembly; 65 this.registry = registry; 66 this.rootDir = rootDir; 67 Descriptor d = DescriptorFactory.buildDescriptor(getServiceUnitRootPath()); 68 if (d != null) { 69 services = d.getServices(); 70 } 71 } 72 73 77 public void init() throws DeploymentException { 78 log.info("Initializing service unit: " + getName()); 79 checkComponentStarted("init"); 80 ServiceUnitManager sum = getServiceUnitManager(); 81 File path = getServiceUnitRootPath(); 82 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 83 try { 84 Thread.currentThread().setContextClassLoader(getComponentClassLoader()); 85 sum.init(getName(), path.getAbsolutePath()); 86 } finally { 87 Thread.currentThread().setContextClassLoader(cl); 88 } 89 currentState = STOPPED; 90 } 91 92 96 public void start() throws DeploymentException { 97 log.info("Starting service unit: " + getName()); 98 checkComponentStarted("start"); 99 ServiceUnitManager sum = getServiceUnitManager(); 100 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 101 try { 102 Thread.currentThread().setContextClassLoader(getComponentClassLoader()); 103 sum.start(getName()); 104 } finally { 105 Thread.currentThread().setContextClassLoader(cl); 106 } 107 currentState = STARTED; 108 } 109 110 114 public void stop() throws DeploymentException { 115 log.info("Stopping service unit: " + getName()); 116 checkComponentStarted("stop"); 117 ServiceUnitManager sum = getServiceUnitManager(); 118 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 119 try { 120 Thread.currentThread().setContextClassLoader(getComponentClassLoader()); 121 sum.stop(getName()); 122 } finally { 123 Thread.currentThread().setContextClassLoader(cl); 124 } 125 currentState = STOPPED; 126 } 127 128 133 public void shutDown() throws DeploymentException { 134 log.info("Shutting down service unit: " + getName()); 135 checkComponentStartedOrStopped("shutDown"); 136 ServiceUnitManager sum = getServiceUnitManager(); 137 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 138 try { 139 Thread.currentThread().setContextClassLoader(getComponentClassLoader()); 140 sum.shutDown(getName()); 141 } finally { 142 Thread.currentThread().setContextClassLoader(cl); 143 } 144 currentState = SHUTDOWN; 145 } 146 147 150 public String getCurrentState() { 151 return currentState; 152 } 153 154 public boolean isShutDown() { 155 return currentState.equals(SHUTDOWN); 156 } 157 158 public boolean isStopped() { 159 return currentState.equals(STOPPED); 160 } 161 162 public boolean isStarted() { 163 return currentState.equals(STARTED); 164 } 165 166 169 public String getName() { 170 return serviceUnit.getIdentification().getName(); 171 } 172 173 176 public String getDescription() { 177 return serviceUnit.getIdentification().getDescription(); 178 } 179 180 public String getComponentName() { 181 return serviceUnit.getTarget().getComponentName(); 182 } 183 184 public String getServiceAssembly() { 185 return serviceAssembly; 186 } 187 188 public String getDescriptor() { 189 File suDir = getServiceUnitRootPath(); 190 return DescriptorFactory.getDescriptorAsText(suDir); 191 } 192 193 public Services getServices() { 194 return services; 195 } 196 197 protected void checkComponentStarted(String task) throws DeploymentException { 198 String componentName = getComponentName(); 199 String suName = getName(); 200 ComponentMBeanImpl lcc = registry.getComponent(componentName); 201 if (lcc == null) { 202 throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not installed"); 203 } 204 if (!lcc.isStarted()) { 205 throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not started"); 206 } 207 if (lcc.getServiceUnitManager() == null) { 208 throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " does not accept deployments"); 209 } 210 } 211 212 protected void checkComponentStartedOrStopped(String task) throws DeploymentException { 213 String componentName = getComponentName(); 214 String suName = getName(); 215 ComponentMBeanImpl lcc = registry.getComponent(componentName); 216 if (lcc == null) { 217 throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not installed"); 218 } 219 if (!lcc.isStarted() && !lcc.isStopped()) { 220 throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not started"); 221 } 222 if (lcc.getServiceUnitManager() == null) { 223 throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " does not accept deployments"); 224 } 225 } 226 227 protected File getServiceUnitRootPath() { 228 return rootDir; 229 } 230 231 protected ServiceUnitManager getServiceUnitManager() { 232 ComponentMBeanImpl lcc = registry.getComponent(getComponentName()); 233 return lcc.getServiceUnitManager(); 234 } 235 236 protected ClassLoader getComponentClassLoader() { 237 ComponentMBeanImpl lcc = registry.getComponent(getComponentName()); 238 return lcc.getComponent().getClass().getClassLoader(); 240 } 241 242 public MBeanAttributeInfo [] getAttributeInfos() throws JMException { 243 AttributeInfoHelper helper = new AttributeInfoHelper(); 244 helper.addAttribute(getObjectToManage(), "currentState", "current state of the service unit"); 245 helper.addAttribute(getObjectToManage(), "name", "name of the service unit"); 246 helper.addAttribute(getObjectToManage(), "componentName", "component name of the service unit"); 247 helper.addAttribute(getObjectToManage(), "serviceAssembly", "service assembly name of the service unit"); 248 helper.addAttribute(getObjectToManage(), "description", "description of the service unit"); 249 return helper.getAttributeInfos(); 250 } 251 252 public MBeanOperationInfo [] getOperationInfos() throws JMException { 253 OperationInfoHelper helper = new OperationInfoHelper(); 254 helper.addOperation(getObjectToManage(), "getDescriptor", "retrieve the jbi descriptor for this unit"); 255 return helper.getOperationInfos(); 256 } 257 258 public Object getObjectToManage() { 259 return this; 260 } 261 262 public String getType() { 263 return "ServiceUnit"; 264 } 265 266 public String getSubType() { 267 return getComponentName(); 268 } 269 270 public void setPropertyChangeListener(PropertyChangeListener listener) { 271 this.listener = listener; 272 } 273 274 protected void firePropertyChanged(String name,Object oldValue, Object newValue){ 275 PropertyChangeListener l = listener; 276 if (l != null){ 277 PropertyChangeEvent event = new PropertyChangeEvent (this,name,oldValue,newValue); 278 l.propertyChange(event); 279 } 280 } 281 282 public String getKey() { 283 return getComponentName() + "/" + getName(); 284 } 285 286 protected void fireEvent(int type) { 287 ServiceUnitEvent event = new ServiceUnitEvent(this, type); 288 ServiceUnitListener[] listeners = (ServiceUnitListener[]) registry.getContainer().getListeners(ServiceUnitListener.class); 289 for (int i = 0; i < listeners.length; i++) { 290 switch (type) { 291 case ServiceUnitEvent.UNIT_STARTED: 292 listeners[i].unitStarted(event); 293 break; 294 case ServiceUnitEvent.UNIT_STOPPED: 295 listeners[i].unitStopped(event); 296 break; 297 case ServiceUnitEvent.UNIT_SHUTDOWN: 298 listeners[i].unitShutDown(event); 299 break; 300 } 301 } 302 } 303 304 } 305 | Popular Tags |