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: 640438 $
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 Producer createProducer() throws Exception {
048            return new DirectProducer<E>(this);
049        }
050    
051        public Consumer<E> createConsumer(Processor processor) throws Exception {
052            return new DefaultConsumer<E>(this, processor) {
053                @Override
054                public void start() throws Exception {
055                    if (!allowMultipleConsumers && !consumers.isEmpty()) {
056                        throw new IllegalStateException("Endpoint " + getEndpointUri() + " only allows 1 active consumer but you attempted to start a 2nd consumer.");
057                    }
058    
059                    consumers.add(this);
060                    super.start();
061                }
062    
063                @Override
064                public void stop() throws Exception {
065                    super.stop();
066                    consumers.remove(this);
067                }
068            };
069        }
070    
071        public boolean isAllowMultipleConsumers() {
072            return allowMultipleConsumers;
073        }
074    
075        public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) {
076            this.allowMultipleConsumers = allowMutlipleConsumers;
077        }
078    
079        public boolean isSingleton() {
080            return true;
081        }
082    
083        public List<DefaultConsumer<E>> getConsumers() {
084            return consumers;
085        }
086    }