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: 659782 $
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 public void addPropertyChangeListener(PropertyChangeListener listener) {
075 propertyChangeSupport.addPropertyChangeListener(listener);
076 }
077
078 public void removePropertyChangeListener(PropertyChangeListener listener) {
079 propertyChangeSupport.removePropertyChangeListener(listener);
080 }
081
082 public Producer<Exchange> createProducer() throws Exception {
083 return new DefaultProducer<Exchange>(this) {
084 public void process(Exchange exchange) throws Exception {
085 onExchange(exchange);
086 }
087 };
088 }
089
090 public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
091 return new LoadBalancerConsumer(this, processor, loadBalancer);
092 }
093
094 public void reset() {
095 exchanges = createExchangeList();
096 }
097
098 protected List<Exchange> createExchangeList() {
099 return new CopyOnWriteArrayList<Exchange>();
100 }
101
102 /**
103 * Invoked on a message exchange being sent by a producer
104 */
105 protected void onExchange(Exchange exchange) throws Exception {
106 exchanges.add(exchange);
107
108 // lets fire any consumers
109 loadBalancer.process(exchange);
110 }
111 }