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.direct;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.camel.Component;
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Producer;
026    import org.apache.camel.impl.DefaultEndpoint;
027    
028    /**
029     * Represents a direct endpoint that synchronously invokes the consumer of the
030     * endpoint when a producer sends a message to it.
031     *
032     * @version 
033     */
034    public class DirectEndpoint extends DefaultEndpoint {
035    
036        private volatile Map<String, DirectConsumer> consumers;
037    
038        public DirectEndpoint() {
039            this.consumers = new HashMap<String, DirectConsumer>();
040        }
041    
042        public DirectEndpoint(String endpointUri, Component component) {
043            this(endpointUri, component, new HashMap<String, DirectConsumer>());
044        }
045    
046        public DirectEndpoint(String uri, Component component, Map<String, DirectConsumer> consumers) {
047            super(uri, component);
048            this.consumers = consumers;
049        }
050    
051        public Producer createProducer() throws Exception {
052            return new DirectProducer(this);
053        }
054    
055        public Consumer createConsumer(Processor processor) throws Exception {
056            return new DirectConsumer(this, processor);
057        }
058    
059        public boolean isSingleton() {
060            return true;
061        }
062    
063        public void addConsumer(DirectConsumer consumer) {
064            String key = consumer.getEndpoint().getEndpointKey();
065            consumers.put(key, consumer);
066        }
067    
068        public void removeConsumer(DirectConsumer consumer) {
069            String key = consumer.getEndpoint().getEndpointKey();
070            consumers.remove(key);
071        }
072    
073        public boolean hasConsumer(DirectConsumer consumer) {
074            String key = consumer.getEndpoint().getEndpointKey();
075            return consumers.containsKey(key);
076        }
077    
078        public DirectConsumer getConsumer() {
079            String key = getEndpointKey();
080            return consumers.get(key);
081        }
082    
083    }