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