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: 640438 $ 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 boolean isSingleton() { 058 return true; 059 } 060 061 public List<Exchange> getExchanges() { 062 return exchanges; 063 } 064 065 public TopicLoadBalancer getLoadBalancer() { 066 return loadBalancer; 067 } 068 069 070 public void addPropertyChangeListener(PropertyChangeListener listener) { 071 propertyChangeSupport.addPropertyChangeListener(listener); 072 } 073 074 public void removePropertyChangeListener(PropertyChangeListener listener) { 075 propertyChangeSupport.removePropertyChangeListener(listener); 076 } 077 078 public Producer<Exchange> createProducer() throws Exception { 079 return new DefaultProducer<Exchange>(this) { 080 public void process(Exchange exchange) throws Exception { 081 onExchange(exchange); 082 } 083 }; 084 } 085 086 public Consumer<Exchange> createConsumer(Processor processor) throws Exception { 087 return new LoadBalancerConsumer(this, processor, loadBalancer); 088 } 089 090 public void reset() { 091 exchanges = createExchangeList(); 092 } 093 094 protected List<Exchange> createExchangeList() { 095 return new CopyOnWriteArrayList<Exchange>(); 096 } 097 098 /** 099 * Invoked on a message exchange being sent by a producer 100 */ 101 protected void onExchange(Exchange exchange) throws Exception { 102 exchanges.add(exchange); 103 104 // lets fire any consumers 105 loadBalancer.process(exchange); 106 } 107 }