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 */
017package org.apache.camel.processor.saga;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Processor;
021import org.apache.camel.model.SagaCompletionMode;
022import org.apache.camel.model.SagaPropagation;
023import org.apache.camel.saga.CamelSagaService;
024import org.apache.camel.saga.CamelSagaStep;
025
026/**
027 * Builder of Saga processors.
028 */
029public class SagaProcessorBuilder {
030
031    private CamelContext camelContext;
032
033    private Processor childProcessor;
034
035    private CamelSagaService sagaService;
036
037    private CamelSagaStep step;
038
039    private SagaPropagation propagation;
040
041    private SagaCompletionMode completionMode;
042
043    public SagaProcessorBuilder() {
044    }
045
046    public SagaProcessorBuilder camelContext(CamelContext camelContext) {
047        this.camelContext = camelContext;
048        return this;
049    }
050
051    public SagaProcessorBuilder childProcessor(Processor childProcessor) {
052        this.childProcessor = childProcessor;
053        return this;
054    }
055
056    public SagaProcessorBuilder sagaService(CamelSagaService sagaService) {
057        this.sagaService = sagaService;
058        return this;
059    }
060
061    public SagaProcessorBuilder step(CamelSagaStep step) {
062        this.step = step;
063        return this;
064    }
065
066    public SagaProcessorBuilder propagation(SagaPropagation propagation) {
067        this.propagation = propagation;
068        return this;
069    }
070
071    public SagaProcessorBuilder completionMode(SagaCompletionMode completionMode) {
072        this.completionMode = completionMode;
073        return this;
074    }
075
076    public SagaProcessor build() {
077        if (propagation == null) {
078            throw new IllegalStateException("A propagation mode has not been set");
079        }
080
081        switch (propagation) {
082        case REQUIRED:
083            return new RequiredSagaProcessor(camelContext, childProcessor, sagaService, completionMode, step);
084        case REQUIRES_NEW:
085            return new RequiresNewSagaProcessor(camelContext, childProcessor, sagaService, completionMode, step);
086        case SUPPORTS:
087            return new SupportsSagaProcessor(camelContext, childProcessor, sagaService, completionMode, step);
088        case NOT_SUPPORTED:
089            return new NotSupportedSagaProcessor(camelContext, childProcessor, sagaService, completionMode, step);
090        case NEVER:
091            return new NeverSagaProcessor(camelContext, childProcessor, sagaService, completionMode, step);
092        case MANDATORY:
093            return new MandatorySagaProcessor(camelContext, childProcessor, sagaService, completionMode, step);
094        default:
095            throw new IllegalStateException("Unsupported propagation mode: " + propagation);
096        }
097    }
098
099}