FlowableEndpoint.java 6.53 KB
Newer Older
郑越's avatar
郑越 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/* 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.flowable.camel;

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

import org.apache.camel.CamelContext;
import org.apache.camel.Consumer;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;

/**
 * This class has been modified to be consistent with the changes to CamelBehavior and its implementations. The set of changes significantly increases the flexibility of our Camel integration, as you
 * can either choose one of three "out-of-the-box" modes, or you can choose to create your own. Please reference the comments for the "CamelBehavior" class for more information on the out-of-the-box
 * implementation class options.
 *
 * @author Ryan Johnston (@rjfsu), Tijs Rademakers, Arnold Schrijver
 */
public class FlowableEndpoint extends DefaultEndpoint {

    protected IdentityService identityService;

    protected RuntimeService runtimeService;

    protected RepositoryService repositoryService;

    protected ManagementService managementService;

    protected FlowableConsumer flowableConsumer;

    protected boolean copyVariablesToProperties;

    protected boolean copyVariablesToBodyAsMap;

    protected boolean copyCamelBodyToBody;

    protected String copyVariablesFromProperties;

    protected String copyVariablesFromHeader;

    protected boolean copyCamelBodyToBodyAsString;

    protected String processInitiatorHeaderName;

    protected Map<String, Object> returnVarMap = new HashMap<>();

    protected long timeout = 5000;

    protected int timeResolution = 100;

    public FlowableEndpoint(String uri, CamelContext camelContext) {
        super();
        setCamelContext(camelContext);
        setEndpointUri(uri);
    }

    public void process(Exchange ex) throws Exception {
        if (flowableConsumer == null) {
            throw new FlowableException("Consumer not defined for " + getEndpointUri());
        }
        flowableConsumer.getProcessor().process(ex);
    }

    @Override
    public Producer createProducer() throws Exception {
        FlowableProducer producer = new FlowableProducer(this, getTimeout(), getTimeResolution());
        producer.setRuntimeService(runtimeService);
        producer.setIdentityService(identityService);
        producer.setRepositoryService(repositoryService);
        producer.setManagementService(managementService);
        return producer;
    }

    @Override
    public Consumer createConsumer(Processor processor) throws Exception {
        return new FlowableConsumer(this, processor);
    }

    protected void addConsumer(FlowableConsumer consumer) {
        if (flowableConsumer != null) {
            throw new FlowableException("Consumer already defined for " + getEndpointUri() + "!");
        }
        flowableConsumer = consumer;
    }

    protected void removeConsumer() {
        flowableConsumer = null;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setIdentityService(IdentityService identityService) {
        this.identityService = identityService;
    }

    public void setRuntimeService(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    public void setRepositoryService(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    public void setManagementService(ManagementService managementService) {
        this.managementService = managementService;
    }

    public boolean isCopyVariablesToProperties() {
        return copyVariablesToProperties;
    }

    public void setCopyVariablesToProperties(boolean copyVariablesToProperties) {
        this.copyVariablesToProperties = copyVariablesToProperties;
    }

    public boolean isCopyCamelBodyToBody() {
        return copyCamelBodyToBody;
    }

    public void setCopyCamelBodyToBody(boolean copyCamelBodyToBody) {
        this.copyCamelBodyToBody = copyCamelBodyToBody;
    }

    public boolean isCopyVariablesToBodyAsMap() {
        return copyVariablesToBodyAsMap;
    }

    public void setCopyVariablesToBodyAsMap(boolean copyVariablesToBodyAsMap) {
        this.copyVariablesToBodyAsMap = copyVariablesToBodyAsMap;
    }

    public String getCopyVariablesFromProperties() {
        return copyVariablesFromProperties;
    }

    public void setCopyVariablesFromProperties(String copyVariablesFromProperties) {
        this.copyVariablesFromProperties = copyVariablesFromProperties;
    }

    public String getCopyVariablesFromHeader() {
        return copyVariablesFromHeader;
    }

    public void setCopyVariablesFromHeader(String copyVariablesFromHeader) {
        this.copyVariablesFromHeader = copyVariablesFromHeader;
    }

    public boolean isCopyCamelBodyToBodyAsString() {
        return copyCamelBodyToBodyAsString;
    }

    public void setCopyCamelBodyToBodyAsString(boolean copyCamelBodyToBodyAsString) {
        this.copyCamelBodyToBodyAsString = copyCamelBodyToBodyAsString;
    }

    public boolean isSetProcessInitiator() {
        return StringUtils.isNotEmpty(getProcessInitiatorHeaderName());
    }

    public Map<String, Object> getReturnVarMap() {
        return returnVarMap;
    }

    public void setReturnVarMap(Map<String, Object> returnVarMap) {
        this.returnVarMap = returnVarMap;
    }

    public String getProcessInitiatorHeaderName() {
        return processInitiatorHeaderName;
    }

    public void setProcessInitiatorHeaderName(String processInitiatorHeaderName) {
        this.processInitiatorHeaderName = processInitiatorHeaderName;
    }

    @Override
    public boolean isLenientProperties() {
        return true;
    }

    public long getTimeout() {
        return timeout;
    }

    public int getTimeResolution() {
        return timeResolution;
    }

}