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.concurrent.CopyOnWriteArrayList;
020    
021    import org.apache.camel.Consumer;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.impl.DefaultConsumer;
026    import org.apache.camel.impl.DefaultEndpoint;
027    import org.apache.camel.impl.DefaultExchange;
028    import org.apache.camel.impl.DefaultProducer;
029    import org.apache.camel.util.ProducerCache;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * Represents a direct endpoint that synchronously invokes the consumers of the endpoint when a producer 
035     * sends a message to it.
036     *
037     * @org.apache.xbean.XBean
038     * @version $Revision: 519973 $
039     */
040    public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
041        private static final Log log = LogFactory.getLog(DirectEndpoint.class);
042    
043            private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>();
044            
045            boolean allowMultipleConsumers=true;    
046            
047        public DirectEndpoint(String uri, DirectComponent<E> component) {
048            super(uri, component);
049        }
050    
051        public Producer createProducer() throws Exception {
052            return new DefaultProducer(this) {
053                public void process(Exchange exchange) throws Exception {
054                    DirectEndpoint.this.process(exchange);
055                }
056            };      
057        }
058    
059        protected void process(Exchange exchange) throws Exception {
060            if (consumers.isEmpty()) {
061                    log.warn("No consumers available on " + this + " for " + exchange);
062            }
063            else {
064                    for (DefaultConsumer<E> consumer : consumers) {
065                                    consumer.getProcessor().process(exchange);
066                            }
067            }
068        }
069    
070            public Consumer<E> createConsumer(Processor processor) throws Exception {
071                    return new DefaultConsumer<E>(this, processor) {
072                            @Override
073                            public void start() throws Exception {
074                                    if( !allowMultipleConsumers && !consumers.isEmpty() )
075                                            throw new IllegalStateException("Endpoint "+getEndpointUri()+" only allows 1 active consumer but you attempted to start a 2nd consumer.");
076                                    
077                                    consumers.add(this);
078                                    super.start();
079                            }
080                            
081                            @Override
082                            public void stop() throws Exception {
083                                    super.stop();
084                                    consumers.remove(this);
085                            }
086                    };
087        }
088    
089        public E createExchange() {
090            // How can we create a specific Exchange if we are generic??
091            // perhaps it would be better if we did not implement this. 
092            return (E) new DefaultExchange(getContext());
093        }
094    
095            public boolean isAllowMultipleConsumers() {
096                    return allowMultipleConsumers;
097            }
098            public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) {
099                    this.allowMultipleConsumers = allowMutlipleConsumers;
100            }
101    
102            public boolean isSingleton() {
103                    return true;
104            }
105    
106    }