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.Component;
023 import org.apache.camel.Consumer;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.Processor;
026 import org.apache.camel.Producer;
027 import org.apache.camel.Service;
028 import org.apache.camel.impl.DefaultEndpoint;
029 import org.apache.camel.impl.DefaultProducer;
030 import org.apache.camel.processor.loadbalancer.LoadBalancer;
031 import org.apache.camel.processor.loadbalancer.LoadBalancerConsumer;
032 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
033 import org.apache.camel.spi.BrowsableEndpoint;
034
035 /**
036 * An endpoint which maintains a {@link List} of {@link Exchange} instances
037 * which can be useful for tooling, debugging and visualising routes.
038 *
039 * @version
040 */
041 public class BrowseEndpoint extends DefaultEndpoint implements BrowsableEndpoint, Service {
042 private List<Exchange> exchanges;
043 private final LoadBalancer loadBalancer = new TopicLoadBalancer();
044
045 public BrowseEndpoint() {
046 }
047
048 public BrowseEndpoint(String uri, Component component) {
049 super(uri, component);
050 }
051
052 public boolean isSingleton() {
053 return true;
054 }
055
056 public List<Exchange> getExchanges() {
057 if (exchanges == null) {
058 exchanges = createExchangeList();
059 }
060 return exchanges;
061 }
062
063 public Producer createProducer() throws Exception {
064 return new DefaultProducer(this) {
065 public void process(Exchange exchange) throws Exception {
066 onExchange(exchange);
067 }
068 };
069 }
070
071 public Consumer createConsumer(Processor processor) throws Exception {
072 return new LoadBalancerConsumer(this, processor, loadBalancer);
073 }
074
075 protected List<Exchange> createExchangeList() {
076 return new CopyOnWriteArrayList<Exchange>();
077 }
078
079 /**
080 * Invoked on a message exchange being sent by a producer
081 *
082 * @param exchange the exchange
083 * @throws Exception is thrown if failed to process the exchange
084 */
085 protected void onExchange(Exchange exchange) throws Exception {
086 getExchanges().add(exchange);
087
088 // now fire any consumers
089 loadBalancer.process(exchange);
090 }
091
092 @Override
093 protected void doStart() throws Exception {
094 exchanges = createExchangeList();
095 super.doStart();
096 }
097
098 @Override
099 protected void doStop() throws Exception {
100 if (exchanges != null) {
101 exchanges.clear();
102 exchanges = null;
103 }
104 super.doStop();
105 }
106 }