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     */
017    package org.apache.camel.component.context;
018    
019    import java.util.ArrayList;
020    import java.util.Arrays;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.Endpoint;
026    import org.apache.camel.impl.DefaultComponent;
027    import org.apache.camel.util.ObjectHelper;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    /**
032     * A Camel Component which exposes a local {@link CamelContext} instance as a black box set of endpoints.
033     */
034    public class LocalContextComponent extends DefaultComponent {
035        private static final transient Logger LOG = LoggerFactory.getLogger(LocalContextComponent.class);
036    
037        private CamelContext localCamelContext;
038        private List<String> localProtocolSchemes = new ArrayList<String>(Arrays.asList("direct", "seda", "mock"));
039    
040        public LocalContextComponent(CamelContext localCamelContext) {
041            ObjectHelper.notNull(localCamelContext, "localCamelContext");
042            this.localCamelContext = localCamelContext;
043        }
044    
045        public List<String> getLocalProtocolSchemes() {
046            return localProtocolSchemes;
047        }
048    
049        /**
050         * Sets the list of protocols which are used to expose public endpoints by default
051         */
052        public void setLocalProtocolSchemes(List<String> localProtocolSchemes) {
053            this.localProtocolSchemes = localProtocolSchemes;
054        }
055    
056        public CamelContext getLocalCamelContext() {
057            return localCamelContext;
058        }
059    
060        public void setLocalCamelContext(CamelContext localCamelContext) {
061            this.localCamelContext = localCamelContext;
062        }
063    
064        @Override
065        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters)
066            throws Exception {
067    
068            // lets first check if we are using a fully qualified name: [context:]contextId:endpointUri
069            Map<String, Endpoint> map = getLocalCamelContext().getEndpointMap();
070    
071            if (LOG.isTraceEnabled()) {
072                LOG.trace("Trying to lookup " + remaining + " in local map " + map.keySet());
073            }
074            Endpoint endpoint = map.get(remaining);
075            if (endpoint != null) {
076                logUsingEndpoint(uri, endpoint);
077                return endpoint;
078            }
079    
080            // lets look to see if there is an endpoint of name 'remaining' using one of the local endpoints within
081            // the black box CamelContext
082            String[] separators = {":", "://"};
083            for (String scheme : localProtocolSchemes) {
084                for (String separator : separators) {
085                    String newUri = scheme + separator + remaining;
086                    endpoint = map.get(newUri);
087                    if (endpoint != null) {
088                        logUsingEndpoint(uri, endpoint);
089                        return endpoint;
090                    }
091                }
092            }
093            return null;
094        }
095    
096        protected void logUsingEndpoint(String uri, Endpoint endpoint) {
097            if (LOG.isDebugEnabled()) {
098                LOG.debug("Mapping the URI: " + uri + " to local endpoint: " + endpoint);
099            }
100        }
101    
102    }