001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.jbi.component;
018:
019: import java.io.File;
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022:
023: import javax.jbi.management.DeploymentException;
024: import javax.xml.namespace.QName;
025:
026: import org.apache.servicemix.common.AbstractDeployer;
027: import org.apache.servicemix.common.BaseComponent;
028: import org.apache.servicemix.common.Endpoint;
029: import org.apache.servicemix.common.ServiceUnit;
030: import org.bpmscript.BpmScriptException;
031: import org.bpmscript.FileUtils;
032: import org.bpmscript.IProcessInstanceManager;
033: import org.bpmscript.IProcessManager;
034:
035: public class BpmScriptDeployer extends AbstractDeployer {
036:
037: public BpmScriptDeployer(BaseComponent component) {
038: super (component);
039: }
040:
041: protected boolean validate(Endpoint endpoint)
042: throws DeploymentException {
043: if (endpoint instanceof ProcessEndpoint == false) {
044: throw new DeploymentException(
045: "Endpoint should be a ProcessEndpoint");
046: }
047: ((ProcessEndpoint) endpoint).validate();
048: return true;
049: }
050:
051: public ServiceUnit deploy(String serviceUnitName,
052: String serviceUnitRootPath) throws DeploymentException {
053:
054: BpmScriptLifeCycle lifeCycle = (BpmScriptLifeCycle) component
055: .getLifeCycle();
056: IProcessInstanceManager processInstanceManager = lifeCycle
057: .getProcessInstanceManager();
058: IProcessManager processManager = lifeCycle.getProcessManager();
059: logger.info("Deploying service unit " + serviceUnitName);
060:
061: String processId;
062: try {
063: processId = processManager.createProcess("serviceunit",
064: serviceUnitName, true);
065: processManager.addSourceToProcess(processId, "main",
066: FileUtils.getFileContents(serviceUnitRootPath,
067: "main.js"), true);
068: processManager.setProcessAsPrimary(processId);
069:
070: ServiceUnit serviceUnit = new ServiceUnit(component);
071: serviceUnit.setName(serviceUnitName);
072: serviceUnit.setRootPath(serviceUnitRootPath);
073:
074: QName service = new QName(lifeCycle.getDefaultNamespace(),
075: serviceUnitName);
076:
077: // TODO: create a service unit for each end point rather than bunging them
078: // all in the same service unit
079: if (component.getComponentContext().getEndpoint(service,
080: serviceUnitName) == null) {
081: ProcessEndpoint processEndpoint = new ProcessEndpoint();
082: processEndpoint.setProcessId(processId);
083: processEndpoint
084: .setProcessInstanceManager(processInstanceManager);
085: processEndpoint.setProcessExecutor(lifeCycle
086: .getProcessExecutor());
087: processEndpoint.setServiceUnit(serviceUnit);
088: processEndpoint.setService(service);
089: processEndpoint.setEndpoint(serviceUnitName);
090: serviceUnit.addEndpoint(processEndpoint);
091: } else {
092: logger
093: .info("endpoint already registered, updating service unit");
094: }
095:
096: logger.info("done");
097: return serviceUnit;
098: } catch (BpmScriptException e) {
099: throw new DeploymentException(e);
100: } catch (FileNotFoundException e) {
101: throw new DeploymentException(e);
102: } catch (IOException e) {
103: throw new DeploymentException(e);
104: }
105: }
106:
107: public boolean canDeploy(String serviceUnitName,
108: String serviceUnitRootPath) {
109: File main = new File(serviceUnitRootPath, getMainFile() + ".js");
110: if (logger.isDebugEnabled()) {
111: logger.debug("Looking for " + main + ": " + main.exists());
112: }
113: return main.exists();
114: }
115:
116: private String getMainFile() {
117: return "main";
118: }
119:
120: }
|