All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.activiti.camel.CamelBehaviour Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.activiti.camel;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.impl.bpmn.behavior.BpmnActivityBehavior;
import org.activiti.engine.impl.pvm.PvmProcessDefinition;
import org.activiti.engine.impl.pvm.delegate.ActivityBehavior;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultExchange;

@Deprecated
public class CamelBehaviour extends BpmnActivityBehavior implements ActivityBehavior {

  private static final long serialVersionUID = 1L;
  
  private Collection contextProviders;

  public CamelBehaviour(Collection camelContext) {
    this.contextProviders = camelContext;
  }

  public void execute(ActivityExecution execution) throws Exception {
    ActivitiEndpoint ae = createEndpoint(execution);
    Exchange ex = createExchange(execution, ae);
    ae.process(ex);
    execution.setVariables(ExchangeUtils.prepareVariables(ex, ae));
    performDefaultOutgoingBehavior(execution);
  }

  private ActivitiEndpoint createEndpoint(ActivityExecution execution) {
    String uri = "activiti://" + getProcessName(execution) + ":" + execution.getActivity().getId();
    return getEndpoint(getContext(execution), uri);
  }

  private ActivitiEndpoint getEndpoint(CamelContext ctx, String key) {
    for (Endpoint e : ctx.getEndpoints()) {
      if (e.getEndpointKey().equals(key) && (e instanceof ActivitiEndpoint)) {
        return (ActivitiEndpoint) e;
      }
    }
    throw new RuntimeException("Activiti endpoint not defined for " + key);    
  }

  private CamelContext getContext(ActivityExecution execution) {
    String processName = getProcessName(execution);
    String names = "";
    for (ContextProvider provider : contextProviders) {
      CamelContext ctx = provider.getContext(processName);
      if (ctx != null) {
        return ctx;
      }
    }
    throw new RuntimeException("Could not find camel context for " + processName + " names are " + names);
  }


  private Exchange createExchange(ActivityExecution activityExecution, ActivitiEndpoint endpoint) {
    Exchange ex = new DefaultExchange(getContext(activityExecution));
    Map variables = activityExecution.getVariables();
    if (endpoint.isCopyVariablesToProperties()) {
      for (Map.Entry var : variables.entrySet()) {
        ex.setProperty(var.getKey(), var.getValue());
      }
    }
    if (endpoint.isCopyVariablesToBodyAsMap()) {
      ex.getIn().setBody(new HashMap(variables));
    }
    return ex;
  }

  private String getProcessName(ActivityExecution execution) {
    PvmProcessDefinition processDefinition = execution.getActivity().getProcessDefinition();
    return processDefinition.getKey();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy