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.browse;
018    
019    import java.util.List;
020    import java.util.concurrent.CopyOnWriteArrayList;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Component;
024    import org.apache.camel.Consumer;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Processor;
027    import org.apache.camel.Producer;
028    import org.apache.camel.Service;
029    import org.apache.camel.impl.DefaultEndpoint;
030    import org.apache.camel.impl.DefaultProducer;
031    import org.apache.camel.processor.loadbalancer.LoadBalancer;
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: 820620 $
041     */
042    public class BrowseEndpoint extends DefaultEndpoint implements BrowsableEndpoint, Service {
043        private List<Exchange> exchanges;
044        private final LoadBalancer loadBalancer = new TopicLoadBalancer();
045    
046        public BrowseEndpoint() {
047        }
048    
049        public BrowseEndpoint(String uri, CamelContext camelContext) {
050            super(uri, camelContext);
051        }
052    
053        public BrowseEndpoint(String uri, Component component) {
054            super(uri, component);
055        }
056    
057        public BrowseEndpoint(String endpointUri) {
058            super(endpointUri);
059        }
060    
061        public boolean isSingleton() {
062            return true;
063        }
064    
065        public List<Exchange> getExchanges() {
066            if (exchanges == null) {
067                exchanges = createExchangeList();
068            }
069            return exchanges;
070        }
071    
072        public Producer createProducer() throws Exception {
073            return new DefaultProducer(this) {
074                public void process(Exchange exchange) throws Exception {
075                    onExchange(exchange);
076                }
077            };
078        }
079    
080        public Consumer createConsumer(Processor processor) throws Exception {
081            return new LoadBalancerConsumer(this, processor, loadBalancer);
082        }
083    
084        protected List<Exchange> createExchangeList() {
085            return new CopyOnWriteArrayList<Exchange>();
086        }
087    
088        /**
089         * Invoked on a message exchange being sent by a producer
090         *
091         * @param exchange the exchange
092         * @throws Exception is thrown if failed to process the exchange
093         */
094        protected void onExchange(Exchange exchange) throws Exception {
095            getExchanges().add(exchange);
096    
097            // lets fire any consumers
098            loadBalancer.process(exchange);
099        }
100    
101        public void start() throws Exception {
102            exchanges = createExchangeList();
103        }
104    
105        public void stop() throws Exception {
106            if (exchanges != null) {
107                exchanges.clear();
108                exchanges = null;
109            }
110        }
111    }