001: package org.bpmscript.jbi.component;
002:
003: import java.io.FileNotFoundException;
004: import java.io.IOException;
005:
006: import javax.jbi.JBIException;
007: import javax.jbi.component.ServiceUnitManager;
008: import javax.jbi.management.DeploymentException;
009: import javax.jbi.management.LifeCycleMBean;
010: import javax.xml.namespace.QName;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.servicemix.common.BaseComponent;
014: import org.apache.servicemix.common.ManagementSupport;
015: import org.apache.servicemix.common.ServiceUnit;
016: import org.bpmscript.BpmScriptException;
017: import org.bpmscript.FileUtils;
018: import org.bpmscript.IProcess;
019: import org.bpmscript.IProcessInstanceManager;
020: import org.bpmscript.IProcessManager;
021:
022: public class BpmScriptServiceUnitManager implements ServiceUnitManager {
023:
024: private Log logger;
025: private BaseComponent component;
026:
027: public BpmScriptServiceUnitManager(BaseComponent component) {
028: this .component = component;
029: this .logger = component.getLogger();
030: }
031:
032: /* (non-Javadoc)
033: * @see javax.jbi.component.ServiceUnitManager#deploy(java.lang.String, java.lang.String)
034: */
035: public synchronized String deploy(String serviceUnitName,
036: String serviceUnitRootPath) throws DeploymentException {
037: try {
038: if (logger.isDebugEnabled()) {
039: logger.debug("Deploying service unit");
040: }
041:
042: BpmScriptLifeCycle lifeCycle = (BpmScriptLifeCycle) component
043: .getLifeCycle();
044: IProcessManager processManager = lifeCycle
045: .getProcessManager();
046: logger.info("Deploying service unit " + serviceUnitName);
047:
048: String processId;
049: try {
050: processId = processManager.createProcess("serviceunit",
051: serviceUnitName, true);
052: processManager.addSourceToProcess(processId, "main",
053: FileUtils.getFileContents(serviceUnitRootPath,
054: "main.js"), true);
055: processManager.setProcessAsPrimary(processId);
056:
057: logger.info("done");
058:
059: } catch (BpmScriptException e) {
060: throw new DeploymentException(e);
061: } catch (FileNotFoundException e) {
062: throw new DeploymentException(e);
063: } catch (IOException e) {
064: throw new DeploymentException(e);
065: }
066:
067: return createSuccessMessage("deploy");
068: } catch (DeploymentException e) {
069: throw e;
070: } catch (Exception e) {
071: throw failure("deploy", "Unable to deploy service unit", e);
072: }
073: }
074:
075: /* (non-Javadoc)
076: * @see javax.jbi.component.ServiceUnitManager#init(java.lang.String, java.lang.String)
077: */
078: public synchronized void init(String serviceUnitName,
079: String serviceUnitRootPath) throws DeploymentException {
080: try {
081: if (logger.isDebugEnabled()) {
082: logger.debug("Initializing service unit");
083: }
084: if (serviceUnitName == null
085: || serviceUnitName.length() == 0) {
086: throw new IllegalArgumentException(
087: "serviceUnitName should be non null and non empty");
088: }
089: if (getServiceUnit(serviceUnitName) == null) {
090: BpmScriptLifeCycle lifeCycle = (BpmScriptLifeCycle) component
091: .getLifeCycle();
092: IProcessInstanceManager processInstanceManager = lifeCycle
093: .getProcessInstanceManager();
094: IProcessManager processManager = lifeCycle
095: .getProcessManager();
096: QName service = new QName(lifeCycle
097: .getDefaultNamespace(), serviceUnitName);
098: IProcess process = processManager
099: .getPrimaryProcess(serviceUnitName);
100: ServiceUnit serviceUnit = new ServiceUnit(component);
101: serviceUnit.setName(serviceUnitName);
102: serviceUnit.setRootPath(serviceUnitRootPath);
103: ProcessEndpoint processEndpoint = new ProcessEndpoint();
104: processEndpoint.setProcessId(process.getId());
105: processEndpoint
106: .setProcessInstanceManager(processInstanceManager);
107: processEndpoint.setProcessExecutor(lifeCycle
108: .getProcessExecutor());
109: processEndpoint.setServiceUnit(serviceUnit);
110: processEndpoint.setService(service);
111: processEndpoint.setEndpoint(serviceUnitName);
112: serviceUnit.addEndpoint(processEndpoint);
113: component.getRegistry()
114: .registerServiceUnit(serviceUnit);
115: }
116: if (logger.isDebugEnabled()) {
117: logger.debug("Service unit initialized");
118: }
119: } catch (Exception e) {
120: throw failure("init", "Unable to init service unit", e);
121: }
122: }
123:
124: /* (non-Javadoc)
125: * @see javax.jbi.component.ServiceUnitManager#start(java.lang.String)
126: */
127: public synchronized void start(String serviceUnitName)
128: throws DeploymentException {
129: try {
130: if (logger.isDebugEnabled()) {
131: logger.debug("Starting service unit");
132: }
133: if (serviceUnitName == null
134: || serviceUnitName.length() == 0) {
135: throw new IllegalArgumentException(
136: "serviceUnitName should be non null and non empty");
137: }
138: ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
139: if (su == null) {
140: throw failure("start", "Service Unit '"
141: + serviceUnitName + "' is not deployed", null);
142: }
143: if (!LifeCycleMBean.STOPPED.equals(su.getCurrentState())
144: && !LifeCycleMBean.SHUTDOWN.equals(su
145: .getCurrentState())) {
146: throw failure(
147: "start",
148: "ServiceUnit should be in a SHUTDOWN or STOPPED state",
149: null);
150: }
151: su.start();
152: if (logger.isDebugEnabled()) {
153: logger.debug("Service unit started");
154: }
155: } catch (DeploymentException e) {
156: throw e;
157: } catch (Exception e) {
158: throw failure("start", "Unable to start service unit", e);
159: }
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.jbi.component.ServiceUnitManager#stop(java.lang.String)
164: */
165: public synchronized void stop(String serviceUnitName)
166: throws DeploymentException {
167: try {
168: if (logger.isDebugEnabled()) {
169: logger.debug("Stopping service unit");
170: }
171: if (serviceUnitName == null
172: || serviceUnitName.length() == 0) {
173: throw new IllegalArgumentException(
174: "serviceUnitName should be non null and non empty");
175: }
176: ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
177: if (su == null) {
178: throw failure("stop", "Service Unit '"
179: + serviceUnitName + "' is not deployed", null);
180: }
181: if (!LifeCycleMBean.STARTED.equals(su.getCurrentState())) {
182: throw failure("stop",
183: "ServiceUnit should be in a SHUTDOWN state",
184: null);
185: }
186: su.stop();
187: if (logger.isDebugEnabled()) {
188: logger.debug("Service unit stopped");
189: }
190: } catch (DeploymentException e) {
191: throw e;
192: } catch (Exception e) {
193: throw failure("stop", "Unable to stop service unit", e);
194: }
195: }
196:
197: /* (non-Javadoc)
198: * @see javax.jbi.component.ServiceUnitManager#shutDown(java.lang.String)
199: */
200: public synchronized void shutDown(String serviceUnitName)
201: throws DeploymentException {
202: try {
203: if (logger.isDebugEnabled()) {
204: logger.debug("Shutting down service unit");
205: }
206: if (serviceUnitName == null
207: || serviceUnitName.length() == 0) {
208: throw new IllegalArgumentException(
209: "serviceUnitName should be non null and non empty");
210: }
211: ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
212: if (su == null) {
213: throw failure("shutDown", "Service Unit '"
214: + serviceUnitName + "' is not deployed", null);
215: }
216: su.shutDown();
217: if (logger.isDebugEnabled()) {
218: logger.debug("Service unit shut down");
219: }
220: } catch (DeploymentException e) {
221: throw e;
222: } catch (Exception e) {
223: throw failure("shutDown",
224: "Unable to shutdown service unit", e);
225: }
226: }
227:
228: /* (non-Javadoc)
229: * @see javax.jbi.component.ServiceUnitManager#undeploy(java.lang.String, java.lang.String)
230: */
231: public synchronized String undeploy(String serviceUnitName,
232: String serviceUnitRootPath) throws DeploymentException {
233: try {
234: if (logger.isDebugEnabled()) {
235: logger.debug("Undeploying service unit");
236: }
237: if (logger.isDebugEnabled()) {
238: logger.debug("Shutting down service unit");
239: }
240: if (serviceUnitName == null
241: || serviceUnitName.length() == 0) {
242: throw new IllegalArgumentException(
243: "serviceUnitName should be non null and non empty");
244: }
245: ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
246: if (su == null) {
247: throw failure("undeploy", "Service Unit '"
248: + serviceUnitName + "' is not deployed", null);
249: }
250: if (!LifeCycleMBean.SHUTDOWN.equals(su.getCurrentState())) {
251: throw failure("undeploy",
252: "ServiceUnit should be in a SHUTDOWN state",
253: null);
254: }
255: try {
256: su.shutDown();
257: } catch (JBIException e) {
258: throw new DeploymentException(
259: "Unable to shutDown service unit", e);
260: }
261: component.getRegistry().unregisterServiceUnit(su);
262: if (logger.isDebugEnabled()) {
263: logger.debug("Service unit undeployed");
264: }
265: return createSuccessMessage("undeploy");
266: } catch (DeploymentException e) {
267: throw e;
268: } catch (Exception e) {
269: throw failure("undeploy",
270: "Unable to undeploy service unit", e);
271: }
272: }
273:
274: protected DeploymentException failure(String task, String info,
275: Exception e) throws DeploymentException {
276: ManagementSupport.Message msg = new ManagementSupport.Message();
277: msg.setComponent(component.getComponentName());
278: msg.setTask(task);
279: msg.setResult("FAILED");
280: msg.setType("ERROR");
281: msg.setException(e);
282: msg.setMessage(info);
283: return new DeploymentException(ManagementSupport
284: .createComponentMessage(msg));
285: }
286:
287: protected String createSuccessMessage(String task) {
288: ManagementSupport.Message msg = new ManagementSupport.Message();
289: msg.setComponent(component.getComponentName());
290: msg.setTask(task);
291: msg.setResult("SUCCESS");
292: return ManagementSupport.createComponentMessage(msg);
293: }
294:
295: protected ServiceUnit getServiceUnit(String name) {
296: return component.getRegistry().getServiceUnit(name);
297: }
298:
299: }
|