1 17 package org.apache.servicemix.common; 18 19 import org.apache.commons.logging.Log; 20 21 import javax.jbi.component.ServiceUnitManager; 22 import javax.jbi.management.DeploymentException; 23 import javax.jbi.management.LifeCycleMBean; 24 25 34 public class BaseServiceUnitManager implements ServiceUnitManager { 35 36 protected final transient Log logger; 37 38 protected BaseComponent component; 39 40 protected Deployer[] deployers; 41 42 protected boolean persistent; 43 44 public BaseServiceUnitManager(BaseComponent component, Deployer[] deployers) { 45 this(component, deployers, false); 46 } 47 48 public BaseServiceUnitManager(BaseComponent component, Deployer[] deployers, boolean persistent) { 49 this.component = component; 50 this.logger = component.logger; 51 this.deployers = deployers; 52 this.persistent = persistent; 53 } 54 55 58 public synchronized String deploy(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException { 59 try { 60 if (logger.isDebugEnabled()) { 61 logger.debug("Deploying service unit"); 62 } 63 if (serviceUnitName == null || serviceUnitName.length() == 0) { 64 throw new IllegalArgumentException ("serviceUnitName should be non null and non empty"); 65 } 66 if (getServiceUnit(serviceUnitName) != null) { 67 throw failure("deploy", "Service Unit '" + serviceUnitName + "' is already deployed", null); 68 } 69 ServiceUnit su = doDeploy(serviceUnitName, serviceUnitRootPath); 70 if (su == null) { 71 throw failure("deploy", "Unable to find suitable deployer for Service Unit '" + serviceUnitName + "'", null); 72 } 73 component.getRegistry().registerServiceUnit(su); 74 if (logger.isDebugEnabled()) { 75 logger.debug("Service unit deployed"); 76 } 77 return createSuccessMessage("deploy"); 78 } catch (DeploymentException e) { 79 throw e; 80 } catch (Exception e) { 81 throw failure("deploy", "Unable to deploy service unit", e); 82 } 83 } 84 85 protected ServiceUnit doDeploy(String serviceUnitName, String serviceUnitRootPath) throws Exception { 86 for (int i = 0; i < deployers.length; i++) { 87 if (deployers[i].canDeploy(serviceUnitName, serviceUnitRootPath)) { 88 return deployers[i].deploy(serviceUnitName, serviceUnitRootPath); 89 } 90 } 91 return null; 92 } 93 94 97 public synchronized void init(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException { 98 try { 99 if (logger.isDebugEnabled()) { 100 logger.debug("Initializing service unit"); 101 } 102 if (serviceUnitName == null || serviceUnitName.length() == 0) { 103 throw new IllegalArgumentException ("serviceUnitName should be non null and non empty"); 104 } 105 if (getServiceUnit(serviceUnitName) == null) { 106 if (!persistent) { 107 ServiceUnit su = doDeploy(serviceUnitName, serviceUnitRootPath); 108 if (su == null) { 109 throw failure("deploy", "Unable to find suitable deployer for Service Unit '" + serviceUnitName + "'", null); 110 } 111 component.getRegistry().registerServiceUnit(su); 112 } else { 113 throw failure("init", "Service Unit '" + serviceUnitName + "' is not deployed", null); 114 } 115 } 116 doInit(serviceUnitName, serviceUnitRootPath); 117 if (logger.isDebugEnabled()) { 118 logger.debug("Service unit initialized"); 119 } 120 } catch (DeploymentException e) { 121 throw e; 122 } catch (Exception e) { 123 throw failure("init", "Unable to init service unit", e); 124 } 125 } 126 127 protected void doInit(String serviceUnitName, String serviceUnitRootPath) throws Exception { 128 } 129 130 133 public synchronized void start(String serviceUnitName) throws DeploymentException { 134 try { 135 if (logger.isDebugEnabled()) { 136 logger.debug("Starting service unit"); 137 } 138 if (serviceUnitName == null || serviceUnitName.length() == 0) { 139 throw new IllegalArgumentException ("serviceUnitName should be non null and non empty"); 140 } 141 ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName); 142 if (su == null) { 143 throw failure("start", "Service Unit '" + serviceUnitName + "' is not deployed", null); 144 } 145 if (!LifeCycleMBean.STOPPED.equals(su.getCurrentState()) && 146 !LifeCycleMBean.SHUTDOWN.equals(su.getCurrentState())) { 147 throw failure("start", "ServiceUnit should be in a SHUTDOWN or STOPPED state", null); 148 } 149 su.start(); 150 if (logger.isDebugEnabled()) { 151 logger.debug("Service unit started"); 152 } 153 } catch (DeploymentException e) { 154 throw e; 155 } catch (Exception e) { 156 throw failure("start", "Unable to start service unit", e); 157 } 158 } 159 160 163 public synchronized void stop(String serviceUnitName) throws DeploymentException { 164 try { 165 if (logger.isDebugEnabled()) { 166 logger.debug("Stopping service unit"); 167 } 168 if (serviceUnitName == null || serviceUnitName.length() == 0) { 169 throw new IllegalArgumentException ("serviceUnitName should be non null and non empty"); 170 } 171 ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName); 172 if (su == null) { 173 throw failure("stop", "Service Unit '" + serviceUnitName + "' is not deployed", null); 174 } 175 if (!LifeCycleMBean.STARTED.equals(su.getCurrentState())) { 176 throw failure("stop", "ServiceUnit should be in a SHUTDOWN state", null); 177 } 178 su.stop(); 179 if (logger.isDebugEnabled()) { 180 logger.debug("Service unit stopped"); 181 } 182 } catch (DeploymentException e) { 183 throw e; 184 } catch (Exception e) { 185 throw failure("stop", "Unable to stop service unit", e); 186 } 187 } 188 189 192 public synchronized void shutDown(String serviceUnitName) throws DeploymentException { 193 try { 194 if (logger.isDebugEnabled()) { 195 logger.debug("Shutting down service unit"); 196 } 197 if (serviceUnitName == null || serviceUnitName.length() == 0) { 198 throw new IllegalArgumentException ("serviceUnitName should be non null and non empty"); 199 } 200 ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName); 201 if (su == null) { 202 throw failure("shutDown", "Service Unit '" + serviceUnitName + "' is not deployed", null); 203 } 204 su.shutDown(); 205 if (logger.isDebugEnabled()) { 206 logger.debug("Service unit shut down"); 207 } 208 } catch (DeploymentException e) { 209 throw e; 210 } catch (Exception e) { 211 throw failure("shutDown", "Unable to shutdown service unit", e); 212 } 213 } 214 215 218 public synchronized String undeploy(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException { 219 try { 220 if (logger.isDebugEnabled()) { 221 logger.debug("Undeploying service unit"); 222 } 223 if (logger.isDebugEnabled()) { 224 logger.debug("Shutting down service unit"); 225 } 226 if (serviceUnitName == null || serviceUnitName.length() == 0) { 227 throw new IllegalArgumentException ("serviceUnitName should be non null and non empty"); 228 } 229 ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName); 230 if (su == null) { 231 throw failure("undeploy", "Service Unit '" + serviceUnitName + "' is not deployed", null); 232 } 233 if (!LifeCycleMBean.SHUTDOWN.equals(su.getCurrentState())) { 234 throw failure("undeploy", "ServiceUnit should be in a SHUTDOWN state", null); 235 } 236 doUndeploy(su); 237 component.getRegistry().unregisterServiceUnit(su); 238 if (logger.isDebugEnabled()) { 239 logger.debug("Service unit undeployed"); 240 } 241 return createSuccessMessage("undeploy"); 242 } catch (DeploymentException e) { 243 throw e; 244 } catch (Exception e) { 245 throw failure("undeploy", "Unable to undeploy service unit", e); 246 } 247 } 248 249 protected void doUndeploy(ServiceUnit su) throws Exception { 250 for (int i = 0; i < deployers.length; i++) { 251 if (deployers[i].canDeploy(su.getName(), su.getRootPath())) { 252 deployers[i].undeploy(su); 253 return; 254 } 255 } 256 throw failure("undeploy", "Unable to find suitable deployer for Service Unit '" + su.getName() + "'", null); 257 } 258 259 protected DeploymentException failure(String task, String info, Exception e) throws DeploymentException { 260 ManagementSupport.Message msg = new ManagementSupport.Message(); 261 msg.setComponent(component.getComponentName()); 262 msg.setTask(task); 263 msg.setResult("FAILED"); 264 msg.setType("ERROR"); 265 msg.setException(e); 266 msg.setMessage(info); 267 return new DeploymentException(ManagementSupport.createComponentMessage(msg)); 268 } 269 270 protected String createSuccessMessage(String task) { 271 ManagementSupport.Message msg = new ManagementSupport.Message(); 272 msg.setComponent(component.getComponentName()); 273 msg.setTask(task); 274 msg.setResult("SUCCESS"); 275 return ManagementSupport.createComponentMessage(msg); 276 } 277 278 protected ServiceUnit getServiceUnit(String name) { 279 return component.getRegistry().getServiceUnit(name); 280 } 281 282 } 283 | Popular Tags |