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.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.Service; 031 import org.apache.camel.impl.DefaultEndpoint; 032 import org.apache.camel.impl.DefaultProducer; 033 import org.apache.camel.processor.loadbalancer.LoadBalancerConsumer; 034 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer; 035 import org.apache.camel.spi.BrowsableEndpoint; 036 037 /** 038 * An endpoint which maintains a {@link List} of {@link Exchange} instances 039 * which can be useful for tooling, debugging and visualising routes. 040 * 041 * @version $Revision: 736555 $ 042 */ 043 public class BrowseEndpoint extends DefaultEndpoint implements BrowsableEndpoint, Service { 044 private List<Exchange> exchanges; 045 private TopicLoadBalancer loadBalancer = new TopicLoadBalancer(); 046 // TODO: firing of property changes not implemented 047 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); 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 return exchanges; 067 } 068 069 public TopicLoadBalancer getLoadBalancer() { 070 return loadBalancer; 071 } 072 073 public void addPropertyChangeListener(PropertyChangeListener listener) { 074 propertyChangeSupport.addPropertyChangeListener(listener); 075 } 076 077 public void removePropertyChangeListener(PropertyChangeListener listener) { 078 propertyChangeSupport.removePropertyChangeListener(listener); 079 } 080 081 public Producer createProducer() throws Exception { 082 return new DefaultProducer(this) { 083 public void process(Exchange exchange) throws Exception { 084 onExchange(exchange); 085 } 086 }; 087 } 088 089 public Consumer createConsumer(Processor processor) throws Exception { 090 return new LoadBalancerConsumer(this, processor, loadBalancer); 091 } 092 093 protected List<Exchange> createExchangeList() { 094 return new CopyOnWriteArrayList<Exchange>(); 095 } 096 097 /** 098 * Invoked on a message exchange being sent by a producer 099 * 100 * @param exchange the exchange 101 * @throws Exception is thrown if failed to process the exchange 102 */ 103 protected void onExchange(Exchange exchange) throws Exception { 104 exchanges.add(exchange); 105 106 // lets fire any consumers 107 loadBalancer.process(exchange); 108 } 109 110 public void start() throws Exception { 111 exchanges = createExchangeList(); 112 } 113 114 public void stop() throws Exception { 115 if (exchanges != null) { 116 exchanges.clear(); 117 exchanges = null; 118 } 119 } 120 }