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.List;
020 import java.util.concurrent.CopyOnWriteArrayList;
021
022 import org.apache.camel.Consumer;
023 import org.apache.camel.Exchange;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Producer;
026 import org.apache.camel.impl.DefaultConsumer;
027 import org.apache.camel.impl.DefaultEndpoint;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030
031 /**
032 * Represents a direct endpoint that synchronously invokes the consumers of the
033 * endpoint when a producer sends a message to it.
034 *
035 * @version $Revision: 660275 $
036 */
037 public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
038 private static final transient Log LOG = LogFactory.getLog(DirectEndpoint.class);
039 private boolean allowMultipleConsumers = true;
040 private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>();
041
042 public DirectEndpoint(String uri, DirectComponent<E> component) {
043 super(uri, component);
044 }
045
046 public DirectEndpoint(String endpointUri) {
047 super(endpointUri);
048 }
049
050 public Producer createProducer() throws Exception {
051 return new DirectProducer<E>(this);
052 }
053
054 public Consumer<E> createConsumer(Processor processor) throws Exception {
055 return new DefaultConsumer<E>(this, processor) {
056 @Override
057 public void start() throws Exception {
058 if (!allowMultipleConsumers && !consumers.isEmpty()) {
059 throw new IllegalStateException("Endpoint " + getEndpointUri() + " only allows 1 active consumer but you attempted to start a 2nd consumer.");
060 }
061
062 consumers.add(this);
063 super.start();
064 }
065
066 @Override
067 public void stop() throws Exception {
068 super.stop();
069 consumers.remove(this);
070 }
071 };
072 }
073
074 public boolean isAllowMultipleConsumers() {
075 return allowMultipleConsumers;
076 }
077
078 public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) {
079 this.allowMultipleConsumers = allowMutlipleConsumers;
080 }
081
082 public boolean isSingleton() {
083 return true;
084 }
085
086 public List<DefaultConsumer<E>> getConsumers() {
087 return consumers;
088 }
089 }