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.list;
018    
019    import java.beans.PropertyChangeListener;
020    import java.beans.PropertyChangeSupport;
021    import java.util.List;
022    import java.util.concurrent.CopyOnWriteArrayList;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.Component;
026    import org.apache.camel.Consumer;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.Processor;
029    import org.apache.camel.Producer;
030    import org.apache.camel.impl.DefaultEndpoint;
031    import org.apache.camel.impl.DefaultProducer;
032    import org.apache.camel.processor.loadbalancer.LoadBalancerConsumer;
033    import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
034    import org.apache.camel.spi.BrowsableEndpoint;
035    
036    /**
037     * An endpoint which maintains a {@link List} of {@link Exchange} instances
038     * which can be useful for tooling, debugging and visualising routes.
039     *
040     * @version $Revision: 41278 $
041     */
042    public class ListEndpoint extends DefaultEndpoint<Exchange> implements BrowsableEndpoint<Exchange> {
043        private List<Exchange> exchanges;
044        private TopicLoadBalancer loadBalancer = new TopicLoadBalancer();
045        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
046    
047        public ListEndpoint(String uri, CamelContext camelContext) {
048            super(uri, camelContext);
049            reset();
050        }
051    
052        public ListEndpoint(String uri, Component component) {
053            super(uri, component);
054            reset();
055        }
056    
057        public ListEndpoint(String endpointUri) {
058            super(endpointUri);
059            reset();
060        }
061    
062        public boolean isSingleton() {
063            return true;
064        }
065    
066        public List<Exchange> getExchanges() {
067            return exchanges;
068        }
069    
070        public TopicLoadBalancer getLoadBalancer() {
071            return loadBalancer;
072        }
073    
074    
075        public void addPropertyChangeListener(PropertyChangeListener listener) {
076            propertyChangeSupport.addPropertyChangeListener(listener);
077        }
078    
079        public void removePropertyChangeListener(PropertyChangeListener listener) {
080            propertyChangeSupport.removePropertyChangeListener(listener);
081        }
082    
083        public Producer<Exchange> createProducer() throws Exception {
084            return new DefaultProducer<Exchange>(this) {
085                public void process(Exchange exchange) throws Exception {
086                    onExchange(exchange);
087                }
088            };
089        }
090    
091        public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
092            return new LoadBalancerConsumer(this, processor, loadBalancer);
093        }
094    
095        public void reset() {
096            exchanges = createExchangeList();
097        }
098    
099        protected List<Exchange> createExchangeList() {
100            return new CopyOnWriteArrayList<Exchange>();
101        }
102    
103        /**
104         * Invoked on a message exchange being sent by a producer
105         */
106        protected void onExchange(Exchange exchange) throws Exception {
107            exchanges.add(exchange);
108    
109            // lets fire any consumers
110            loadBalancer.process(exchange);
111        }
112    }