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 */
017package org.apache.camel.component.direct;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.camel.Component;
023import org.apache.camel.Consumer;
024import org.apache.camel.Processor;
025import org.apache.camel.Producer;
026import org.apache.camel.impl.DefaultEndpoint;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.UriEndpoint;
029import org.apache.camel.spi.UriParam;
030import org.apache.camel.spi.UriPath;
031import org.apache.camel.util.ObjectHelper;
032
033/**
034 * The direct component provides direct, synchronous call to another endpoint from the same CamelContext.
035 *
036 * This endpoint can be used to connect existing routes in the same CamelContext.
037 */
038@UriEndpoint(firstVersion = "1.0.0", scheme = "direct", title = "Direct", syntax = "direct:name", consumerClass = DirectConsumer.class, label = "core,endpoint")
039public class DirectEndpoint extends DefaultEndpoint {
040
041    private volatile Map<String, DirectConsumer> consumers;
042
043    @UriPath(description = "Name of direct endpoint") @Metadata(required = "true")
044    private String name;
045
046    @UriParam(label = "producer", defaultValue = "true")
047    private boolean block = true;
048    @UriParam(label = "producer", defaultValue = "30000")
049    private long timeout = 30000L;
050    @UriParam(label = "producer")
051    private boolean failIfNoConsumers = true;
052
053    public DirectEndpoint() {
054        this.consumers = new HashMap<>();
055    }
056
057    public DirectEndpoint(String endpointUri, Component component) {
058        this(endpointUri, component, new HashMap<String, DirectConsumer>());
059    }
060
061    public DirectEndpoint(String uri, Component component, Map<String, DirectConsumer> consumers) {
062        super(uri, component);
063        this.consumers = consumers;
064    }
065
066    public Producer createProducer() throws Exception {
067        if (block) {
068            return new DirectBlockingProducer(this);
069        } else {
070            return new DirectProducer(this);
071        }
072    }
073
074    public Consumer createConsumer(Processor processor) throws Exception {
075        Consumer answer = new DirectConsumer(this, processor);
076        configureConsumer(answer);
077        return answer;
078    }
079
080    public boolean isSingleton() {
081        return true;
082    }
083
084    public void addConsumer(DirectConsumer consumer) {
085        String key = consumer.getEndpoint().getKey();
086        consumers.put(key, consumer);
087    }
088
089    public void removeConsumer(DirectConsumer consumer) {
090        String key = consumer.getEndpoint().getKey();
091        consumers.remove(key);
092    }
093
094    public boolean hasConsumer(DirectConsumer consumer) {
095        String key = consumer.getEndpoint().getKey();
096        return consumers.containsKey(key);
097    }
098
099    public DirectConsumer getConsumer() {
100        String key = getKey();
101        return consumers.get(key);
102    }
103
104    public boolean isBlock() {
105        return block;
106    }
107
108    /**
109     * If sending a message to a direct endpoint which has no active consumer,
110     * then we can tell the producer to block and wait for the consumer to become active.
111     */
112    public void setBlock(boolean block) {
113        this.block = block;
114    }
115
116    public long getTimeout() {
117        return timeout;
118    }
119
120    /**
121     * The timeout value to use if block is enabled.
122     *
123     * @param timeout the timeout value
124     */
125    public void setTimeout(long timeout) {
126        this.timeout = timeout;
127    }
128
129    public boolean isFailIfNoConsumers() {
130        return failIfNoConsumers;
131    }
132
133    /**
134     * Whether the producer should fail by throwing an exception, when sending to a DIRECT endpoint with no active consumers.
135     */
136    public void setFailIfNoConsumers(boolean failIfNoConsumers) {
137        this.failIfNoConsumers = failIfNoConsumers;
138    }
139
140    protected String getKey() {
141        String uri = getEndpointUri();
142        if (uri.indexOf('?') != -1) {
143            return ObjectHelper.before(uri, "?");
144        } else {
145            return uri;
146        }
147    }
148}