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