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

org.apache.camel.component.context.LocalContextComponent Maven / Gradle / Ivy

Go to download

Camel Context component to expose CamelContext objects as a black box Component for use in other routes

There is a newer version: 2.25.4
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.camel.component.context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.ResolveEndpointFailedException;
import org.apache.camel.impl.DefaultComponent;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A Camel Component which exposes a local {@link CamelContext} instance as a black box set of endpoints.
 */
public class LocalContextComponent extends DefaultComponent {
    private static final Logger LOG = LoggerFactory.getLogger(LocalContextComponent.class);

    private CamelContext localCamelContext;
    private List localProtocolSchemes = new ArrayList(Arrays.asList("direct", "seda", "mock"));

    public LocalContextComponent(CamelContext localCamelContext) {
        ObjectHelper.notNull(localCamelContext, "localCamelContext");
        this.localCamelContext = localCamelContext;
    }

    public List getLocalProtocolSchemes() {
        return localProtocolSchemes;
    }

    /**
     * Sets the list of protocols which are used to expose public endpoints by default
     */
    public void setLocalProtocolSchemes(List localProtocolSchemes) {
        this.localProtocolSchemes = localProtocolSchemes;
    }

    public CamelContext getLocalCamelContext() {
        return localCamelContext;
    }

    public void setLocalCamelContext(CamelContext localCamelContext) {
        this.localCamelContext = localCamelContext;
    }

    @Override
    protected Endpoint createEndpoint(String uri, String remaining, Map parameters)
        throws Exception {

        // first check if we are using a fully qualified name: [context:]contextId:endpointUri
        Map map = getLocalCamelContext().getEndpointMap();

        if (LOG.isTraceEnabled()) {
            LOG.trace("Trying to lookup {} in local map {}", remaining, map.keySet());
        }
        Endpoint endpoint = map.get(remaining);
        if (endpoint != null) {
            logUsingEndpoint(uri, endpoint);
            return endpoint;
        }

        // look to see if there is an endpoint of name 'remaining' using one of the local endpoints within
        // the black box CamelContext
        String[] separators = {":", "://"};
        for (String scheme : localProtocolSchemes) {
            for (String separator : separators) {
                String newUri = scheme + separator + remaining;
                endpoint = map.get(newUri);
                if (endpoint != null) {
                    logUsingEndpoint(uri, endpoint);
                    return endpoint;
                }
            }
        }
        throw new ResolveEndpointFailedException("Cannot find the endpoint with uri " + uri + " in the CamelContext " + getLocalCamelContext().getName());
        
    }

    protected void logUsingEndpoint(String uri, Endpoint endpoint) {
        LOG.debug("Mapping the URI: {} to local endpoint: {}", uri, endpoint);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy