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.List;
020    import java.util.concurrent.CopyOnWriteArrayList;
021    
022    import org.apache.camel.Consumer;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Producer;
026    import org.apache.camel.impl.DefaultConsumer;
027    import org.apache.camel.impl.DefaultEndpoint;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * Represents a direct endpoint that synchronously invokes the consumers of the
033     * endpoint when a producer sends a message to it.
034     *
035     * @version $Revision: 41278 $
036     */
037    public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
038        private static final Log LOG = LogFactory.getLog(DirectEndpoint.class);
039        boolean allowMultipleConsumers = true;
040        private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>();
041    
042    
043        public DirectEndpoint(String uri, DirectComponent<E> component) {
044            super(uri, component);
045        }
046    
047        public DirectEndpoint(String endpointUri) {
048            super(endpointUri);
049        }
050    
051        public Producer createProducer() throws Exception {
052            return new DirectProducer<E>(this);
053        }
054    
055        public Consumer<E> createConsumer(Processor processor) throws Exception {
056            return new DefaultConsumer<E>(this, processor) {
057                @Override
058                public void start() throws Exception {
059                    if (!allowMultipleConsumers && !consumers.isEmpty()) {
060                        throw new IllegalStateException("Endpoint " + getEndpointUri() + " only allows 1 active consumer but you attempted to start a 2nd consumer.");
061                    }
062    
063                    consumers.add(this);
064                    super.start();
065                }
066    
067                @Override
068                public void stop() throws Exception {
069                    super.stop();
070                    consumers.remove(this);
071                }
072            };
073        }
074    
075        public boolean isAllowMultipleConsumers() {
076            return allowMultipleConsumers;
077        }
078    
079        public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) {
080            this.allowMultipleConsumers = allowMutlipleConsumers;
081        }
082    
083        public boolean isSingleton() {
084            return true;
085        }
086    
087        public List<DefaultConsumer<E>> getConsumers() {
088            return consumers;
089        }
090    }