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.ByteArrayOutputStream;
020:
021: import javax.xml.namespace.QName;
022:
023: import org.apache.servicemix.common.BaseComponent;
024: import org.apache.servicemix.common.BaseLifeCycle;
025: import org.apache.servicemix.common.Endpoint;
026: import org.apache.servicemix.common.EndpointSupport;
027: import org.apache.servicemix.jbi.util.FileUtil;
028: import org.bpmscript.IProcess;
029: import org.bpmscript.IProcessManager;
030: import org.bpmscript.IProcessSource;
031: import org.springframework.context.ResourceLoaderAware;
032: import org.springframework.core.io.Resource;
033: import org.springframework.core.io.ResourceLoader;
034:
035: /**
036: *
037: * @org.apache.xbean.XBean element="component"
038: * description="My component"
039: */
040: public class BpmScriptSpringComponent extends BaseComponent implements
041: ResourceLoaderAware {
042:
043: protected ProcessEndpoint[] endpoints;
044: protected ResourceLoader resourceLoader;
045:
046: /**
047: * @return Returns the endpoints.
048: */
049: public ProcessEndpoint[] getEndpoints() {
050: return endpoints;
051: }
052:
053: /**
054: * @param endpoints The endpoints to set.
055: */
056: public void setEndpoints(ProcessEndpoint[] endpoints) {
057: this .endpoints = endpoints;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.servicemix.common.BaseComponent#createLifeCycle()
062: */
063: protected BaseLifeCycle createLifeCycle() {
064: return new LifeCycle();
065: }
066:
067: public void setConfiguration(Object configuration) {
068: getBpmScriptLifeCycle().setConfiguration(configuration);
069: }
070:
071: public BpmScriptLifeCycle getBpmScriptLifeCycle() {
072: return (BpmScriptLifeCycle) getLifeCycle();
073: }
074:
075: public void setProcessManager(IProcessManager processManager) {
076: getBpmScriptLifeCycle().setProcessManager(processManager);
077: }
078:
079: /**
080: * @author gnodet
081: */
082: public class LifeCycle extends BpmScriptLifeCycle {
083:
084: public LifeCycle() {
085: super (BpmScriptSpringComponent.this );
086: }
087:
088: /* (non-Javadoc)
089: * @see org.servicemix.common.BaseLifeCycle#doInit()
090: */
091: protected void doInit() throws Exception {
092: super .doInit();
093: }
094:
095: /* (non-Javadoc)
096: * @see org.servicemix.common.BaseLifeCycle#doStart()
097: */
098: protected void doStart() throws Exception {
099: super .doStart();
100: if (endpoints != null) {
101: for (int i = 0; i < endpoints.length; i++) {
102: ProcessEndpoint processEndpoint = (ProcessEndpoint) endpoints[i];
103:
104: IProcess primaryProcess = processManager
105: .getPrimaryProcess(processEndpoint
106: .getEndpoint());
107:
108: Resource resource = resourceLoader
109: .getResource(processEndpoint
110: .getSourceResource());
111: ByteArrayOutputStream out = new ByteArrayOutputStream();
112: FileUtil.copyInputStream(resource.getInputStream(),
113: out);
114: String mainSource = out.toString();
115:
116: // don't keep writing new records but override any different ones
117: if (primaryProcess != null) {
118: IProcessSource primaryMainSource = processManager
119: .getMainSource(primaryProcess.getId());
120: if (primaryMainSource != null
121: && primaryMainSource.equals(mainSource)) {
122: continue;
123: }
124: }
125:
126: String processId = processManager.createProcess(
127: "spring", processEndpoint.getEndpoint(),
128: true);
129: processManager.addSourceToProcess(processId,
130: "main", mainSource, true);
131: processManager.setProcessAsPrimary(processId);
132: processEndpoint.setService(new QName(
133: defaultNamespace, processEndpoint
134: .getEndpoint()));
135:
136: Endpoint endpoint = component.getRegistry()
137: .getEndpoint(
138: EndpointSupport
139: .getKey(processEndpoint));
140: if (endpoint == null) {
141: processEndpoint.setProcessId(processId);
142: processEndpoint
143: .setProcessInstanceManager(processInstanceManager);
144: processEndpoint
145: .setProcessExecutor(processExecutor);
146: endpoints[i].setServiceUnit(processServiceUnit);
147: endpoints[i].validate();
148: processServiceUnit.addEndpoint(endpoints[i]);
149: component.getRegistry().registerEndpoint(
150: processEndpoint);
151: processEndpoint.activate();
152: processEndpoint.start();
153: }
154: }
155: }
156: }
157:
158: /* (non-Javadoc)
159: * @see org.servicemix.common.BaseLifeCycle#doStop()
160: */
161: protected void doStop() throws Exception {
162: super .doStop();
163: }
164:
165: /* (non-Javadoc)
166: * @see org.servicemix.common.BaseLifeCycle#doShutDown()
167: */
168: protected void doShutDown() throws Exception {
169: super .doShutDown();
170: }
171: }
172:
173: public void setResourceLoader(ResourceLoader resourceLoader) {
174: this.resourceLoader = resourceLoader;
175: }
176:
177: }
|