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

org.citrusframework.camel.actions.StartCamelContextAction Maven / Gradle / Ivy

There is a newer version: 4.4.0
Show newest version
/*
 * Copyright the original author or authors.
 *
 * 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.citrusframework.camel.actions;

import org.apache.camel.CamelContext;
import org.citrusframework.camel.CamelSettings;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StartCamelContextAction extends AbstractCamelAction {

    private final String contextName;

    /** Logger */
    private static final Logger logger = LoggerFactory.getLogger(StartCamelContextAction.class);

    /**
     * Default constructor.
     */
    public StartCamelContextAction(Builder builder) {
        super("start-context", builder);

        this.contextName = builder.contextName;
    }

    @Override
    public void doExecute(TestContext context) {
        CamelContext camelContext;
        if (referenceResolver != null && referenceResolver.isResolvable(contextName, CamelContext.class)) {
            camelContext = referenceResolver.resolve(contextName, CamelContext.class);
        } else if (context.getReferenceResolver().isResolvable(contextName, CamelContext.class)) {
            camelContext = context.getReferenceResolver().resolve(contextName, CamelContext.class);
        } else {
            throw new CitrusRuntimeException("Unable to resolve Camel context '%s'".formatted(contextName));
        }

        try {
            camelContext.start();
        } catch (Exception e) {
            throw new IllegalStateException("Failed to start Camel context '%s'".formatted(contextName), e);
        }

        logger.info("Started Camel context '%s'".formatted(contextName));
    }

    /**
     * Action builder.
     */
    public static final class Builder extends AbstractCamelAction.Builder {

        private String contextName = CamelSettings.getContextName();

        public Builder contextName(String name) {
            this.contextName = name;
            return this;
        }

        @Override
        public StartCamelContextAction doBuild() {
            return new StartCamelContextAction(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy