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.direct; 018 019 import java.util.concurrent.CopyOnWriteArrayList; 020 021 import org.apache.camel.Consumer; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Processor; 024 import org.apache.camel.Producer; 025 import org.apache.camel.impl.DefaultConsumer; 026 import org.apache.camel.impl.DefaultEndpoint; 027 import org.apache.camel.impl.DefaultExchange; 028 import org.apache.camel.impl.DefaultProducer; 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 032 /** 033 * Represents a direct endpoint that synchronously invokes the consumers of the 034 * endpoint when a producer sends a message to it. 035 * 036 * @org.apache.xbean.XBean 037 * @version $Revision: 519973 $ 038 */ 039 public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> { 040 private static final Log LOG = LogFactory.getLog(DirectEndpoint.class); 041 042 boolean allowMultipleConsumers = true; 043 private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>(); 044 045 public DirectEndpoint(String uri, DirectComponent<E> component) { 046 super(uri, component); 047 } 048 049 public Producer createProducer() throws Exception { 050 return new DefaultProducer(this) { 051 public void process(Exchange exchange) throws Exception { 052 DirectEndpoint.this.process(exchange); 053 } 054 }; 055 } 056 057 protected void process(Exchange exchange) throws Exception { 058 if (consumers.isEmpty()) { 059 LOG.warn("No consumers available on " + this + " for " + exchange); 060 } else { 061 for (DefaultConsumer<E> consumer : consumers) { 062 consumer.getProcessor().process(exchange); 063 } 064 } 065 } 066 067 public Consumer<E> createConsumer(Processor processor) throws Exception { 068 return new DefaultConsumer<E>(this, processor) { 069 @Override 070 public void start() throws Exception { 071 if (!allowMultipleConsumers && !consumers.isEmpty()) { 072 throw new IllegalStateException("Endpoint " + getEndpointUri() + " only allows 1 active consumer but you attempted to start a 2nd consumer."); 073 } 074 075 consumers.add(this); 076 super.start(); 077 } 078 079 @Override 080 public void stop() throws Exception { 081 super.stop(); 082 consumers.remove(this); 083 } 084 }; 085 } 086 087 public E createExchange() { 088 // How can we create a specific Exchange if we are generic?? 089 // perhaps it would be better if we did not implement this. 090 return (E)new DefaultExchange(getContext()); 091 } 092 093 public boolean isAllowMultipleConsumers() { 094 return allowMultipleConsumers; 095 } 096 097 public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) { 098 this.allowMultipleConsumers = allowMutlipleConsumers; 099 } 100 101 public boolean isSingleton() { 102 return true; 103 } 104 105 }