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.util.Arrays;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import javax.jbi.JBIException;
024: import javax.xml.namespace.QName;
025:
026: import org.apache.servicemix.common.BaseComponent;
027: import org.apache.servicemix.common.BaseLifeCycle;
028: import org.apache.servicemix.common.Endpoint;
029: import org.apache.servicemix.common.xbean.XBeanServiceUnit;
030: import org.bpmscript.IProcess;
031: import org.bpmscript.IProcessExecutor;
032: import org.bpmscript.IProcessInstanceManager;
033: import org.bpmscript.IProcessManager;
034: import org.bpmscript.InMemoryProcessExecutor;
035: import org.bpmscript.InMemoryProcessManager;
036: import org.bpmscript.XStreamMarshaler;
037: import org.bpmscript.jbi.PojoServiceEndpoint;
038: import org.bpmscript.remote.RemoteProcessManager;
039:
040: /**
041: * Fast in memory lifecycle. Constructs only in memory versions of the
042: * process managers etc.
043: */
044: public class InMemoryBpmScriptLifeCycle extends BaseLifeCycle {
045:
046: protected IProcessManager processManager;
047: protected IProcessInstanceManager processInstanceManager;
048: protected IProcessExecutor processExecutor;
049: protected String defaultNamespace = "http://bpmscript.org/jbi";
050: protected List<String> libraries = new LinkedList<String>(
051: Arrays
052: .asList(new String(
053: "classpath:/org/bpmscript/jbi/component/bpmscriptlibrary.js")));
054: protected Endpoint alarmEndpoint = null;
055: protected CorellationEndpoint corellationEndpoint = null;
056: protected PojoServiceEndpoint remoteProcessManagerEndpoint = null;
057: protected XBeanServiceUnit defaultServiceUnit = null;
058: protected XBeanServiceUnit processServiceUnit = null;
059:
060: public InMemoryBpmScriptLifeCycle(BaseComponent component) {
061: super (component);
062: }
063:
064: @Override
065: protected void doInit() throws Exception {
066: super .doInit();
067: // TODO: consider changing this or throwing an exception
068: if (processManager == null && processInstanceManager == null) {
069: InMemoryProcessManager defaultProcessManager = new InMemoryProcessManager();
070: processManager = defaultProcessManager;
071: processInstanceManager = defaultProcessManager;
072: }
073: if (processExecutor == null) {
074: InMemoryProcessExecutor defaultProcessExecutor = new InMemoryProcessExecutor();
075: defaultProcessExecutor.setLibraries(libraries);
076: defaultProcessExecutor.setProcessManager(processManager);
077: processExecutor = defaultProcessExecutor;
078: }
079: if (alarmEndpoint == null) {
080: alarmEndpoint = new AlarmEndpoint();
081: alarmEndpoint.setService(new QName(defaultNamespace,
082: "alarm"));
083: alarmEndpoint.setEndpoint("alarm");
084: }
085: if (remoteProcessManagerEndpoint == null) {
086: remoteProcessManagerEndpoint = new PojoServiceEndpoint();
087: remoteProcessManagerEndpoint.setService(new QName(
088: defaultNamespace, "remoteProcessManager"));
089: remoteProcessManagerEndpoint.setEndpoint("processManager");
090: remoteProcessManagerEndpoint
091: .setMarshaler(new XStreamMarshaler());
092: RemoteProcessManager remoteProcessManager = new RemoteProcessManager();
093: remoteProcessManager.setProcessManager(processManager);
094: remoteProcessManagerEndpoint.setPojo(remoteProcessManager);
095: }
096: if (corellationEndpoint == null) {
097: corellationEndpoint = new CorellationEndpoint();
098: corellationEndpoint.setService(new QName(defaultNamespace,
099: "corellator"));
100: corellationEndpoint.setEndpoint("corellator");
101: corellationEndpoint
102: .setLibrary("classpath:/org/bpmscript/jbi/component/corellation.js");
103: }
104: defaultServiceUnit = new XBeanServiceUnit();
105: defaultServiceUnit.setComponent(component);
106:
107: alarmEndpoint.setServiceUnit(defaultServiceUnit);
108: remoteProcessManagerEndpoint.setServiceUnit(defaultServiceUnit);
109: corellationEndpoint.setServiceUnit(defaultServiceUnit);
110:
111: defaultServiceUnit.addEndpoint(alarmEndpoint);
112: defaultServiceUnit.addEndpoint(remoteProcessManagerEndpoint);
113: defaultServiceUnit.addEndpoint(corellationEndpoint);
114:
115: defaultServiceUnit.setName("default");
116:
117: processServiceUnit = new XBeanServiceUnit();
118: processServiceUnit.setName("process");
119: processServiceUnit.setComponent(component);
120:
121: // TODO: we'll have to rethink if we don't want to distribute
122: // processes to containers that don't expect them.
123: List<IProcess> primaryProcesses = processManager
124: .getPrimaryProcesses();
125: for (IProcess process : primaryProcesses) {
126: ProcessEndpoint processEndpoint = new ProcessEndpoint();
127: processEndpoint.setService(new QName(defaultNamespace,
128: process.getName()));
129: processEndpoint.setProcessId(process.getId());
130: processEndpoint
131: .setProcessInstanceManager(processInstanceManager);
132: processEndpoint.setProcessExecutor(processExecutor);
133: processEndpoint.setServiceUnit(processServiceUnit);
134: processEndpoint.setEndpoint(process.getName());
135: processServiceUnit.addEndpoint(processEndpoint);
136: }
137:
138: component.getRegistry().registerServiceUnit(defaultServiceUnit);
139: component.getRegistry().registerServiceUnit(processServiceUnit);
140: }
141:
142: @Override
143: public void start() throws JBIException {
144: super .start();
145: try {
146: defaultServiceUnit.start();
147: processServiceUnit.start();
148: } catch (Exception e) {
149: throw new JBIException(e);
150: }
151: }
152:
153: @Override
154: public void stop() throws JBIException {
155: super .stop();
156: try {
157: defaultServiceUnit.stop();
158: processServiceUnit.stop();
159: } catch (Exception e) {
160: throw new JBIException(e);
161: }
162: }
163:
164: @Override
165: public void shutDown() throws JBIException {
166: super .shutDown();
167: try {
168: defaultServiceUnit.shutDown();
169: processServiceUnit.shutDown();
170: } catch (Exception e) {
171: throw new JBIException(e);
172: }
173: }
174:
175: public IProcessManager getProcessManager() {
176: return processManager;
177: }
178:
179: public void setProcessManager(IProcessManager processManager) {
180: this .processManager = processManager;
181: }
182:
183: public String getDefaultNamespace() {
184: return defaultNamespace;
185: }
186:
187: public void setDefaultNamespace(String defaultNamespace) {
188: this .defaultNamespace = defaultNamespace;
189: }
190:
191: public IProcessExecutor getProcessExecutor() {
192: return processExecutor;
193: }
194:
195: public void setProcessExecutor(IProcessExecutor processExecutor) {
196: this .processExecutor = processExecutor;
197: }
198:
199: public List<String> getLibraries() {
200: return libraries;
201: }
202:
203: public void setLibraries(List<String> libraries) {
204: this .libraries = libraries;
205: }
206:
207: public XBeanServiceUnit getProcessServiceUnit() {
208: return processServiceUnit;
209: }
210:
211: public void setProcessServiceUnit(
212: XBeanServiceUnit processServiceUnit) {
213: this .processServiceUnit = processServiceUnit;
214: }
215:
216: public void setAlarmEndpoint(Endpoint alarmEndpoint) {
217: this.alarmEndpoint = alarmEndpoint;
218: }
219:
220: }
|